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

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

问题描述

我有一个名为 spriteCCSprite,它是一个名为 movingLayerCCLayer 的子级,它本身就是一个当前运行我的游戏逻辑的 CCLayer 的子级,所以在这种情况下它是 self.movingLayer 在屏幕上来回移动,这是一个永远重复的动作,而 sprite 正骑在上面.我想让 sprite 下车" movingLayer 并上车"到 self ,它可以自己做一些动作.

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.

当我尝试这样做时,我需要告诉 movingLayer 删除 spriteself 添加 sprite.这就是我所做的......

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];
}

我认为 stopAllActions 是多余的,但这不是问题.如果我在将 movingLayer 添加到 self 之前将 spritemovingLayer 中删除,我会因访问僵尸而崩溃,并且如果我尝试将其添加到 self 首先,我因为尝试添加已经添加的内容而崩溃.

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.

推荐答案

您是否尝试将清理设置为 NO?

Have you tried setting cleanup to NO?

或者,在将精灵从movingLayer中移除之前尝试保留它,然后在完成后释放它:

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天全站免登陆