MVC - 游戏 - Objective C
我正在为 iPhone 开发一款游戏。该游戏基本上使屏幕上的对象具有动画效果,允许用户单击它们。我正在尝试使用 MVC 模式。我的功能如下:
模型: 保存有关目标的数据(速度、相对大小、图像等) 计时器正在运行,将目标添加到列表中(这应该在控制器中吗?)
控制器: 订阅从模型激发的事件(例如添加的目标) 订阅从视图触发的事件(例如单击目标)
视图: 显示目标
流程如下:
- 控制器告诉模型开始游戏
- 模型触发计时器,提示添加目标
- 控制器听到事件并将其传递给视图
- 视图将图像添加到屏幕(对其进行动画处理)
- 用户单击图像
- 视图触发事件,提示该事件单击图像
- 控制器听到事件并将其传递给模型
- 模型从自身中删除目标
最后,我不确定将动画放在哪里。视图是否应该构造动画(基于模型的设置)?
I am developing a game for the iPhone. The game basically animates objects on the screen, allowing a user to click on them. I am trying to use the MVC pattern. I have the functionality laid out as such:
Model:
Holds data about the targets (speed, relative size, image, etc)
Has timer running that adds targets to the list (should this be in the controller?)
Controller:
Subscribes to events fired from the model (such as target added)
Subscribes to events fired from the view (such as target clicked)
View:
Displays targets
The flow can be as follows:
- Controller tells Model to start game
- Model fires timer which says to add a target
- Controller hears event and passes it to view
- View adds image to screen (animating it)
- User clicks on image
- View fires event which says image was clicked
- Controller hears event and passes it to model
- Model removes target from itself
Lastly, I am unsure where to put the animation. Should the view construct the animation (based off of settings from the model)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑让视图或控制器处理动画,这样您就可以利用 Cocoa 的内置动画支持。例如,它会像这样:
如果目标可以改变速度,您有一些选择。您可以让模型触发事件来改变速度,并取消正在进行的动画并开始一个新的动画。或者,您可以让视图在每次动画完成时重新查询模型,询问有效的位置和速度,并计算下一个位置。对于后者,模型和视图之间会存在一些不同步,但每 0.1 秒更新一次,不会偏离太远。这取决于您需要的精确程度。
I'd consider having the view or controller handle the animation so you can take advantage of Cocoa's built-in animation support. For example it would go like this:
And if targets can change velocity, you have some options. You can have the model fire events for velocity changes, and cancel the animation in progress and start a new one. Or you can just have the view requery the model each time an animation completes, ask for the valid position and velocity, and compute the next position. With the latter there would be some missynchronization between the model and the view, but with updates every 0.1s it wouldn't get too far off. It depends how precise you need to be.
我认为动画将是模型(或模型中的模型)的一部分,由控制器指定,并由视图呈现。
这是 iPhone 的一个很好的示例:
http://www.bit-101.com/博客/?p=1969
I would think that an animation would be part of a model (or a model within a model), specified by a controller, and rendered by a view.
Here's a good example for the iPhone:
http://www.bit-101.com/blog/?p=1969