Scaling sprite results as rotated image

Hii, hope you all are fine.
I am creating an Android project and using an image for the background and scaling it to the size of the screen, but it results as 180 degree rotated image.
here is my code…

    visibleSize = Director::getInstance()->getVisibleSize();
    vsWidth = visibleSize.width;
    vsHeight = visibleSize.height;

    auto backgroundRoad = Sprite::create("background.png");
    auto brWidth = backgroundRoad->getContentSize().width;
    auto brHeight = backgroundRoad->getContentSize().height;
    backgroundRoad->setScale(vsWidth/brWidth, vsHeight/brHeight);
    backgroundRoad->setPosition(Point(vsWidth/2, vsHeight/2));

    auto renderTextureForRoad = RenderTexture::create( vsWidth, vsHeight, PixelFormat::RGBA8888 );
    renderTextureForRoad->begin();
    backgroundRoad->visit();
    renderTextureForRoad->end();

    road = Sprite::createWithTexture(renderTextureForRoad->getSprite()->getTexture());
    road->setPosition(vsWidth/2, vsHeight/2);
    this->addChild(road);

I have scaled another image with the same code and it worked fine for that image. I don’t know why this strange thing is happening with this image.
I am attaching the image…background

Can someone please help me in this. Thanks in advance.

Post an image of what you expect to see, and another of what you’re actually seeing, to get a better understanding of what the problem is.

Also, are you aware that the RenderTexture sprite is actually flipped vertically? You have to flip it back before using it via Sprite:::setFlippedY();.

Try to adding that call to the RenderTexture sprite and see if it helps, like this:

road = Sprite::createWithTexture(renderTextureForRoad->getSprite()->getTexture());
road->setFlippedY(true);
road->setPosition(vsWidth/2, vsHeight/2);
this->addChild(road);

or alternatively, what may work is to flip the sprites you’re adding to the RenderTexture before calling visit on them, and remember to flip them back if you’re still using them somewhere else.

auto backgroundRoad = Sprite::create("background.png");
auto brWidth = backgroundRoad->getContentSize().width;
auto brHeight = backgroundRoad->getContentSize().height;
backgroundRoad->setScale(vsWidth/brWidth, vsHeight/brHeight);
backgroundRoad->setPosition(Point(vsWidth/2, vsHeight/2));
backgroundRoad->setFlippedY(true);

auto renderTextureForRoad = RenderTexture::create( vsWidth, vsHeight, PixelFormat::RGBA8888 );
renderTextureForRoad->begin();
backgroundRoad->visit();
renderTextureForRoad->end();
backgroundRoad->setFlippedY(false);

road = Sprite::createWithTexture(renderTextureForRoad->getSprite()->getTexture());
road->setPosition(vsWidth/2, vsHeight/2);
this->addChild(road);

Ohh okay, I didn’t know this before. Can you please explain to me why it is so or if there is a reference link by which I can come to know about it. And you got my problem right, I was getting vertically flipped image.
like this:


but I want this:

One more thing, setFlippedY() function is not available for RenderTexture, it is available for normal sprite, so I have to apply it on road sprite( for my example).
And can’t we just add that flip function in RenderTexture code itself so that we will get correct oriented image without flipping it?

Thanks.

Oh sorry! The code sample I posted is actually incorrect, so I’ll update the post with the fix.

It’s okay and Thanks for your help. :slightly_smiling_face:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.