开发者

MVC - Game - Objective C

I am developing a game for the iPhone. The game ba开发者_开发技巧sically 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)?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜