簡體   English   中英

如何在android libgdx中的一個屏幕中移動兩個sprite對象?

[英]How to move two sprite object in one screen in android libgdx?

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

你可以使用scene2d來實現這個目的。你可以通過動作輕松移動對象。 你可以按照這些鏈接來學習scene2d。

http://www.gamefromscratch.com/post/2013/11/27/LibGDX-Tutorial-9-Scene2D-Part-1.aspx

http://www.gamefromscratch.com/post/2013/12/09/LibGDX-Tutorial-9-Scene2D-Part-2-Actions.aspx

我希望這些鏈接可以幫到你。

你可以這樣做:

取兩個2DVector對象:

private Vector2 positiononesprite,positiontwosprite;
Sprite sprite_one,sprite_two;

然后在你的create方法中執行此操作

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是一個方向向量,速度是一個浮點變量,指針位置是一個vector2對象,因為我聲明了positiononesprite

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM