简体   繁体   English

Cocos2d-将CCSprite传递给子层

[英]Cocos2d - Passing a CCSprite to a child layer

I have a scene, a main layer and a child layer, the main layer contains a Player* object which is a subclass of CCSprite, I want to pass the Player into the child layer like so: 我有一个场景,一个主层和一个子层,主层包含一个Player *对象,该对象是CCSprite的子类,我想将Player传递给子层,如下所示:

BattleLayer* b = [[BattleLayer alloc] initWithPlayer:_player];
[((CCScene*)self.parent) addChild:b];

-(id)initWithPlayer:(Player *)p
{
    if((self=[super init]))
    {
        _player = p;
        [self addChild:_player.spriteSheet];
    }
    return self;
}

However, this gives an error when I do [self addChild:_player.spriteSheet]; 但是,这在我执行[self addChild:_player.spriteSheet]时出现错误;

*** Assertion failure in -[BattleLayer addChild:z:tag:]

I've tried removing the sprite from the main layer before passing it in, but even with cleanup:NO this gives an error of 我尝试过在传入主层之前将其从主层中删除,但即使使用cleanup:NO,这也会产生错误

*** -[CCSpriteBatchNode tag]: message sent to deallocated instance 0x747a580

What am I doing wrong here? 我在这里做错了什么? And what's the best way of achieving this? 而实现这一目标的最佳方法是什么?

I can see one of three things causing an addSprite assertion failure. 我可以看到导致addSprite断言失败的三件事之一。

  • p is nil p为零
  • p.spriteSheet is nil p.spriteSheet为零
  • or finally, p.spriteSheet already has a parent (ie spriteSheet has already been added to a CCNode beforehand) 或最后,p.spriteSheet已经有一个父对象(即spriteSheet已经预先添加到CCNode中)

I've run in to this before and retained and then released after reordering. 我之前曾碰到过这个问题,保留并在重新排序后发布。 My objects were autoreleased and retained only by the CCLayer so this worked, Try: 我的对象仅由CCLayer自动释放和保留,因此可以正常工作,请尝试:

[_player retain];
[_player removeFromParentAndCleanUp:YES];
[self addChild:_player];
[_player release];

However I have run into a bug with rapid removal and re-adding of sprites and it is described here: http://www.cocos2d-iphone.org/forum/topic/9980 但是,我遇到了快速删除和重新添加精灵的错误,并在此处进行了描述: http : //www.cocos2d-iphone.org/forum/topic/9980

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM