cocos2d-x游戏场景精灵无法显示 ,跪求大神帮解决
问题如下:
如果直接在AppDelegate.cpp中执行:
auto scene = GameLayer::createScene();
// run
director->runWithScene(scene);
则游戏主场景
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("panda.plist");
//添加精灵
this->panda = Panda::createWithSpriteFrameName("panda.png");
this->panda->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(this->panda, 10, GameSceneNodeTagFighter);
精灵可以正常显示,
但是如果AppDelegate.cpp里面是先创建HelloWorldScene,
再在HelloWorldScene中通过一个菜单回调创建GameScene时,
auto scene = HelloWorld::createScene();
director->runWithScene(scene);
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Scene* tsc = TransitionFade::create(1.0f, GameLayer::createScene());
if (tsc)
Director::getInstance()->pushScene(tsc);
// Director::getInstance()->end();
}
游戏场景就无法显示精灵,什么都没有。
求各大神帮忙如何解决,我是新手来着;
我在想是不是引擎本身的优化设计问题,对于暂时用不到的精灵对象引擎会不会自动延迟创建,直到有事件响应关联到精灵对象时才会真正开始创建???
Panda.cpp代码如下:
#include "Panda.h"
USING_NS_CC;
Panda* Panda::createWithSpriteFrameName(const char* spriteFrameName)
{
Panda *panda = new Panda();
if (panda && panda->initWithSpriteFrameName(spriteFrameName)) {
panda->autorelease();
auto body = PhysicsBody::createBox(panda->getContentSize() - Size(3, 3));
body->setCategoryBitmask(0x01); //0001
body->setCollisionBitmask(0x02); //0010
body->setContactTestBitmask(0x01);
panda->setPhysicsBody(body);
return panda;
}
CC_SAFE_DELETE(panda);
return NULL;
}
void Panda::setPosition(const cocos2d::Vec2& newPosition)
{
Size screenSize = Director::getInstance()->getVisibleSize();
float halfWidth = this->getContentSize().width / 2;
float halfHeight = this->getContentSize().height / 2;
float pos_x = newPosition.x;
float pos_y = newPosition.y;
if (pos_x < halfWidth) {
pos_x = halfWidth;
}
else if (pos_x >(screenSize.width - halfWidth)) {
pos_x = screenSize.width - halfWidth;
}
if (pos_y < halfHeight) {
pos_y = halfHeight;
}
else if (pos_y >(screenSize.height - halfHeight)) {
pos_y = screenSize.height - halfHeight;
}
Sprite::setPosition(Vec2(pos_x, pos_y));
Sprite::setAnchorPoint(Vec2(0.5f, 0.5f));
}