Cocos2d anchor point Anchor Point Analysis)

Source: Internet
Author: User

What is anchor point? It is hard to understand because we usually look at an image to determine the position of the Image Based on the image center. In cocos2d, the position of an image is determined by two dimensions: position, the center of the image, and anchor point. As long as we figure out their relationships, we will naturally be able to solve them.

Their relationship is as follows:

Actualposition. x = position. x + width * (0.5-anchor_point.x); acturalposition. Y = position. Y + height * (0.5-anchor_point.y)

Actualposition is the position actually displayed on the screen by Sprite, poistion is set by program, and achor_point is set by program.

For details, see the following example 1:

 

[HTML]  View plaincopy

  1. Ccsprite*Sprite=[CcspriteSpritewithfile: @ "blacksquare.png"]; 
  2. Sprite. Position = CCP (0, 0 ); 
  3. Sprite. anchorpoint = CCP (0, 0 ); 
  4. [SelfAddchild: SPRITE]; 


The specific effect is as follows:

Based on the formula above: assume that the width of the Sprite is 10.

Actualposition. x = 0 + 10 * (0.5-0) = 5; actualposition. Y= 0 + 10 * (0.5-0) = 5;

(5, 5) the result is the actual position of the current image on the screen.

Example 2:

 

[HTML]  View plaincopy

  1. Ccsprite*Sprite=[CcspriteSpritewithfile: @ "blacksquare.png"]; 
  2. Sprite. Position = CCP (0, 0 ); 
  3. Sprite. anchorpoint = CCP (-1,-1 ); 
  4. [SelfAddchild: SPRITE]; 


The specific effect is as follows:

 

Based on the formula above: assume that the width of the Sprite is 10.

Actualposition. x = 0 + 10 * (0.5-(-1) = 15; actualposition. Y= 0 + 10 * (0.5-(-1) = 15;

(15, 15) the result is the actual position of the current image on the screen.

Example 3

 

[HTML]  View plaincopy

  1. Ccsprite*Sprite=[CcspriteSpritewithfile: @ "blacksquare.png"]; 
  2. Sprite. anchorpoint = CCP (1, 1 ); 
  3. Sprite. Position = CCP (sprite. contentsize. Width,Sprite. contentsize. Height ); 
  4. [SelfAddchild: SPRITE]; 

Based on the formula above: assume that the width of the Sprite is 10.

Actualposition. x = 10 + 10 * (0.5-(1) = 5; actualposition. Y= 10 + 10 * (0.5-(1) = 5;

(5, 5) the result is the actual position of the current image on the screen.

Reprinted from: http://blog.sina.com.cn/s/blog_4ab2f5c8010146xg.html

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.