【问题标题】:Cocos2d-x : some sprites become invisible when a PhysicsWorld is setCocos2d-x : 当设置 PhysicsWorld 时,一些精灵变得不可见
【发布时间】:2015-06-29 10:42:33
【问题描述】:

我目前正在尝试使用 cocos2d-x 引擎制作游戏。正如我想象的那样,游戏需要滚动背景。然后我所做的是在一个表格中创建 2 个背景精灵:

bool HelloWorld::init()
{
    ///////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    //creating "first" background 
    background[0] = Sprite::create("fond.png");
    background[0]->setPosition(0, 0);
    background[0]->setAnchorPoint(Vec2(0, 0));
    this->addChild(background[0]);

    //creating "second" background
    background[1] = Sprite::create("fond.png");
    background[1]->setPosition(background[0]->getContentSize().width, 0);
    background[1]->setAnchorPoint(Vec2(0, 0));
    this->addChild(background[1]);

    this->scheduleUpdate();
    return true;
}

然后我让它们在更新功能中以这种方式滚动:

void HelloWorld::update(float delta)
{
    for(int i = 0 ; i < 2 ; i++) { //scrolling background
        if (background[i]->getPositionX() <= 0) {
            background[i]->setPositionX(background[i]->getPositionX()-800*delta);
            background[1-i]->setPositionX(background[1-i]->getContentSize().width+(background[i]->getPositionX()));
        }
    }
}

所以,现在,我有了基本的 init() 和 update() 函数,这很棒:)。现在我需要执行我的 createScene() 函数,起初看起来像这样:

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

然后它就完美运行了!但遗憾的是,对于我的游戏的其余部分,我需要物理(你知道,碰撞和其他东西),所以我需要将此函数更改为至少:

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::createWithPhysics();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

问题来了:当我添加 PhysicsWorld(就像那里一样)时,background[1] 变得不可见,就像当他必须出现在屏幕上时,我有一个漂亮的黑屏直到背景[0] 来了。 我感觉几乎尝试了所有方法,但找不到任何解决方案……有人知道我应该怎么做吗?

P.S:我目前在Linux平台上做了所有的测试,我有cocos2d-x 3.4版本。

【问题讨论】:

    标签: c++ cocos2d-x game-physics cocos2d-x-3.0


    【解决方案1】:

    我不确定您是否在项目旁边使用 SpriteBuilder 或 Cocos Studio,但在与 Cocos2d 一起使用的多个 GUI 中,我看到当您设置一些精灵相对于屏幕大小的位置而不是使用点或 UI 点,一旦你在代码中移动精灵,它通常会产生很多错误。 尝试从加载的那一刻开始在代码中设置相对位置,或者不要使用相对初始定位来在您使用的 GUI 中移动精灵。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多