Search code examples
cocos2d-iphoneframe-rate

Cocos2D- Frame rate hiccups with only one small image


I'm trying to have a car travel from 0 to offscreen. But I can see there's an obvious hiccup in the display while car moves. Let me show you what I do:

I have a @property (nonatomic,strong) CCSprite car;

In init method I do the following:

self.car=[CCSprite spriteWithFileName:@"car.png"];
CGSize *windowSize=[[CCDirector sharedDirector] winSize];
CGSize carSize=car.contentSize;
car.position=ccp(0-carSize.width/2,windowSize.height/2);
[selp addChild:car];
[self schedule:@selecor(tick:) interval:0.5];

Here's the tick method:

-(void)tick:(ccTime)time{
 [[self.car runAction:MoveBy actionWithDuration:time position:ccp(100,0)];
}

As you see it's just a simple test app. The size of the image "car.png" is 64x128. In the AppDelegate.m the frame rate is set to 30 FPS.


Solution

  • First of all, if you just want to make your sprite move out of screen borders, why dont't just use one action without starting new one every 0.5 seconds? You can create the single action. Also with such method as yours you will have problems in case of lags. If your app, for example, will have, by any reason, 2 seconds lag, after it your sprite wil slowly move by 100 points by X axis, because there is no guaranty that you always will receive 0.5 as time parmeter. In case of 2 seconds lag you will receive time = 2.

    If you really want to move your sprite in some kind of update method, it is better to change it's position with setPosition, not with action. For example it can be useful if you want to synchronize your sprite's display position with it's position in physical world(b2World, etc.). Or in case of some kind of complicated movement. For example, in case of movement with formula sin(t), or archimedes' spiral.