如何为CCAction的每一帧设置回调?

时间:2012-09-18 01:06:39

标签: cocos2d-iphone

我想这样做:

id action = [CCScaleTo actionWithDuration:0.5 scaleX:self.Xposition scaleY:1];
id ease   = [CCEaseInOut actionWithAction:action rate:4];

[someObject[i] runAction:ease];

但是我希望能够挂钩动作的每一帧来做一些事情...我想知道除了在someObject上创建更新方法之外是否有任何方法可以实现这一点?我试图避免这样做,因为它是一个CCSprite,我目前没有一个对象包装器。它实际上只是在头部定义为类似CCSprite * someObject [4] ...所以我只是想知道如果有任何方法在runAction的上下文中指定每帧回调?

2 个答案:

答案 0 :(得分:1)

没有本地方法可以做到这一点。

如果这是您真正想要做的而不是使用更新方法,您可以创建一个类似于Ease操作的工作方式的类。将另一个操作作为参数并覆盖更新以修改其行为。

例如,如果您将其命名为CCCallBackAction,则会按如下方式创建它:

id action = [CCScaleTo actionWithDuration:0.5 scaleX:self.Xposition scaleY:1];
id ease   = [CCEaseInOut actionWithAction:action rate:4];
id callback = [CCCallBackAction actionWithAction:ease target:self selector:@selector(callback:)];

[someObject[i] runAction:callback];

在CCCallBackAction中,您将覆盖更新

- (void)update:(ccTime)dt
{
     [target performSelector:SEL];
     [other update:dt];
}

注意避免误解: CCCallBackAction不是cocos2d类,在此示例中是假设的

答案 1 :(得分:1)

阻止救援!!! :)

id action = [CCScaleTo actionWithDuration:0.5 scaleX:self.Xposition scaleY:1];
id ease   = [CCEaseInOut actionWithAction:action rate:4];
id call = [CCCallBlockN actionWithBlock:^(CCNode* node) {
    // node is the sprite, you may have to cast it:
    CCSprite* someSprite = (CCSprite*)node;

    // do whatever you need to do with sprite here…
    someSprite.opacity = CCRANDOM_0_1() * 255;
}];
id sequence = [CCSequence actions:action, ease, call, nil];

[someObject[i] runAction:sequence];

或者你也可以简单地使用someObject [i]数组,甚至可以访问其他精灵:

id action = [CCScaleTo actionWithDuration:0.5 scaleX:self.Xposition scaleY:1];
id ease   = [CCEaseInOut actionWithAction:action rate:4];
id call = [CCCallBlock actionWithBlock:^{
    CCSprite* someSprite = someObject[i];
    CCSprite* firstSprite = someObject[0];

    // do whatever you need to do with sprite here…
}];
id sequence = [CCSequence actions:action, ease, call, nil];

[someObject[i] runAction:sequence];