像CCSpawn这样的CCSequence使用来自不同类的函数

时间:2012-05-10 07:07:02

标签: cocos2d-iphone action sequence spawn

我尝试在GameLayer中使用一些函数。首先 - 从另一个类,第二个 - 来自GameLayer类,但CCSequence像CCSpawn一样运行,而不是序列。另外,他们在GameLayer中都很完美。

在GameLayer中

    [self runAction:[CCSequence actions:
                    [CCCallFuncN actionWithTarget:self.rolypoly selector:@selector(jumpToDeath:)],
                    [CCCallFuncND actionWithTarget:self selector:@selector(goToGameOverLayer:tagName:)data:(int)TagGameOverLose],     
                     nil]];

in rolypoly class

-(void)jumpToDeath:(id)sender
{

       [self.sprite stopAllActions];

       id actionSpaw = [CCSpawn actions:
                       [CCMoveTo actionWithDuration:0.5f position:ccp(self.sprite.position.x, self.sprite.position.y+self.sprite.contentSize.height)],
                       [CCBlink actionWithDuration:1.0f blinks:4],
                        nil];

       [self.sprite runAction:[CCSequence actions:
                              [CCCallFuncND actionWithTarget:self selector:@selector(setJumpingToDeath:withValue:)data:(void*)1],
                              actionSpaw,
                              [CCHide action],
                              [CCCallFunc actionWithTarget:self selector:@selector(moveSpriteDeath)], 
                              nil]];


}

1 个答案:

答案 0 :(得分:1)

问题是runAction不是阻止方法。这意味着当您使用函数调用操作(如CCCallFunc)时,将在函数调用返回时执行序列中的操作。在您的情况下jumpToDeath运行操作但不等待它们完成,当它返回时,执行主序列中的第二个操作(在jumpToDeath内的序列完成之前)

尝试重新安排您的行动。如果您需要更多帮助,请告诉我。

编辑:我的建议:

 [self runAction:[CCSequence actions:
                    [CCCallFuncN actionWithTarget:self.rolypoly selector:@selector(jumpToDeath:)], nil]];

-(void)jumpToDeath:(id)sender
{

       [self.sprite stopAllActions];

       id actionSpaw = [CCSpawn actions:
                       [CCMoveTo actionWithDuration:0.5f position:ccp(self.sprite.position.x, self.sprite.position.y+self.sprite.contentSize.height)],
                       [CCBlink actionWithDuration:1.0f blinks:4],
                        nil];

       [self.sprite runAction:[CCSequence actions:
                              [CCCallFuncND actionWithTarget:self selector:@selector(setJumpingToDeath:withValue:)data:(void*)1],
                              actionSpaw,
                              [CCHide action],
                              [CCCallFunc actionWithTarget:self selector:@selector(moveSpriteDeath)],
                              [CCCallFuncND actionWithTarget:self selector:@selector(goToGameOverLayer:tagName:)data:(int)TagGameOverLose],
                              nil]];
}

正如您所看到的,我将最后一次调用函数操作移动到jumpToDeath方法中的最后一个,以确保在完成所有其他操作后最后执行该操作。

我不知道moveSpriteDeath的实现是什么,但是如果它不包含行动而不是正常,否则显示其实现或尝试做同样的