LibGdx,如何处理触摸事件?

6

我是LibGdx的新手,正在尝试使我的冰淇淋图片可触摸。我想知道如何设置输入处理(通过屏幕触摸)。我需要创建另一个类吗?当我尝试将输入处理实现到我的Prac1类中时,JAVA不允许我在不改变类抽象的情况下进行实现。具体来说,我想让每次用户触摸图像时都计算触摸次数。以下是我的代码,谢谢您的帮助。

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;

public class Prac1 extends ApplicationAdapter {
    int w,h,tw,th =0;
    OrthographicCamera camera;
    SpriteBatch batch;
    Texture img;

    @Override
    public void create () {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera(w, h);
        camera.position.set(w/2, h/2, 0);
        camera.update();
        batch = new SpriteBatch();
        img = new Texture(Gdx.files.internal("iceCream.png"));
        tw = img.getWidth();
        th = img.getHeight();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, camera.position.x - (tw/2), camera.position.y - (th/2));
        batch.end();
    }
}
3个回答

7
您可以使用
InputProcessor
来处理用户输入。 例如:
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Prac1 extends ApplicationAdapter {
    float w,h,tw,th =0;
    OrthographicCamera camera;
    SpriteBatch batch;
    Sprite img;

    @Override
    public void create () {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera(w, h);
        camera.position.set(w/2, h/2, 0);
        camera.update();
        batch = new SpriteBatch();
        img = new Sprite(new Texture(Gdx.files.internal("iceCream.png")));

        tw = img.getWidth();
        th = img.getHeight();
        img.setBounds( camera.position.x - (tw/2), camera.position.y - (th/2),tw,th);
        Gdx.input.setInputProcessor(new InputAdapter(){

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {

                if(img.getBoundingRectangle().contains(screenX, screenY))
                       System.out.println("Image Clicked");

                return true;
            }

        });
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        img.draw(batch);
        batch.end();
    }
}

请替换此代码为您自己的代码,您可以轻松了解此处正在发生的事情。您还可以实现

GestureListener
来处理手势事件。


2
由于您需要从图像获取触摸事件,因此可以使用Stage和Actors来实现。 您需要创建一个带有纹理的Stage和Image,然后添加Touchable属性:
iceCreamImg.setTouchable(Touchable.enabled);
iceCreamImg.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
    Gdx.app.debug(TAG, "touchDown()");
    // must return true for touchUp event to occur
    return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
    Gdx.app.debug(TAG, "touchUp()");
}

在舞台上添加图片。在渲染方法中,您应该添加以下内容:
stage.act();
stage.draw();

并且还要为您的舞台设置输入处理器,使用:
Gdx.input.setInputProcessor(stage);

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接