I'm converting my android game, previously written in Java to cocos2d-x. In this game I need to spawn walls at a few random positions, so I call setPosition on my sprite in my init method but the position on the x-axis isn't changed (they are changed on the y-axis though Oo).
The position does change when I call it on onTouchBegun() and change it there, it just doesn't work on that initial call. I can't find any related problem on google and I'm all out of ideas.
I'll add the code I use to spawn the walls down below:
void WallPair::init(cocos2d::Layer *scene, float height, float width, float gapWidth, Vec2 position){
this->position = position;
this->height = height;
this->scene = scene;
this->gapWidth = gapWidth;
//Pointtrigger (obv)
pointWall = Sprite::create();
pointWall->setScale(gapWidth, height);
pointWall->setPosition(position.x, position.y);
auto pointTrigger = PhysicsBody::createBox(Size(gapWidth, height));
pointTrigger->setDynamic(false);
pointTrigger->setGravityEnable(false);
pointTrigger->setCategoryBitmask((int)PhysicsCategory::POINT);
pointTrigger->setCollisionBitmask((int)PhysicsCategory::NONE);
pointTrigger->setContactTestBitmask((int)PhysicsCategory::PLAYER);
pointWall->setPhysicsBody(pointTrigger);
scene->addChild(pointWall, 2);
leftWall = Sprite::create(texture);
leftWall->setScale(width/leftWall->getContentSize().width, height/leftWall->getContentSize().height);
Size wallSize = Size(leftWall->getContentSize().width * leftWall->getScaleX(), leftWall->getContentSize().height * leftWall->getScaleY());
std::cout << "POSRES: " << (position.x - gapWidth - wallSize.width) << " ::: LEFT;" << std::endl;
std::cout << "LPOS: " << leftWall->getPosition().x << ", LSIZE: " << wallSize.width << std::endl;
std::cout << "SCALE: " << leftWall->getScaleX() << ", " << leftWall->getScaleY() << std::endl;
// std::cout << "contentSizeLW: " << leftWall->getContentSize().width << " pos: " << (position.x - gapWidth - leftWall->getContentSize().width/2) <<", SCALE: " << leftWall->getScaleY()<<std::endl;
//leftWall->setContentSize(Size(leftWall->getContentSize().width, height));
scene->addChild(leftWall, 2);
auto pbl = PhysicsBody::createBox(wallSize, PhysicsMaterial(0.1f, 1.0f, 1.0f));
pbl->setDynamic(false);
pbl->setGravityEnable(false);
pbl->setCategoryBitmask((int)PhysicsCategory::WALL);
pbl->setCollisionBitmask((int)PhysicsCategory::PLAYER);
pbl->setContactTestBitmask((int)PhysicsCategory::PLAYER);
leftWall->setPhysicsBody(pbl);
leftWall->setPosition(position.x - gapWidth - wallSize.width, position.y);
rightWall = Sprite::create(texture);
rightWall->setScale(width/rightWall->getContentSize().width, height/rightWall->getContentSize().height);
wallSize = Size(rightWall->getContentSize().width * rightWall->getScaleX(), rightWall->getContentSize().height * rightWall->getScaleY());
std::cout << "POSRES: " << (position.x + gapWidth + wallSize.width) << ":::RIGHT;" << std::endl;
//rightWall->setContentSize(Size(rightWall->getContentSize().width * rightWall->getScaleX(), height));
std::cout << "RPOS: " << rightWall->getPosition().x << std::endl;
//rightWall->setContentSize(Size(rightWall->getContentSize().width, height));
// std::cout << "contentSizeRW: " << rightWall->getContentSize().width << " pos: " << (position.x + gapWidth + rightWall->getContentSize().width/2) << ", SCALE: " << rightWall->getScaleY() <<std::endl;
scene->addChild(rightWall, 2);
pbl = PhysicsBody::createBox(wallSize, PhysicsMaterial(0.1f, 1.0f, 1.0f));
pbl->setDynamic(false);
pbl->setGravityEnable(false);
pbl->setCategoryBitmask((int)PhysicsCategory::WALL);
pbl->setCollisionBitmask((int)PhysicsCategory::PLAYER);
pbl->setContactTestBitmask((int)PhysicsCategory::PLAYER);
rightWall->setPhysicsBody(pbl);
rightWall->setPosition(position.x + gapWidth + wallSize.width, position.y);
}
The WallPair's onTouch method:
void touch(){
leftWall->setPosition(leftWall->getPositionX() - 50.0f, leftWall->getPositionY());
rightWall->setPosition(rightWall->getPositionX() + 50.0f, rightWall->getPositionY());
}
EDIT:
Just to clarify:
The problem is not the touch method. That works correctly. The problem is WallPair::init(). It puts the sprites at an xpos of 0 even though getPosition tells me they should be all over the place (ranging from -500 to +1500). But they still are on 0.
Thank you in advance!
So after a lot of time Googling, debuging, testing and coding I found the problem. If I use a Transition when I do replaceScene I am not able to manually set the position of sprites with physicsbodies, but I'm able to do so without transition.
All I did was change the replaceScene code:
from:
director->replaceScene(TransitionFlipX::create(2, scene));
to:
director->replaceScene(scene);
And it now works!