Create a sprite based on the picture:
Auto Sprite_back = sprite::create ("Login_background.png");
The wizard that loads the image directly calls the method Setcontentsize () does not work, the picture is still much larger.
So the only thing that can actually be said to change size is the scaling setscale (float x,float y);
The actual project is generally based on the screen to adapt, generally refer to the screen as a percentage.
Let's say there's a background map, full screen, Sprite width = screen width, Sprite height = screen height
Here we get the zoom ratio and then call the Zoom method to zoom
Zoom ratio x = screen width/Sprite width
Zoom ratio y similarly
Call Setcontentsize at this point (zoom ratio x, zoom ratio y) to get normal width
In general, in order not to destroy the actual aspect ratio of the picture so that the image distortion becomes blurred, only need to get X or y can be directly scaled. Except for the background map.
When you need to set the screen as a percentage:
Zoom ratio x = Screen width * percent/Sprite width
Zoom ratio y similarly
Example:
Get screen width height
size size = Director::getinstance ()->getwinsize ();
Background map:
Background image
Auto Sprite_back = sprite::create ("Login_background.png");
Sprite_back->setposition (SIZE.WIDTH/2, SIZE.HEIGHT/2); Set position
sprite_back->setanchorpoint (Point (0.5, 0.5)); Set Anchor point
//Set actual size according to scale size
Size_back = Sprite_back->getcontentsize (); Get the sprite size (when setting the picture, the sprite size is the picture size)
//Calculate the desired zoom ratio by the actual width and the total width
float ScaleX = (float) size.width/(float) size_ Back.width;
float ScaleY = (float) size.height/(float) size_back.height;
Sprite_back->setscale (Scalex,scaley); Scaling
The size is based on the percentage of the screen, where 10%
Percent Sprite
Auto Sprite_pai_back = sprite::create ("Lianhuan_button_sure.png");
Sprite_pai_back->setposition (SIZE.WIDTH/2,SIZE.HEIGHT/2);
Sprite_pai_back->setanchorpoint (Point (0.5,0.5));
Set the actual size according to the scale
Size_pai = Sprite_pai_back->getcontentsize ();
Depending on the actual width and total width, the desired zoom ratio is calculated as
float Scalex_pai = (float) size.width*0.1f/(float) size_pai.width;
float Scaley_pai = (float) size.height*0.1f/(float) size_pai.height;
Sprite_pai_back->setscale (Scalex_pai, Scaley_pai);