参考网友微信打飞机demo实现炸弹图标和数量更新时遇到的问题解决心得

        博客专栏地址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 需要更新精灵的位置,如果每次都需要重新创建一次,那么需要在创建之前最好删除掉已存在的精灵

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值