开发者

Simple UIKit game: What is a good approach for repeatedly putting new objects on screen?

Currently, I'm building a simple game using pure UIKit. In my game content MVC, I have a CADisplayLink timer object that is activated on game and is repeatedly calling gameLogic method for displaying new objects. The method calculates and renews object positions on the screen. The frameInterval of my timers is 2 frames. Now, I need somehow to set an interval for the repeated appearance of new objects on the screen. The solution that I have used so far is to use a static counter in gameLogic method. Here is a fragment of the method:

-(void)gameLogic{
  static int timeCounter = 0;
  if ((timeCounter % 50)==0){ 
    [self addItemToScreen];
  }
  ...
  timeCounter++;

At every 50th cycle of gameLogic method execution, I'm putting a new object on screen. The rest code of gameLogic method repositions the existing objects. Currently, everything is managed by only one timer. The other solution that IMHO should work is to have a separate timer for adding new objects on screen. But I'm not sure, is i开发者_JS百科t a better solution, and will they work successfully concurrently. What are your opinions about these approaches? What other solutions you propose to use?


I think its fine to have all your objects respond to a frame counter. However, I would likely instead dispatch a notification across your application instead of having one function that has a ton of code in it, doing all kinds of things. I think you really should consider breaking up all that logic into separate classes, i.e. have a GameObjectCreator class that responds to the notification posted when your timer fires, and only have that class add objects to the view. For your code that rearranges the objects on screen, they could also respond to the same notification, and be either handled by a parent controller to manipulate all the objects, or each individual object could respond to the notification as well. Performance wise I'm not sure if using notifications is bad with a lot of objects responding at once, but something like this is more the approach I would take so you don't have all this spaghetti code in one single timer function.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜