Java LibGDX 在 Android 上移动到触摸位置

标签 java android libgdx

我正在使用这段代码,但有些东西并不完美。它正在工作,但并不顺利。首先它进入 touchX,然后进入 touchY。但我同时想要这个。我尝试了不同的代码但没有成功。我该如何修复它?有什么问题吗?

package com.anil.game1;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
public class Game1 extends ApplicationAdapter {
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private Texture player1Texture;
    private Rectangle player1Rectangle;
    private Vector3 touchPos;
    @Override
    public void create () {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(480, 800);
        player1Texture = new Texture("Player1.png");
        player1Rectangle = new Rectangle();
        player1Rectangle.set(240, 0, 64, 64);
        touchPos = new Vector3();
    }
    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        if (Gdx.input.isTouched()) {
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(touchPos);
            //player1Rectangle.setPosition(new Vector2(touchPos.x, touchPos.y));
        }
        if (touchPos.x > player1Rectangle.x)
            player1Rectangle.x += 5;
        else
            player1Rectangle.x -= 5;
        if (touchPos.y > player1Rectangle.y)
            player1Rectangle.y += 5;
        else
            player1Rectangle.y -= 5;
        batch.draw(player1Texture, player1Rectangle.x - 32, player1Rectangle.y - 32);
        batch.end();
    }
    @Override
    public void dispose () {
        batch.dispose();
        player1Texture.dispose();
    }
}

最佳答案

在本节中:

    if (touchPos.x > player1Rectangle.x)
        player1Rectangle.x += 5;
    else
        player1Rectangle.x -= 5;
    if (touchPos.y > player1Rectangle.y)
        player1Rectangle.y += 5;
    else
        player1Rectangle.y -= 5;

您强制矩形始终在 X 和 Y 方向上移动设定的量。永远不允许只是坐着不动或放慢速度。另外,您可能不想单独处理 X 和 Y,因为这样它的速度在基本方向上会更慢,而在对角线方向上会更快,这看起来很奇怪。最后,你需要配合速度和时间来保持稳定的移动速度。

首先,定义对象的速度。此外,您还需要一个方便计算的 Vector2。

private static final float SPEED = 300f; //world units per second
private final Vector2 tmp = new Vector2();

现在计算出触摸位置后,您想要确定要向哪个方向移动,以及沿着该方向移动多远。因此,在您的 isTouched block 之后:

//how far the player can move this frame (distance = speed * time):
float maxDistance = SPEED * Gdx.graphics.getDeltaTime();

//a vector from the player to the touch point:
tmp.set(touchPos.x, touchPos.y).sub(player1Rectangle.x, player1Rectangle.y); 

if (tmp.len() <= maxDistance) {// close enough to just set the player at the target
    player1Rectangle.x = touchPos.x;
    player1Rectangle.y = touchPos.y;
} else { // need to move along the vector toward the target
    tmp.nor().scl(maxDistance); //reduce vector length to the distance traveled this frame
    player1Rectangle.x += tmp.x; //move rectangle by the vector length
    player1Rectangle.y += tmp.y;
} 

关于Java LibGDX 在 Android 上移动到触摸位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875598/

相关文章:

android - 如何在 Android drawable 中为环形制作圆角

java - SQL语句获取上周、月份和年份记录

android - SMS 正文并未更改其 Intent 中的文本

android - 如何在 LibGDx 中加载图像作为背景?

opengl-es - 在 glsl 着色器中合成两个 alpha 纹理

java - 使用 Artifactory 代理到 Docker Hub 并在防火墙内使用测试容器进行测试

java - Primefaces p :datatable adding and editing rows not working

java - FilenameFilter,搜索文件夹

java - 设置安卓热点 channel

java - 在 Libgdx 中处理渐进式 jpeg 图像