Cocos2d-x:当设置PhysicsWorld时,一些精灵变得不可见

问题描述 投票:2回答:1

我目前正在尝试使用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(就像那里)时,背景[1]变得不可见,就像他必须出现在屏幕上一样,我有一个漂亮的黑屏,直到背景[0]出现。我有尝试几乎所有的东西,但我找不到任何解决方案......有人知道我应该做什么吗?

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

c++ cocos2d-x game-physics cocos2d-x-3.0
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.