Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何删除精灵并再次使用相同的精灵_C++_Ios_Xcode_Cocos2d X - Fatal编程技术网

C++ 如何删除精灵并再次使用相同的精灵

C++ 如何删除精灵并再次使用相同的精灵,c++,ios,xcode,cocos2d-x,C++,Ios,Xcode,Cocos2d X,我是初学者,目前我正在使用cocos2d-x2.2.3开发一款类似于2048的x-code游戏。在我的游戏中,我必须碰撞两个精灵。碰撞时,我必须移除两个Spit并在同一位置添加一个新精灵。我使用以下代码: if(_player1->boundingBox().intersectsRect(_player2->boundingBox())) { this->removeChild(_player1, true);//it is not removing properly this

我是初学者,目前我正在使用cocos2d-x2.2.3开发一款类似于2048的x-code游戏。在我的游戏中,我必须碰撞两个精灵。碰撞时,我必须移除两个Spit并在同一位置添加一个新精灵。我使用以下代码:

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
}

如果您的精灵没有被移除,请提前感谢,可能是因为移除后它的引用计数不是0。
检查您是否保留了它或将其添加到某个容器中

首先尝试使用带有“false”的removeChild,如果不起作用,您可以使用:

_player1->removeFromParent();
但我建议更改精灵纹理(对于玩家1)并更改位置

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

}