AndEngine Tutorial #2: Flipping a Sprite’s Orientation

Do you have a character that needs to be drawn facing right or left depending on what direction he/she/it is heading?  Then you will want to learn how to flip the character so you don’t have to manually flip that asset in Photoshop and double the file size and complexity of implenting the character graphics.

Before we get into the code, let me give some quick and overly simplistic definitions. In AndEngine, an Entity is a display object and a Sprite is a type of Entity that is used to display either a single image asset or a group of image assets.  An asset can be a character, inanimate object, background or (in a future release) containers/groups that can consist of any number of other entities allowing for more of a hierarchical system (search ‘sprite composition’ at AndEngine’s forum for more details).

Back at the task at hand.  To flip an entity’s orientation either vertically or horizontally is as simple as getting a reference to the TextureRegion used for that entity and calling ‘setFlippedHorizontal’ and/or ‘setFlippedVertical’.  If all you have is a reference to the Sprite, you can call its ‘getTextureRegion’ method.

//Flip the original texture region.
mSnapdragonTextureRegion.setFlippedHorizontal(true);
 
//OR
 
//Get a reference to the texture region and then flip.
snapdragon.getTextureRegion().setFlippedHorizontal(true);

As one would expect, flipping does not change an object’s x or y position, so if you position your character correctly in the original file asset they will be exactly where they were before the flip but facing the other direction.

Please note that there is one major catch/gotcha to doing this.  If you have multiple entities using the same TextureRegion, flipping it will result in all other Entities also flipping.  So imagine you have 4 enemies on screen who all use the same TextureRegion.  One of the enemies changes direction and therefore flips its orientation so its facing the opposite direction.  Whoops, now all 4 are facing the new direction…this is bad.

The solution is simple, when creating or setting the Sprite’s TextureRegion, simply call the method ‘clone’.

for(int i = 0 ; i < 4 ; i++)
{
	//This uses the same TextureRegion over and over.
	snapdragon = new AnimatedSprite(300, 150, 	this.mSnapdragonTextureRegion);
}
 
//OR
 
for(int i = 0 ; i < 4 ; i++)
{
	//This clones the TextureRegion so you can make changes
	//to it without affecting other Sprites.
	snapdragon = new AnimatedSprite(300, 150, 	this.mSnapdragonTextureRegion.clone());
}

Now you have an easy solution for flipping your Sprites.  Now go and make the most of of it!

For more articles like this, news, tutorials, trends and more follow us on Twitter.

And check out our other AndEngine tips and tutorials.

Share:
  • Twitter
  • Digg
  • Yahoo! Buzz
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Mixx
  • FriendFeed
  • Technorati
  • Facebook
  • RSS

3 Comments

  1. [...] out the rest of our AndEngine tips and tutorials OR go directly to tutorial #2. [...]

  2. [...] This post was mentioned on Twitter by Eric Snowden and Theory Nine. Theory Nine said: #AndEngine #tutorial no.2 released: Flipping a Sprite http://ow.ly/3FLlA [...]

Leave a Comment

Please leave these two fields as-is: