Cocos2D:重用存储在CCArray中的CCSequence

时间:2012-03-31 00:18:08

标签: objective-c cocos2d-iphone

我对精灵执行的操作有问题。我在CCArray中有一个CCSequence,我有一个调度方法(每5秒调用一次),使sprite运行动作。该动作仅在第一次(前5秒)正确执行,之后,动作执行任何它想要的操作。

以下是代码:

在.h - >

@interface PowerUpLayer : CCLayer {
    PowerUp *powerUp;
    CCArray *trajectories;
}

@property (nonatomic, retain) CCArray *trajectories;

在.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;
    }
}

这是预定的方法:

-(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])];
    }
}

我不知道发生了什么事;我从未在第一次之后修改CCSequence的内容。

谢谢!

1 个答案:

答案 0 :(得分:0)

CCAction课程是一次性的。一旦他们跑了,他们就不会重置他们的状态。如果你第二次重新开始行动,就会发生任何事情。

现在有open issue requesting reusable actions已存在超过2年了。有人建议再次简单地调用action的initWith ...方法来重置其状态,但如果特定操作是保留对象,则that's against good practices and may be downright dangerous。最终可能会出现内存泄漏,或者更糟。

解决方案:每次需要时重新创建序列。目前没有更安全的选择。