问题描述
我是LibGdx新手,并试图使我的iceCream图像可触摸。
我想知道如何设置输入过程(通过触摸屏幕)。
我需要再上一堂课吗?当我尝试将输入过程实现到
我的Prac1类时,JAVA不允许我在不改变类抽象的情况下实现。具体来说,我喜欢在用户触摸图像
时制作它,它会计算触摸次数。这是我的代码并感谢您的帮助。
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 tomy 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();
}
}
推荐答案
你可以使用
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
来处理手势事件。
这篇关于LibGdx,如何处理触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!