Cocos2d-x : some sprites become invisible when a PhysicsWorld is set

Datetime:2016-08-23 04:30:47          Topic: Cocos2d-X  IOS Development           Share

So first, hello to everybody :).

I’m currently trying to make a game with cocos2d-x engine. The game, as I imagine it, needs a scrolling background. What I did then is creating 2 background sprites in a table:

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;
}

I then made them scroll in that way, in an update function:

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()));
		}
	}
}

So, now, I have my basic init() and update() functions, which is great :). Now I need to do my createScene() function which, at first, looks like this:

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;
}

And then it works perfectly! But sadly, for the rest of my game I need physics (you know, collisions and stuff), so I need to change this function to at least this:

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;
}

And there comes the problem: when I add PhysicsWorld (like there), background[1] becomes invisible , like when he has to appear on the screen, I have a beautiful black screen until background[0] comes.

I have the feeling to have tried almost everything but I can’t find any solution… Do somebody have an idea of what I should do?

P.S: I did all my tests on the Linux platform for the moment and I have cocos2d-x 3.4 version.





About List