Java Libgdx Actor未检测到的触摸输入
我正在寻找触摸检测。下面显示的代码是我在应用程序中设置圆圈所做的操作。我想检测这个圆上的触摸,而不是周围或整个纹理。奇怪的是触摸没有被检测到,在任何地方我都检测不到 圆圈类:Java Libgdx Actor未检测到的触摸输入,java,libgdx,touch,actor,Java,Libgdx,Touch,Actor,我正在寻找触摸检测。下面显示的代码是我在应用程序中设置圆圈所做的操作。我想检测这个圆上的触摸,而不是周围或整个纹理。奇怪的是触摸没有被检测到,在任何地方我都检测不到 圆圈类: public class Circle_Obj extends Actor{ private Vector2 position; private float radius; private com.badlogic.gdx.math.Circle circle; private Textu
public class Circle_Obj extends Actor{
private Vector2 position;
private float radius;
private com.badlogic.gdx.math.Circle circle;
private Texture texture;
public Circle_Obj(float x, float y, float radius) {
position = new Vector2(x,y);
this.radius = radius;
circle = new com.badlogic.gdx.math.Circle(x,y,radius);
texture = new Texture(Gdx.files.internal("texture.png"));
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("TOUCHED", " TOUCHED ");
return true;
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture,0, 0);
}
}
public class GameScreen implements Screen {
private Stage stage;
private Circle_Obj circle_obj;
public GameScreen() {
circle_obj = new Circle_Obj(Static_values.Width/2, Static_values.Height/2, Static_values.Width / 100 * 10);
stage = new Stage(new FitViewport(Static_values.Width/3, Static_values.Height/3));
stage.addActor(circle_obj);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
}
/** other methods **/
}
屏幕类别:
public class Circle_Obj extends Actor{
private Vector2 position;
private float radius;
private com.badlogic.gdx.math.Circle circle;
private Texture texture;
public Circle_Obj(float x, float y, float radius) {
position = new Vector2(x,y);
this.radius = radius;
circle = new com.badlogic.gdx.math.Circle(x,y,radius);
texture = new Texture(Gdx.files.internal("texture.png"));
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("TOUCHED", " TOUCHED ");
return true;
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture,0, 0);
}
}
public class GameScreen implements Screen {
private Stage stage;
private Circle_Obj circle_obj;
public GameScreen() {
circle_obj = new Circle_Obj(Static_values.Width/2, Static_values.Height/2, Static_values.Width / 100 * 10);
stage = new Stage(new FitViewport(Static_values.Width/3, Static_values.Height/3));
stage.addActor(circle_obj);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
}
/** other methods **/
}
您可以在circle类中使用libgdx的触控检测。只有触控的circle才会受到影响
public boolean is_touched() {
if (Gdx.input.justTouched()) {
float xx = Gdx.input.getX();
float yy = Gdx.input.getY();
float x = position.x;
float y = position.y;
return (xx - x) * (xx - x) + (yy - y) * (yy - y) < radius * radius;
}
}
当两个圆重叠且用户触摸重叠区域时,也可以忽略其中一个被触摸的圆
if (Gdx.input.justTouched()) {
float xx = Gdx.input.getX();
float yy = Gdx.input.getY();
if (circle0.is_touched(xx, yy)) {
// do something about circle0
}
else if (circle1.is_touched(xx, yy)) {
// do something about circle1
}
else if (circle2.is_touched(xx, yy)) {
// do something about circle2
}
}
要检测添加到舞台中的演员的触摸,该演员的方法
hit
被称为()
如果从Actor继承,则需要设置边界,否则将无法单击/触摸!。 只需设置边界以匹配演员包含的纹理
//add this Set bounds the x, y, width, and height
circle_obj.setBounds(0, 0,texture.getWidth(), texture.getHeight());
您是否尝试过“stage=newstage()”;它不起作用编辑:“>radius*radius”是错误的,正确的是“@Override
public Actor hit (float x, float y, boolean touchable) {
if (touchable && getTouchable() != Touchable.enabled) return null;
return (x - position.x)*(x- position.x) + (y - position.y)*(y - position.y) < radius*radius ? this : null;
}
//add this Set bounds the x, y, width, and height
circle_obj.setBounds(0, 0,texture.getWidth(), texture.getHeight());