Question

I am developing an iPhone version of this game. http://www.students.uni-mainz.de/rathb000/Fillit/Game.html

I am using an NSTimer to change the position of the ship by 1 pixel and the time interval for the timer is 0.002.

However, i have noticed that the ship doesnt move faster if i reduce the time interval furthur. Infact, there is no difference between 0.05 and 0.002. Is there is a restriction on the minimum time you have to give as NSTimeInterval?

The ship currently moves too slow for the game to be exciting.

Also, the movement of birds (which is also achieved by NSTimer) becomes slow when i press any button for moving the ship.

Any idea about how this can be solved?

Was it helpful?

Solution 6

Figured this out.

Had to use Core Animation to move the ship and made the birds move by NSTimer. Works fine this way but only upto a certain limit. Birds again slow down when the speed of the ship increases more than a certain limit.

OTHER TIPS

NSTimer is not a real-time mechanism; it fires as part of the run loop and is not ideal for creating smooth, fluid animations. However, there is an SDK that is part of the iPhone OS called Core Animation - it provides a library of functions for performing smooth animation of layers and views. Here is a reasonably good series of video tutorials that you may find useful. There is also an excellent book called Core Animation for Mac OS X and iPhone that you may find useful.

There is also the core animation documentation from Apple that describes how to use the CABasicAnimation class to animate properties of any layer or view. Here is an example of an explicit layer animation from the docs:

CABasicAnimation *theAnimation; 

theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=3.0;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;
theAnimation.fromValue=CATransform3DTranslate(0,0,0);
theAnimation.toValue=CATransform3DTranslate(20,20,0);

[theLayer addAnimation:theAnimation forKey:@"animateTransform"];

From NSTimer docs:

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs while the run loop is in a mode that is not monitoring the timer or during a long callout, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

Maybe you can try moving the UIView using UIView animations instead of a timer.

You could consider increasing the distance size: move the ship 3 or 4 pixels every .05 seconds rather than 1 pixel every .002 seconds.

In my opinion, you should be using the same timestep globally across all entities in your game, not a timestep exclusively for the player's ship. If the ship is moving too slowly with your timestep, increase the amount of distance it is moving per frame.

Really, you should be thinking of moving your player's ship in terms of pixels/second. When the NSTimer event fires, get the elapsed time since the last update. Use this elapsed time to move your game entities. For instance, if your update timestep is 0.05 seconds and you want to move the player's ship 100 pixels per second, then you should move it 100 * 0.05 = 5 pixels in this update. This method will account for varying size timesteps and your game entities will always appear to moving at a consistent speed, even if your timer mechanism is not that consistent.

Not only is NSTimer not the right tool for this type of animation your design is wrong.

A timer with an inteval of 0002 fires 500 times per second. That's a crazy way to animate this game.

I suggest you read up on 2D game design and find out a better way to do collision detection - like anonymous suggests you should have a collision rectangle round the ship rather than relying on the actual pixels.

You should also check out Cocos-2D for iPhone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top