如何在序列中更改ZOrder?

时间:2014-01-16 05:17:54

标签: cocos2d-iphone ccsprite z-order

我想知道如何在CCSequence中更改CCSprite的ZOrder。我已尝试在另一个线程中建议使用CCCallBlock,但它已经删除并停止同一类移动的其他精灵。还有其他方法可以建议吗?

crystalEntryPoint = [self position];
float xD = [crystal position].x - crystalEntryPoint.x;
float yD = [crystal position].y - crystalEntryPoint.y;
CGPoint dest = ccp(crystalEntryPoint.x + (xD * 2),crystalEntryPoint.y + (yD * 2));
float easingRate = 2;

CCMoveTo *moveTo = [CCMoveTo actionWithDuration:1 position:dest];
CCMoveTo *moveBack = [CCMoveTo actionWithDuration:1 position:crystalEntryPoint];
CCEaseInOut *easeTo = [CCEaseInOut actionWithAction:moveTo rate:easingRate];
CCEaseInOut *easeBack = [CCEaseInOut actionWithAction:moveBack rate:easingRate];
CCCallBlock *zOrderBehind = [CCCallBlock actionWithBlock:^{ [self setZOrder:kManaSpriteBehindCrystalZValue]; }];
CCCallBlock *zOrderInFront = [CCCallBlock actionWithBlock:^{ [self setZOrder:kManaSpriteZValue]; }];

CCSequence *seq = [CCSequence actions:easeTo,zOrderBehind,easeBack,zOrderInFront,nil]; //,zOrderInfront
CCRepeatForever *rep = [CCRepeatForever actionWithAction:seq];

[self runAction:rep];

1 个答案:

答案 0 :(得分:0)

尝试CCCallBlockN,它为块提供运行操作的节点作为参数。当前设置在块内部保留self,这可能导致节点以后不再释放,因为序列永远运行,因此在节点解除分配之前保持强大的自引用 - 我不能说cocos2d是否能够正确地清理它,但它可能不会。

不确定这是否与您的问题有关,但在我看来,可能的解释是考虑到代码段中的其他内容看起来不错。

CCCallBlockN *zOrderBehind = [CCCallBlockN actionWithBlock:^(CCNode* node){ 
    [node setZOrder:kManaSpriteBehindCrystalZValue]; 
}];
CCCallBlockN *zOrderInFront = [CCCallBlockN actionWithBlock:^(CCNode* node){ 
    [node setZOrder:kManaSpriteZValue]; 
}];

如果这不起作用,请尝试CCActionTween,允许按名称更改任何属性。

id zOrderChange = [CCActionTween actionWithDuration:0.0 
                                                key:"zOrder" 
                                               from:kManaSpriteZValue 
                                                 to:kManaSpriteZValue];

我不确定具有相同值的from / to是否有效。如果它不尝试使用0作为from参数,甚至可能会将duration增加到某个低值,例如0.01。