Cocos2d: Adding a CCSequence to a CCArray
- by Axort
I have a problem with an action performed by a sprite. I have one CCSequence in a CCArray and I have an scheduled method (is called every 5 seconds) that make the sprite run the action. The action is performed correctly only the first time (the first 5 seconds), after that, the action do whatever it wants lol.
Here is the code:
In .h -
@interface PowerUpLayer : CCLayer {
PowerUp *powerUp;
CCArray *trajectories;
}
@property (nonatomic, retain) CCArray *trajectories;
In .mm -
@implementation PowerUpLayer
@synthesize trajectories;
-(id)init
{
if((self = [super init]))
{
[self createTrajectories];
self.isTouchEnabled = YES;
[self schedule:@selector(spawn:) interval:5];
}
return self;
}
-(void)createTrajectories
{
self.trajectories = [CCArray arrayWithCapacity:1];
//Wave trajectory
ccBezierConfig firstWave, secondWave;
firstWave.controlPoint_1 = CGPointMake([[CCDirector sharedDirector] winSize].width + 30, [[CCDirector sharedDirector] winSize].height / 2);//powerUp.sprite.position.x, powerUp.sprite.position.y);
firstWave.controlPoint_2 = CGPointMake([[CCDirector sharedDirector] winSize].width - ([[CCDirector sharedDirector] winSize].width / 4), 0);
firstWave.endPosition = CGPointMake([[CCDirector sharedDirector] winSize].width / 2, [[CCDirector sharedDirector] winSize].height / 2);
secondWave.controlPoint_1 = CGPointMake([[CCDirector sharedDirector] winSize].width / 2, [[CCDirector sharedDirector] winSize].height / 2);
secondWave.controlPoint_2 = CGPointMake([[CCDirector sharedDirector] winSize].width / 4, [[CCDirector sharedDirector] winSize].height);
secondWave.endPosition = CGPointMake(-30, [[CCDirector sharedDirector] winSize].height / 2);
id bezierWave1 = [CCBezierTo actionWithDuration:1 bezier:firstWave];
id bezierWave2 = [CCBezierTo actionWithDuration:1 bezier:secondWave];
id waveTrajectory = [CCSequence actions:bezierWave1, bezierWave2, [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)], nil];
[self.trajectories addObject:waveTrajectory];
//[powerUp.sprite runAction:bezierForward];
// [CCMoveBy actionWithDuration:3 position:CGPointMake(-[[CCDirector sharedDirector] winSize].width - powerUp.sprite.contentSize.width, 0)]
//[powerUp.sprite runAction:[CCSequence actions:bezierWave1, bezierWave2, [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)], nil]];
}
-(void)setInvisible:(id)sender
{
if(powerUp != nil)
{
[self removeChild:sender cleanup:YES];
powerUp = nil;
}
}
This is the scheduled method:
-(void)spawn:(ccTime)dt
{
if(powerUp == nil)
{
powerUp = [[PowerUp alloc] initWithType:0];
powerUp.sprite.position = CGPointMake([[CCDirector sharedDirector] winSize].width + powerUp.sprite.contentSize.width, [[CCDirector sharedDirector] winSize].height / 2);
[self addChild:powerUp.sprite z:-1];
[powerUp.sprite runAction:((CCSequence *)[self.trajectories objectAtIndex:0])];
}
}
I don't know what is happening; I never modify the content of the CCSequence after the first time.
Thanks!