0

我想要做:

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 的上下文中指定每帧回调?

4

2 回答 2

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 类,在这个例子中是假设的

于 2012-09-18T02:00:05.290 回答
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];
于 2012-09-18T08:06:46.020 回答