Reputation: 1238
In my cocos2d project I am moving sprites from left most corner to the right most corner using CCMoveBy action. here is the code
CCSprite* sprite1 = [CCSprite spriteWithFile:@"Icon.png"];
sprite1.position = ccp(100, 100);
[self addChild:sprite1];
[sprite1 runAction:[CCSequence actions:
[CCMoveBy actionWithDuration:4 position:ccp(300, 0)],
[CCMoveBy actionWithDuration:2 position:ccp(0, 200)],
[CCMoveBy actionWithDuration:4 position:ccp(-300, 0)],
[CCMoveBy actionWithDuration:2 position:ccp(0, -200)], nil]];
the sprite is not moving smoothly, instead it stuck sometimes while moving. someone asked a similar question In cocos2d forum
but In my game I am using action sequences at so many places and it would be too much to code every movement sequence by scheduling update or custom selectors.
Upvotes: 0
Views: 1576
Reputation: 5545
i think you can achieve this by following code
MovingSprite.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface MovingSprite : CCSprite {
float _vx;
float _vy;
}
-(void) update:(ccTime)dt;
@property (nonatomic, assign) float vx;
@property (nonatomic, assign) float vy;
@end
MovingSprite.m
#import "MovingSprite.h"
@implementation MovingSprite
@synthesize vx = _vx;
@synthesize vy = _vy;
-(void)update:(ccTime)dT
{
self.vy -= (kGravity * dT); //optionally apply gravity
self.position = ccp(self.position.x + (self.vx*dT), self.position.y + (self.vy*dT));
}
And add [self scheduleUpdate]; to the init method of your game layer. Then add an update method within the game layer where you call update for all moving sprites.
Upvotes: 0