C++ Cocos2d-x:设置物理世界时,某些精灵将不可见
我目前正在尝试用cocos2d-x引擎制作一个游戏。正如我想象的那样,这个游戏需要一个滚动的背景。然后我在一个表中创建了两个背景精灵:C++ Cocos2d-x:设置物理世界时,某些精灵将不可见,c++,cocos2d-x,game-physics,cocos2d-x-3.0,C++,Cocos2d X,Game Physics,Cocos2d X 3.0,我目前正在尝试用cocos2d-x引擎制作一个游戏。正如我想象的那样,这个游戏需要一个滚动的背景。然后我在一个表中创建了两个背景精灵: bool HelloWorld::init() { /////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } //creating "first" background b
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()));
}
}
}
然后它就完美地工作了!但遗憾的是,在我的游戏剩下的时间里,我需要物理(你知道,碰撞等等),所以我需要将这个函数至少改为:
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]出现。
我感觉几乎什么都试过了,但我找不到任何解决办法。。。有人知道我该怎么做吗
注:目前我在Linux平台上做了所有测试,我有cocos2d-x 3.4版本。我不确定您是否在项目中使用SpriteBuilder或Cocos Studio,但是在Cocos2d旁边使用的不止一个GUI中,我看到当你设置一些精灵相对于屏幕大小的位置而不是使用点或UI点时,一旦你在代码中移动精灵,通常会产生很多bug。 尝试从加载精灵的那一刻起在代码中设置相对位置,或者不要在正在使用的GUI中使用相对初始位置来移动精灵
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;
}