cocos2d-x - cocos2dx中精灵换图的问题
本文介绍了cocos2d-x - cocos2dx中精灵换图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
问题已经解决了。onEnter被调用了两次,每当scene切换,都会触发onEnter()。
可以在源码DIrector与Node中看到。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
想要做的是一个数字类,
这样一张图片实现游戏的分数的变化。
void NumberBase::onEnter()
{
//得到每个数字的宽高
Node::onEnter();
Sprite* numberSprite = Sprite::create("numwhite-hd.png");
this->numWidth = numberSprite->getContentSize().width / 12;
this->numHeight = numberSprite->getContentSize().height;
//设置精灵显示数字1
numOne = Sprite::create("numwhite-hd.png");
numOne->setTextureRect(Rect(numWidth * 2,0,numWidth,numHeight));
numOne->setPosition(Point(0,0));
this->addChild(numOne);
}
void NumberBase::setNumber()
{
//设置精灵显示数字2
this->numOne->setTextureRect(Rect(numWidth * 3,0,numWidth,numHeight));
}
场景类中的init()函数中:
auto numTemp = new NumberBase();
numTemp->onEnter();
numTemp->setPosition(Point(visibleSize.width / 2,visibleSize.height / 2));
this->addChild(numTemp,4);
numTemp->setNumber();
预想的结果应该是数字1替换数字0,结果数字1出现在0下方,0并没有被替换。
解决方案
你的实现没有问题,你看看是不是别的地方贴了一个零
这篇关于cocos2d-x - cocos2dx中精灵换图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文