Jump to content

What is anchor x,y used for?


LearningPhaser9
 Share

Recommended Posts

Think of the image that makes up your sprite as a rectangle. The upper-left corner is (0,0). The lower-right corner is (width - 1, height - 1). The anchor gives a position in the sprite as a percentage of both its width and its height.

 

So an anchor of (0.5, 0.5) is the position (width * 0.5, height * 0.5) in that rectangle. Likewise, an anchor of (0.2, 1) is the position (width * 0.2, height * 1) in your sprite.

 

Phaser uses that to position the sprite. When you set the sprite.x or sprite.y properties you are setting the position of the *anchor* of the sprite in the world.

 

For a platformer, it makes a lot of sense to set the anchor of the player to (0.5, 1). That's the middle of the sprite in the x direction, and the bottom of the sprite in the y direction.

 

Make sense?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...