我目前正在嘗試使用cocos2d-x引擎進行遊戲。遊戲,就像我想象的那樣,需要一個滾動背景。我做了什麼,然後在一個表中創建2個背景精靈:Cocos2d-x:設置PhysicsWorld時,一些精靈變得不可見
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版本。