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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样