Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I just finished getting movement/jump animation for a player sprite in Xcode using Cocos2D. The basic movement algorithm is a timer that updates every 0.01 sec, changing the sprite position to (sprite.position.x + xVel, sprite.position.y + yVel). Each time a movement button is tapped, the appropriate velocity (initialized to 0) is changed to whatever speed I choose, then a stop movement button returns the velocity to 0. It's not an ideal solution but I'm very new at this and stoked to at least have that working with little help from the internet.

So I may not have explained that perfectly, but it is in fact working to my satisfaction in Xcode's iPhone Simulator, however when I build it for my device and run it on my phone, the sprite's movement speed is noticeably slower than in Xcode. At first I thought it must have to do with the resolution of the iPhone 4, making the sprite's movement path twice as long, but I found that if I pull up the multitask bar, then return to the app the speed will sometimes jump back to normal. My second theory was that the code is just inefficient and is bogging the processes down, but I would see this reflected in the frame rate wouldn't I? It stays at 59-60 the whole time, and the spritesheet animation runs at the correct speed.

Has anyone experienced this? Is this a really obvious issue that I'm completely missing? Any help (or tips for optimizing my approach to movement) would be much appreciated!

share|improve this question
    
You should probably post some more code. I wonder why you chose a timer instead of using the Cocos2D update method? –  bummzack Apr 21 '12 at 9:16
    
Thanks for the reply! I'm really new to Cocos2D and programming in general. After reading some of the documentation on update, I'm definitely going to use it instead. –  nvillec Apr 21 '12 at 14:23
    
This might not solve your problem. dqhendricks' answer will probably solve this issue. –  Coyote Dec 28 '12 at 23:18
add comment

1 Answer

The reason it is different speeds is because you are not calculating/using a delta time. Basically the frame rate could be any number at any time slowing or speeding up your animations. To counteract this, programmers calculate a delta time:

delta = currentTime - previousFramesTime; // time difference between this frame and the last frame

Then use this in their animation:

position.y = position.y + (velocity * delta);

This way no matter what the framerate is, your animations will remain the same speed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.