【发布时间】: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