Reputation: 12376
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.
Upvotes: 0
Views: 249
Reputation: 9089
In your tick method, i would stop action on the move before running a new one. Also, instead of using 'time' for action duration, use .5f. The time is what you actually got for THIS tick, probably some number off from .5 (+/- 1/60 secs if i had to guess). As more objects are on screen, coupled with user actions, the 'time' received on the scheduled method will become more random. I personally only use that to compute elapsed time between two meta-events handled by the scheduled method.
Upvotes: 1
Reputation: 10860
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.
Upvotes: 1