views:

1109

answers:

6

Hi,

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?

A: 

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.

pgb
A: 

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

Dan Lorenc
This would make the logic for collision detection go wrong. I will have to flag all the points the the ship pass on. Maybe i can take an array and flag the in-between points but not sure if that is a good idea.
Chintan Patel
A: 

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.

Andrew Garrison
Yes, this sounds good but i have to keep the ship move only 1 pixel at a time. It is needed for the logic of the game. Otherwise, the bird may not detect the line and would pass through a filled area or may not kill the ship when the path is incomplete. So i want to know if there are any such "translation" functions in quartz which would speed up the movement.
Chintan Patel
I'm assuming that the collision detection uses the rectangle around the player ship. You could increase this rectangle to encompass the player ship, and its velocity. For instance, if your player ship moved 10 pixels in the y-axis, you could use a rectangle that included the bounds of the player ship, but also extends 10 pixels past the ship on the y-axis to include the amount of distance the ship travelled in the last frame.
Andrew Garrison
No, there is no collision for the ship and the birds. Only the line made by the ship and the birds. So all the points on the line need to be flagged 3 so that we would know when the bird crossed the line. If it is flagged 4, the bird has to deflect. So i have to make each and every point on the ships path a particular value and so the ship cant be made to move more than 1 pixel at a time.
Chintan Patel
+2  A: 

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"];
LBushkin
Thank you so much for the link. On it.
Chintan Patel
No problem, you can also choose to accept my answer as the accepted answer with the check mark icon if you found it useful.
LBushkin
This is yet to be answered. I am still stuck.. :(
Chintan Patel
Will surely try out this one. So, will this code make my ship move from 0,0 to 20,20 with animation? I mean not "blink and its there" but with a smooth movement which lets you know it moves from here to there.
Chintan Patel
Hi, tried this code. The values assigned to the fromValue and toValue seem to be wrong. It requires an NSNumber type. I am reading the book you mentioned but it tells very little about animation on iPhone. Its mostly for Mac.
Chintan Patel
A: 

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

Roger Nolan
Yaa, i know i am wrong at this. But as i said in the comment above, i dont need collision detection between the ship and the bird. Yaa, i tried cocos2d for about a week but found that my game is simple enough to be made in quartz. Also, i couldnt learn it as fast as users often claim about cocos2d. Thanks, anyways.
Chintan Patel
So you have your solution and you don't want the advice from Stack Overflow. Why even bother to ask the question?
Roger Nolan
Where did i mention that i have the solution and that i dont want advice? Perhaps you should read my comments above before forming an opinion. Believe me, it will help a lot in avoiding some exchanges.
Chintan Patel
Maybe I misinterpreted your answers - they seemed to all be "yes but this won't work because..." even though they were good answers. if I did that and I'm mistaken, I apologise.
Roger Nolan
No problem. Its ok... My problem is i have to keep the step as 1 pixel and make the ship's movement as fast as possible without changing that. This is the problem. Just making it move 2 or 3 pixels in one step will make the game's logic go wrong.
Chintan Patel
...and the advice is that if your logic makes that constraint, your logic needs to change. Don't use the actual pixels for collision detection. Set up a collision box and your animation will become easier.
Roger Nolan
A: 

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.

Chintan Patel