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)?
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.