Reputation: 43
I am beginner's,currently i am developing a game like 2048 in x-code using cocos2d-x2.2.3.In my game i have to collide two sprites.While colliding, i have to remove the two spites and add a new sprite in same location.I am using below code:
if(_player1->boundingBox().intersectsRect(_player2->boundingBox()))
{
this->removeChild(_player1, true);//it is not removing properly
this->removeChild(_player2, true);
_player1 = new CCSprite();
_player1->initWithFile("2.png");
_player1->setPosition(ccp(position.x,position.y));
this->addChild(_player1);//I have to add same player again
}
Thanks in advance
Upvotes: 0
Views: 479
Reputation: 206
if(_player1->boundingBox().intersectsRect(_player2->boundingBox()))
{
this->removeChild(_player2, true);
if(_player1)
{
this->removeChild(_player1, true);
_player1 = new CCSprite();
_player1->initWithFile("2.png");
_player1->setPosition(ccp(position.x,position.y));
this->addChild(_player1);
}
}
Upvotes: 1
Reputation: 79
First at all try to use removeChild with "false" If that don't work you can use:
_player1->removeFromParent();
But I recommend change the sprite texture (for player 1) and change the position.
Upvotes: 0
Reputation: 1823
if your sprite is not removed, possibly because it's reference count is not 0 after removed. check if you retained it or added it into some container
Upvotes: 0