MVC - 游戏 - Objective C

发布于 2024-12-02 08:29:07 字数 478 浏览 3 评论 0原文

我正在为 iPhone 开发一款游戏。该游戏基本上使屏幕上的对象具有动画效果,允许用户单击它们。我正在尝试使用 MVC 模式。我的功能如下:

模型: 保存有关目标的数据(速度、相对大小、图像等) 计时器正在运行,将目标添加到列表中(这应该在控制器中吗?)

控制器: 订阅从模型激发的事件(例如添加的目标) 订阅从视图触发的事件(例如单击目标)

视图: 显示目标

流程如下:

  1. 控制器告诉模型开始游戏
  2. 模型触发计时器,提示添加目标
  3. 控制器听到事件并将其传递给视图
  4. 视图将图像添加到屏幕(对其进行动画处理)
  5. 用户单击图像
  6. 视图触发事件,提示该事件单击图像
  7. 控制器听到事件并将其传递给模型
  8. 模型从自身中删除目标

最后,我不确定将动画放在哪里。视图是否应该构造动画(基于模型的设置)?

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:

  1. Controller tells Model to start game
  2. Model fires timer which says to add a target
  3. Controller hears event and passes it to view
  4. View adds image to screen (animating it)
  5. User clicks on image
  6. View fires event which says image was clicked
  7. Controller hears event and passes it to model
  8. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-12-09 08:29:07

我会考虑让视图或控制器处理动画,这样您就可以利用 Cocoa 的内置动画支持。例如,它会像这样:

  1. View 查询位置的模型
  2. View 将图像添加到屏幕上的位置
  3. View 查询速度的模型
  4. View 在 0.1 秒后计算新位置并将图像动画到新位置
  5. Cocoa 触发动画完成事件
  6. View 听到事件
  7. View 计算新位置0.1 秒后并将图像动画到新位置

如果目标可以改变速度,您有一些选择。您可以让模型触发事件来改变速度,并取消正在进行的动画并开始一个新的动画。或者,您可以让视图在每次动画完成时重新查询模型,询问有效的位置和速度,并计算下一个位置。对于后者,模型和视图之间会存在一些不同步,但每 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:

  1. View queries model for position
  2. View adds image to screen at position
  3. View queries model for velocity
  4. View computes new position after 0.1s and animates image to new position
  5. Cocoa fires animation complete event
  6. View hears event
  7. View computes new position after 0.1s more and animates image to new position

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.

青柠芒果 2024-12-09 08:29:07

我认为动画将是模型(或模型中的模型)的一部分,由控制器指定,并由视图呈现。

这是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文