I am just trying to add a background, how come this is not working? Instead it is shifted slightly lower then it should be in the scene.
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
width = visibleSize.width;
height = visibleSize.height;
auto background = Sprite::create("HelloWorld.png");
background->setPosition(Vec2(width / 2.0, height / 2.0));
scaleSpriteTo(background, width, height);
background->setAnchorPoint(Vec2(0.5, 0.5));
this->addChild(background);
void scaleSpriteTo(Sprite *it, float xscale, float yscale)
{
it->setScale(xscale / it->getContentSize().width, yscale / it->getContentSize().height);
}
Try to run:
auto bg = Sprite::create("HelloWorld.png");
bg->setScale(getContentSize().width / bg->getContentSize().width, getContentSize().height / bg->getContentSize().height);
bg->setPosition(getContentSize() / 2);
addChild(bg);
Note that the anchor point is (0.5, 0.5) by default, so no need setting that in your case.
@grueth
Well this is very odd! Doing just
auto bg = Sprite::create("HelloWorld.png");
bg->setScale(getContentSize().width / bg->getContentSize().width, getContentSize().height / bg->getContentSize().height);
Made this
Doing
auto bg = Sprite::create(“HelloWorld.png”);
bg->setScale(getContentSize().width / bg->getContentSize().width, getContentSize().height / bg->getContentSize().height);
bg->setPosition(getContentSize() / 2);
Made this
Neither of these are correct! What on earth is going on?
Could you post the code of this layer?