2016-06-21 39 views
1

我想將動作添加到單個遊戲畫面中的兩個精靈對象。我的意思是兩個精靈對象應該使用Libgdx在單個頁面或應用程序的遊戲屏幕中獨立移動(或遵循一些預定義路徑)。我怎樣才能做到這一點。 請幫幫我。如果可能的話,請提供一些參考代碼。謝謝。如何在android libgdx的一個屏幕中移動兩個精靈對象?

+0

你應該改變它們的渲染方法的座標。你有沒有嘗試過一些東西?問題是什麼? – MilanG

回答

0

你可以這樣做:

採取兩種2DVector對象:

private Vector2 positiononesprite,positiontwosprite; 
Sprite sprite_one,sprite_two; 
在創建方法

然後做到這一點

positiononesprite = new Vector2(0,0); 
positiontwosprite = new Vector2(0,0); 

//set your sprite position 
sprite_one.setPosition(x,y);//your x and y coordinates 
sprite_two.setPosition(x1,y1);//your second sprite postions 

positiononesprite.x = sprite_one.getX(); 
positiononesprite.y = sprite_one.getY(); 

positiontwosprite.x = sprite_two.getX(); 
positiontwosprite.y = sprite_two.getY(); 

/* 
then to make them move in a custom direction you can use either 
setPosition method or translate method*/ 

//apply your algorithm on vectors and set or translate your sprites 
// in render method define there speed, direction and move them 
//for example i did this to move it in a particular direction 

pointerposition.x += directionpointer.x * speed; 
      pointerposition.y += directionpointer.y * speed; 

      // pointer.setPosition(pointerposition.x, pointerposition.y); 
      ball.setPosition(pointerposition.x, pointerposition.y); 

這是移動我的球在一個特定的方向 這裏directionpointer是一個方向矢量,speed是一個float變量,pointerposition是一個vector2對象,正如我聲明的positiononesprite

相關問題