如何将CCSprite从一个父母转移到另一个? [英] How to transfer a CCSprite from one parent to another?

查看:189
本文介绍了如何将CCSprite从一个父母转移到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CCSprite 调用 sprite CCLayer 调用 movingLayer ,它本身是当前 CCLayer 运行我的游戏逻辑的子 self 在这种情况下。 moveLayer 正在整个屏幕上来回移动,重复一次永远的动作, sprite 正在骑着它。我想获得 sprite 以下车 moveLayer 和get on到



当我尝试这样做时,我需要告诉 moveLayer 删除 sprite self 添加 sprite。 / code>这是我做的...

   - (void)攻击: $ b {
[sprite stopAllActions];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
CGFloat distance = ccpDistance(sprite.position,ccp(sprite.position.x,-sprite.contentSize.height / 2));
id goDown = [CCMoveTo actionWithDuration:distance / moveDuration position:ccp(sprite.position.x,-sprite.contentSize.height / 2)];
id repeatDown = [CCRepeatForever actionWithAction:[CCequence actions:[CCMoveTo actionWithDuration:0 position:ccp(sprite.position.x,sprite.contentSize.height / 2 + self.contentSize.height)],[CCMoveTo actionWithDuration: moveDuration position:ccp(sprite.position.x,-sprite.contentSize.height / 2)],nil]];
id attackAction = [CCSequence actions:goDown,repeatDown,nil];
[sprite runAction:attackAction];
}

我认为 stopAllActions 是多余的,但这不是问题。如果我将 sprite moveLayer 中移除,然后将其添加到 self 我碰到访问僵尸,如果我尝试添加到 self 首先,我试图添加一些已经添加的东西。



或者,尝试保留sprite,然后再从中删除它。 moveLayer,然后在完成后释放它:

  [sprite retain]; 
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
[sprite release];


I have a CCSprite called sprite that is a child of a CCLayer called movingLayer that is itself a child of the current CCLayer running my game logic, so it is self in this case. movingLayer is moving back and forth across the screen in a repeat forever action and sprite is riding along on it. I want to get sprite to "get off" of movingLayer and "get on" to self where it can do some actions of its own.

When I try to do this, I need to tell movingLayer to remove sprite and self to add sprite. This is what I did...

- (void)attack:(id)sender
{
    [sprite stopAllActions];
    [movingLayer removeChild:sprite cleanup:YES];
    [self addChild:sprite z:1];
    CGFloat distance = ccpDistance(sprite.position, ccp(sprite.position.x, -sprite.contentSize.height/2));
    id goDown = [CCMoveTo actionWithDuration:distance/moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)];
    id repeatDown = [CCRepeatForever actionWithAction:[CCSequence actions:[CCMoveTo actionWithDuration:0 position:ccp(sprite.position.x, sprite.contentSize.height/2+self.contentSize.height)], [CCMoveTo actionWithDuration:moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)], nil]];
    id attackAction = [CCSequence actions:goDown, repeatDown, nil];
    [sprite runAction:attackAction];
}

I think stopAllActions is redundant, but that's not the problem. If I remove sprite from movingLayer before adding it to self I crash for accessing a zombie, and if I try to add it to self first, I crash for trying to add something already added.

解决方案

Have you tried setting cleanup to NO?

Alternatively, try to retain sprite before you remove it from movingLayer, then release it when you're done with it:

[sprite retain];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
[sprite release];

这篇关于如何将CCSprite从一个父母转移到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆