![](/img/trans.png)
[英]how to move randomly generated object to the center of the screen in android Using LIBgdx
[英]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.