COCOS2DX, after the sprite loads the image, changes the sprite size by zooming

Source: Internet
Author: User

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);





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.