博客专栏地址http://blog.csdn.net/column/details/jackyairplane.html
非常感谢博主的无私奉献,在制作这个打飞机游戏过程中炸弹图标在炸弹数量减到0 的时候图标居然没消失,调试了几次,终于了找到问题了。出问题的代码片段
//update the bigBomb
void GameLayer::updateBigItem(int itemCount)
{
CCSprite *pNormalBomb = CCSprite::createWithSpriteFrameName("bomb.png");
CCSprite *pPressBomb = CCSprite::createWithSpriteFrameName("bomb.png");
if(itemCount < 0)
{
return;
}else if(0 == itemCount)
{
if(this->getChildByTag(TAG_BIGBOMB_ITEM))
{
this->removeChildByTag(TAG_BIGBOMB_ITEM);
}
if (this->getChildByTag(TAG_BIGBOMB_LABEL))
{
this->removeChildByTag(TAG_BIGBOMB_LABEL);
}
}else if (itemCount == 1)
{
CCMenuItemImage *pBigBombMenuItem = CCMenuItemImage::create();
pBigBombMenuItem->initWithNormalSprite(pNormalBomb, pPressBomb, NULL, this, menu_selector(GameLayer::bigBombMenuCallBack));
pBigBombMenuItem->setAnchorPoint(ccp(1.0f, 0.5f));
pBigBombMenuItem->setPosition(ccp(pNormalBomb->getContentSize().width + 10, pNormalBomb->getContentSize().height / 2 + 10));
CCMenu *pBigBombMenu = CCMenu::createWithItem(pBigBombMenuItem);
pBigBombMenu->setPosition(CCPointZero);
this->addChild(pBigBombMenu, 1, TAG_BIGBOMB_ITEM);
if (this->getChildByTag(TAG_BIGBOMB_LABEL))
{
//delete the count label
this->removeChildByTag(TAG_BIGBOMB_LABEL);
}
}else
{
CCMenuItemImage *pBigBombMenuItem = CCMenuItemImage::create();
pBigBombMenuItem->initWithNormalSprite(pNormalBomb, pPressBomb, NULL, this, menu_selector(GameLayer::bigBombMenuCallBack));
pBigBombMenuItem->setAnchorPoint(ccp(1.0f, 0.5f));
pBigBombMenuItem->setPosition(ccp(pNormalBomb->getContentSize().width + 10, pNormalBomb->getContentSize().height / 2 + 10));
CCMenu *pBigBombMenu = CCMenu::createWithItem(pBigBombMenuItem);
pBigBombMenu->setPosition(CCPointZero);
this->addChild(pBigBombMenu, 0, TAG_BIGBOMB_ITEM);
if (this->getChildByTag(TAG_BIGBOMB_LABEL))
{
//delete the count label
this->removeChildByTag(TAG_BIGBOMB_LABEL);
}
if (itemCount >= 0 && itemCount <= MAX_BIGBOOM)
{
CCString *pBigBombCountStr =CCString::createWithFormat("X%d", itemCount);
CCLabelBMFont *pBigBombCountLabel = CCLabelBMFont::create(pBigBombCountStr->getCString(), "font/font.fnt");
pBigBombCountLabel->setAnchorPoint(ccp(0.0, 0.5f));
pBigBombCountLabel->setColor(ccc3(143, 146, 147));
pBigBombCountLabel->setPosition(ccp(pBigBombMenuItem->getPositionX() + 5, pBigBombMenuItem->getPositionY()));
this->addChild(pBigBombCountLabel, 0, TAG_BIGBOMB_LABEL);
}
}
}
游戏在运行后炸弹图标一直不能remove,原来是每次更新的时候都画了一次图标,那么所有的图标都叠起来了,一层一层的,点了多少次炸弹就有多少个图标精灵。所以在每次增加炸弹图标精灵和label之前都需要删除一次原有精灵,这样就保证了每次是先删除然后在绘制,总的精灵数就不变了。所以修改后源码就是在每次增加pBigBombMenu之前调用
if(this->getChildByTag(TAG_BIGBOMB_ITEM))
{
this->removeChildByTag(TAG_BIGBOMB_ITEM);
}
修改后源码为
//update the bigBomb
void GameLayer::updateBigItem(int itemCount)
{
CCSprite *pNormalBomb = CCSprite::createWithSpriteFrameName("bomb.png");
CCSprite *pPressBomb = CCSprite::createWithSpriteFrameName("bomb.png");
if(itemCount < 0)
{
return;
}else if(0 == itemCount)
{
if(this->getChildByTag(TAG_BIGBOMB_ITEM))
{
this->removeChildByTag(TAG_BIGBOMB_ITEM);
}
if (this->getChildByTag(TAG_BIGBOMB_LABEL))
{
this->removeChildByTag(TAG_BIGBOMB_LABEL);
}
}else if (itemCount == 1)
{
//we must remove the bigBombMenu at first
if(this->getChildByTag(TAG_BIGBOMB_ITEM))
{
this->removeChildByTag(TAG_BIGBOMB_ITEM);
}
CCMenuItemImage *pBigBombMenuItem = CCMenuItemImage::create();
pBigBombMenuItem->initWithNormalSprite(pNormalBomb, pPressBomb, NULL, this, menu_selector(GameLayer::bigBombMenuCallBack));
pBigBombMenuItem->setAnchorPoint(ccp(1.0f, 0.5f));
pBigBombMenuItem->setPosition(ccp(pNormalBomb->getContentSize().width + 10, pNormalBomb->getContentSize().height / 2 + 10));
CCMenu *pBigBombMenu = CCMenu::createWithItem(pBigBombMenuItem);
pBigBombMenu->setPosition(CCPointZero);
this->addChild(pBigBombMenu, 1, TAG_BIGBOMB_ITEM);
if (this->getChildByTag(TAG_BIGBOMB_LABEL))
{
//delete the count label
this->removeChildByTag(TAG_BIGBOMB_LABEL);
}
}else
{
//we must remove the bigBombMenu at first
if(this->getChildByTag(TAG_BIGBOMB_ITEM))
{
this->removeChildByTag(TAG_BIGBOMB_ITEM);
}
CCMenuItemImage *pBigBombMenuItem = CCMenuItemImage::create();
pBigBombMenuItem->initWithNormalSprite(pNormalBomb, pPressBomb, NULL, this, menu_selector(GameLayer::bigBombMenuCallBack));
pBigBombMenuItem->setAnchorPoint(ccp(1.0f, 0.5f));
pBigBombMenuItem->setPosition(ccp(pNormalBomb->getContentSize().width + 10, pNormalBomb->getContentSize().height / 2 + 10));
CCMenu *pBigBombMenu = CCMenu::createWithItem(pBigBombMenuItem);
pBigBombMenu->setPosition(CCPointZero);
this->addChild(pBigBombMenu, 0, TAG_BIGBOMB_ITEM);
if (this->getChildByTag(TAG_BIGBOMB_LABEL))
{
//delete the count label
this->removeChildByTag(TAG_BIGBOMB_LABEL);
}
if (itemCount >= 0 && itemCount <= MAX_BIGBOOM)
{
CCString *pBigBombCountStr =CCString::createWithFormat("X%d", itemCount);
CCLabelBMFont *pBigBombCountLabel = CCLabelBMFont::create(pBigBombCountStr->getCString(), "font/font.fnt");
pBigBombCountLabel->setAnchorPoint(ccp(0.0, 0.5f));
pBigBombCountLabel->setColor(ccc3(143, 146, 147));
pBigBombCountLabel->setPosition(ccp(pBigBombMenuItem->getPositionX() + 5, pBigBombMenuItem->getPositionY()));
this->addChild(pBigBombCountLabel, 0, TAG_BIGBOMB_LABEL);
}
}
}
总结:在cocos2d-x 需要更新精灵的位置,如果每次都需要重新创建一次,那么需要在创建之前最好删除掉已存在的精灵