qq_30823901 2016-02-25 14:29 采纳率: 0%
浏览 2008

cocos2d-x游戏场景精灵无法显示 ,跪求大神帮解决

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));
}

  • 写回答

1条回答

  • threenewbee 2016-03-06 22:02
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题