LibGdx, How to handle touch event?

2019-02-24 10:43发布

I am LibGdx Newbie, and trying to make my iceCream image touchable. I like to know how to set the input-process(by touch on screen). Do I need to make another class? When I try to implements the input-process to my Prac1 class, JAVA doesn't allow me to implements without changing the class abstract. To be specific, I like to make it whenever the user touch the image ,it counts the number of touch. Here is my code and Thank you for help.

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条回答
爷的心禁止访问
2楼-- · 2019-02-24 10:58

You can use

InputProcessor
to handle user input. Like this:-

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();
    }
}

replace this code with your code you can easily understand what is happening here. your can also implement

GestureListener
to handle gesture events.

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-24 11:05

Since you need to get touch events from image, you can do that with Stage and Actors. You'll need to create a Stage and Image with your texture, then add Touchable attributes:

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()");
}

and add Image to the stage. In render method you should add:

stage.act();
stage.draw();

and also set input processor for your stage with

Gdx.input.setInputProcessor(stage);
查看更多
淡お忘
4楼-- · 2019-02-24 11:20

If you want to use both ApplicationAdapter and InputProcessor in you Class, you have to use the interfaces rather than the abstract: Change you class signature to Prac1 implements ApplicationListener, InputProcessor

Check here for a complete tutorial: http://www.gamefromscratch.com/post/2013/10/24/LibGDX-Tutorial-5-Handling-Input-Touch-and-gestures.aspx

查看更多
登录 后发表回答