Header Ads

[Cocos2d-x v3.0] Scale the background image to fill the Screen

Scale the background image to fill the Screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// ask the director for the visible window size
// specifically, it return visible size of the OpenGL view in points
Size visibleSize = Director::getInstance()->getVisibleSize();
     
// returns visible origin of the OpenGL view in points.
Point origin = Director::getInstance()->getVisibleOrigin();
 
 
// create background image from png
auto bgImage = Sprite::create("HelloWorld.png");
 
// position the sprite on the center of the screen
bgImage->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
 
// add background image at z-position = -1, bottom of all
// the z-position value is relative to parent
this->addChild(bgImage, -1);
     
// calculate the scaling factor to fill the screen size
float rX = visibleSize.width / bgImage->getContentSize().width;
float rY = visibleSize.height / bgImage->getContentSize().height;
     
// set the scaling factor to the background image
bgImage->setScaleX(rX);
bgImage->setScaleY(rY);

No comments:

Powered by Blogger.