简体   繁体   English

如何在android libgdx中的一个屏幕中移动两个sprite对象?

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

I want to add motion to two sprite objects in single game screen . 我想在单个游戏画面中为两个精灵对象添加动作 I mean two sprite object should move(or follow some predefined path) independently in single page or game screen of app using Libgdx . 我的意思是两个精灵对象应该在使用Libgdx的应用程序的单页或游戏屏幕中独立移动(或遵循一些预定义的路径)。 How can I achieve that. 我怎样才能做到这一点。 Please help me. 请帮我。 If possible please provide some reference code also. 如果可能,请提供一些参考代码。 Thank you. 谢谢。

you can use scene2d for this purpose.where you can easily move object through actions. 你可以使用scene2d来实现这个目的。你可以通过动作轻松移动对象。 you can follow these links to learn scene2d. 你可以按照这些链接来学习scene2d。

http://www.gamefromscratch.com/post/2013/11/27/LibGDX-Tutorial-9-Scene2D-Part-1.aspx 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 http://www.gamefromscratch.com/post/2013/12/09/LibGDX-Tutorial-9-Scene2D-Part-2-Actions.aspx

i hope these links will help you. 我希望这些链接可以帮到你。

You can do this: 你可以这样做:

take two 2DVector objects: 取两个2DVector对象:

private Vector2 positiononesprite,positiontwosprite;
Sprite sprite_one,sprite_two;

then in your create method do this 然后在你的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);

this is moving my ball in a particular direction here directionpointer is a direction vector and speed is a float variable and pointerposition is a vector2 object as i declared positiononesprite 这是在一个特定方向移动我的球这里directionpointer是一个方向向量,速度是一个浮点变量,指针位置是一个vector2对象,因为我声明了positiononesprite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM