Whats the best Way to move a sprite?

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

Whats the best Way to move a sprite?

Postby Volvagia » Mon Mar 12, 2012 10:10 pm

I have a simple List containing a row of Fields (e. g. Waypoints), sprites move from First to the Next in a loop.
But the moving of Sprites looking different. E. G. One is faster then Another or any lag and jumping more then moving.
I have detected the Move(X/Y)Modifier, but I don't know its the best Way to 'restart' Loop into Listeners.

Her's the simple movement Code. The 'Waypoints' containing X/Y-Descriptor of Fields (0/0, 0/1, 0/2, 5/5 etc.), the Loop get the Class of Fields (Terrain), get his Sprite, from the Sprite the real x/y-Coordinates, and move the Sprite of the Unit about sinus/cosinus and 'Pythagoras' for the Distance from one field to another.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final int LOOP_MOVE_WIDTH = 4;
  2. private static final long SLEEP_TIME = 30L;
  3.  
  4. public void run() {
  5.         final Unit unit = model.getUnit();
  6.         final TiledSprite unitSprite = unit.getSprite();
  7.        
  8.         Point[] way = model.getWay();
  9.        
  10.         int currentX = unit.getX(); //Feldkoordinaten
  11.         int currentY = unit.getY();
  12.        
  13.         for(Point p:way) {
  14.                 int nextX = p.x; //Feldkoordinaten
  15.                 int nextY = p.y;
  16.                
  17.                 Terrain currentTerrain = model.getTerrain(currentX, currentY);
  18.                 Terrain nextTerrain = model.getTerrain(nextX, nextY);
  19.                
  20.                 Sprite currentTerrainSprite = currentTerrain.getSprite();
  21.                
  22.                 int currentTerrainX = (int) currentTerrainSprite.getX(); //Pixelkoordinaten
  23.                 int currentTerrainY = (int) currentTerrainSprite.getY();
  24.                
  25.                 Sprite nextTerrainSprite = nextTerrain.getSprite();
  26.                
  27.                 final int nextTerrainX = (int) nextTerrainSprite.getX(); //Pixelkoordinaten
  28.                 final int nextTerrainY = (int) nextTerrainSprite.getY();
  29.                
  30.                 double atan2 = Math.atan2(nextTerrainY - currentTerrainY, nextTerrainX - currentTerrainX);
  31.                 double sin = Math.sin(atan2) * LOOP_MOVE_WIDTH;
  32.                 double cos = Math.cos(atan2) * LOOP_MOVE_WIDTH;
  33.                
  34.                 double distance = Math.sqrt(Math.pow(nextTerrainX - currentTerrainX, 2) +
  35.                                 Math.pow(nextTerrainY - currentTerrainY, 2));
  36.                
  37.                 double realX = currentTerrainX;
  38.                 double realY = currentTerrainY;
  39.                
  40.                 int spriteTileId = getSpriteTileId(currentTerrainX, currentTerrainY, nextTerrainX, nextTerrainY);
  41.                 spriteTileId = (spriteTileId != 0 ? spriteTileId * 2 : 0);
  42.                 unitSprite.setCurrentTileIndex(spriteTileId);
  43.                                        
  44.                 while(distance > 0) {
  45.                         realX+= cos;
  46.                         realY+= sin;
  47.                        
  48.                         final double finalX = realX;
  49.                         final double finalY = realY;
  50.                        
  51.                         model.postToUpdateThread(new Runnable() {
  52.                                 public void run() {
  53.                                         unitSprite.setPosition((int)finalX, (int)finalY);                                              
  54.                                 }
  55.                         });
  56.                        
  57.                         try {
  58.                                 TimeUnit.MILLISECONDS.sleep(SLEEP_TIME);
  59.                         } catch (InterruptedException e) {
  60.                                 e.printStackTrace();
  61.                         }
  62.                        
  63.                         distance-= LOOP_MOVE_WIDTH;
  64.                 }
  65.                
  66.                 currentX = nextX;
  67.                 currentY = nextY;
  68.                                
  69.                 model.postToUpdateThread(new Runnable() {
  70.                         public void run() {
  71.                                 unitSprite.setPosition(nextTerrainX, nextTerrainY);
  72.                         }
  73.                 });
  74.                 unit.setX(nextX); //Feldkoordinaten
  75.                 unit.setY(nextY);
  76.         }
  77. }
  78.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Volvagia
 
Posts: 3
Joined: Wed Feb 29, 2012 5:57 am
Location: Austria

Return to GLES2

Who is online

Users browsing this forum: Google [Bot], xninjaxelitex, Yahoo [Bot] and 31 guests