1
\$\begingroup\$

I move a simple CCSprite around the screen of an iOS device using this code:

[self schedule:@selector(update:) interval:0.0167];

- (void) update:(ccTime) delta {
    CGPoint currPos = self.position;
    currPos.x += xVelocity;
    currPos.y += yVelocity;

    self.position = currPos;
}

This works however the animation is not smooth. How can I improve the smoothness of my animation?

My scene is exceedingly simple (just has one full-screen CCSprite with a background image and a relatively small CCSprite that moves slowly).

I've logged the ccTime delta and it's not consistent (it's almost always greater than my specified interval of 0.0167... sometimes up to a factor of 4x).

I've considered tailoring the motion in the update method to the delta time (larger delta => larger movement etc). However given the simplicity of my scene it's seems there's a better way (and something basic that I'm probably missing).

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

you can try this code:

[self schedule:@selector(update:) interval:0.0167];

- (void) update:(ccTime) delta {
    CGPoint currPos = self.position;
    currPos.x += xVelocity * delta;
    currPos.y += yVelocity * delta;

    self.position = currPos;
}

this way you are using the delta time provided to you, it means object will move with the same speed no matter how many times update function is called in one second. it also should help your animation look smoother.

\$\endgroup\$
2
  • \$\begingroup\$ Definitely a good idea. However calling scheduleUpdate instead of schedule:interval: worked much better. \$\endgroup\$
    – MrDatabase
    Commented Nov 25, 2011 at 1:14
  • \$\begingroup\$ @MrDatabase that's for you to decide, each of those functions have their own benefits in certain situations, but in this case I agree with calling scheduleUpdate \$\endgroup\$
    – Ali1S232
    Commented Nov 25, 2011 at 8:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .