Flip sprite with accelerometer

  ... discussions about development with the GLES1 branch of AndEngine.

Flip sprite with accelerometer

Postby squeeb » Thu Jan 19, 2012 6:19 pm

How can you flip/change a sprite based on whether it is moving left or right with the accelerometer?
i would like my sprite to face left when moving left and face right when it is moving right. Could someone "point" me in the right direction :D
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Flip sprite with accelerometer

Postby ViktorS » Thu Jan 19, 2012 6:59 pm

Many ways to achive that.
First:
Load a TiledTexture with both states. Use AnimatedSprite change currentTileIndex() when needed.

Second:
Make a copy of you texture by using Texture mCopyTexture = mTexture.deepCopy();
Then do mCopyTexture.setFlippedHorizontal(true);
Then Make a sprite with your copied texture and then swap sprites when needed.(Many ways to acchive that too)

Third:
If your sprite is Vertically simmetrical use a Sprite.setRotation(180.0f);
'Javelin OU' presents, my latest game - Joj the alien
ViktorS
 
Posts: 580
Joined: Thu Oct 27, 2011 2:18 pm

Re: Flip sprite with accelerometer

Postby squeeb » Fri Jan 20, 2012 3:48 am

I understand the copy and everything, but where do I put it so it changes when its moving left...and chang again when it moves right?
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Flip sprite with accelerometer

Postby ViktorS » Fri Jan 20, 2012 1:25 pm

Well, as I already wrote many ways to achieve that.

First:
Search forum for "swap texture", I belive someone asked that question already.(And there were a lot of answers on how to do so)

Second:
Have 2 Sprite. And 1 Reference. Like so:

Sprite mSpriteLeft;
Sprite mSpriteRight;
Sprite Hero = mSpriteLeft;

When needd to change swap it.
float bufX = Hero.getX();
float bufY = Hero.getY();
Hero = mSpriteRight;
Hero.setPosition(bufX,bufY);
(Or any other way to achieve that).
'Javelin OU' presents, my latest game - Joj the alien
ViktorS
 
Posts: 580
Joined: Thu Oct 27, 2011 2:18 pm

Re: Flip sprite with accelerometer

Postby squeeb » Wed Jan 25, 2012 5:21 pm

it loads textures, then force closes



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private BitmapTextureAtlas mTexture2;
  2.  
  3.  
  4.        
  5.         private TiledTextureRegion mPibito;
  6.         private AnimatedSprite mPibitoright;
  7.         private AnimatedSprite mPibitoSprite;
  8.         private ScreenOrientation mScreenOrientation;
  9.         private Camera mCamera;
  10.         private Scene mScene;
  11.         private SensorManager sensorManager;
  12.           private float accellerometerSpeedX;
  13.             private float accellerometerSpeedY;
  14.             private int sX, sY;
  15.  
  16.  
  17.         public Scene onLoadScene() {
  18.                 mScene = new Scene();          
  19.                
  20.             sX = (CAMERA_WIDTH - this.mPibito.getWidth()) / 2;
  21.         sY = (CAMERA_HEIGHT - this.mPibito.getHeight()) / 2;
  22.  
  23.          mPibitoSprite = new AnimatedSprite(sX, sY, this.mPibito);
  24.          mPibitoSprite.setScale((float) 2);
  25.          mPibitoSprite.animate(50, true);
  26.          mScene.attachChild(mPibitoSprite);
  27.  
  28.  
  29. private void updatemSpritePosition() {
  30.                 if ((accellerometerSpeedX != 0) || (accellerometerSpeedY != 0)) {
  31.                          // Set the Boundary limits
  32.                     int tL = 100;
  33.                     int lL = 300;
  34.                     int rL = CAMERA_WIDTH - (int)mPibitoSprite.getWidth();
  35.                     int bL = CAMERA_HEIGHT - (int)mPibitoSprite.getHeight();
  36.          
  37.                     // Calculate New X,Y Coordinates within Limits
  38.                     if (sX >= lL) sX += accellerometerSpeedX; else sX = lL;
  39.                     if (sX <= rL) sX += accellerometerSpeedX; else sX = rL;
  40.                     if (sY >= tL) sY += accellerometerSpeedY; else sY = tL;
  41.                     if (sY <= bL) sY += accellerometerSpeedY; else sY = bL;
  42.          
  43.                     // Double Check That New X,Y Coordinates are within Limits
  44.                     if (sX < lL)      sX = lL;
  45.                     else if (sX > rL) sX = rL;
  46.                     if (sY < tL)      sY = tL;
  47.                     else if (sY > bL) sY = bL;
  48.          
  49.                     mPibitoSprite.setPosition(sX, sY);
  50.                    
  51.                     float bufX = mPibitoSprite.getX();
  52.                     float bufY = mPibitoSprite.getY();
  53.                     mPibitoSprite = mPibitoright;
  54.                     mPibitoSprite.setPosition(bufX,bufY);
  55.                 }
  56.             }
  57.            
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Flip sprite with accelerometer

Postby ViktorS » Wed Jan 25, 2012 5:31 pm

I kinda don't see initialization of mPibitoright sprite... Although i doubt thats actually the case... Quite easy to check, try to start while holdig the phone in a way that your hero will not need a flip. If it's not the case, could you copy/paste logcat error output here? It would really help, thanks.
'Javelin OU' presents, my latest game - Joj the alien
ViktorS
 
Posts: 580
Joined: Thu Oct 27, 2011 2:18 pm

Re: Flip sprite with accelerometer

Postby squeeb » Thu Jan 26, 2012 6:41 pm

1st time using logcat, is this what you needed? When i hold in a way it shouldnt flip, it still FC. it loads background and sprite that moves, then FC

[s01-26 08:36:06.414: INFO/ActivityManager(25692): Start proc com.sw.newwaterfalllwp for service com.sw.newwaterfalllwp/.LiveWallpaperService: pid=1718 uid=10039 gids={}
01-26 08:36:10.238: ERROR/AndroidRuntime(1718): at com.sw.newwaterfalllwp.LiveWallpaperService.updatemSpritePosition(LiveWallpaperService.java:413)
01-26 08:36:10.238: ERROR/AndroidRuntime(1718): at com.sw.newwaterfalllwp.LiveWallpaperService.access$5(LiveWallpaperService.java:388)
01-26 08:36:10.238: ERROR/AndroidRuntime(1718): at com.sw.newwaterfalllwp.LiveWallpaperService$1.onUpdate(LiveWallpaperService.java:330)
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Flip sprite with accelerometer

Postby ViktorS » Thu Jan 26, 2012 6:45 pm

Oh, sorry, my mistake didn't specify... what i need are the Red ones with "AndroidRuntime" tag They usually start with a line like "FATAL EXCEPTION"
'Javelin OU' presents, my latest game - Joj the alien
ViktorS
 
Posts: 580
Joined: Thu Oct 27, 2011 2:18 pm

Re: Flip sprite with accelerometer

Postby squeeb » Thu Jan 26, 2012 6:49 pm

these are the red ones, how can i filter so i only see what my wallpaper is doing, not everything else my phone is doing?

thank you so much
squeeb
 
Posts: 167
Joined: Mon May 16, 2011 1:24 am

Re: Flip sprite with accelerometer

Postby ViktorS » Thu Jan 26, 2012 7:04 pm

A green "+" button creates a filter.
Make one for Log Tag = "AndroidRuntime" , with Log level = "Error", Name and PID are irrelevant right now.
'Javelin OU' presents, my latest game - Joj the alien
ViktorS
 
Posts: 580
Joined: Thu Oct 27, 2011 2:18 pm

Next

Return to GLES1

Who is online

Users browsing this forum: No registered users and 18 guests