我是cocos2d-x的新手。我正在使用cocos2d-x在xcode中开发一款游戏。 在我的游戏中,我有精灵动画(人)和移动障碍物和硬币。我与精灵动画&硬币相撞。当我获得10个硬币时,我将添加一个生命(为生活添加一个精灵)。我的问题是当精灵动画(人)&障碍之间发生碰撞,生活应该减少(我的意思是生活精灵应该删除),但它不会被删除。 我正在使用以下代码。在cocos2d-x中删除一个精灵
if(coincount%10==0)
{
lifecount=lifecount+1;
}
if(lifecount==1)
{
life = CCSprite::create("life.png");
life->setPosition(ccp(winwsize/2, winhsize/1.08));
this->addChild(life, 5);
}
else if(lifecount==2)
{
life1 = CCSprite::create("life.png");
life1->setPosition(ccp(winwsize/1.8, winhsize/1.08));
this->addChild(life1, 5);
}
else if (lifecount==3)
{
life2 = CCSprite::create("life.png");
life2->setPosition(ccp(winwsize/1.6, winhsize/1.08));
this->addChild(life2, 5);
}
if (manRect.intersectsRect(obs5Rect))
{
if(lifecount>=1)
{
lifecount=lifecount-1;
this->scheduleOnce(schedule_selector(PlayScene::remove),0.5f);
}
void PlayScene::remove()
{
if(lifecount==0)
{
this->removeChild(life, true);
}
else if(lifecount==1)
{
this->removeChild(life1, true);
}
else if(lifecount==2)
{
this->removeChild(life2, true);
}
但是,当障碍物与精灵动画(人)碰撞时,精灵不移除。 请任何人都可以帮助我找到解决方案。谢谢。
我编辑我的问题。在调用remove函数之前,我减少了lifecount = lifecount-1。 – arunkumar
那么,这是否意味着这个建议不能解决问题?你也可以尝试调用'life-> removeFromParentAndCleanup(true)'而不是'this-> removeChild(...)' – Losiowaty