我是LibGdx新手,并试图使我的iceCream图像可触摸。 我想知道如何设置输入过程(通过触摸屏幕)。 我需要再上一堂课吗?当我尝试实现输入过程时 在我的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();
}
}
答案 0 :(得分:6)
您可以使用
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来处理手势事件。
答案 1 :(得分:2)
由于您需要从图像中获取触摸事件,因此您可以使用舞台和演员来完成此操作。您需要使用纹理创建舞台和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()");
}
并将Image添加到舞台上。在渲染方法中,您应该添加:
stage.act();
stage.draw();
并使用
为舞台设置输入处理器Gdx.input.setInputProcessor(stage);
答案 2 :(得分:1)
如果要在类中同时使用ApplicationAdapter
和InputProcessor
,则必须使用接口而不是摘要:将类签名更改为Prac1 implements ApplicationListener, InputProcessor
点击此处查看完整教程: http://www.gamefromscratch.com/post/2013/10/24/LibGDX-Tutorial-5-Handling-Input-Touch-and-gestures.aspx