Libgdx 演员未检测到触摸输入

Libgdx Actor undetected touch input

我正在寻找触摸检测。下面显示的代码是我在我的应用程序中设置一个圆圈所做的。我想检测这个圆圈上的触摸,而不是整个纹理周围或整个纹理上的触摸。奇怪的是没有检测到触摸,在任何地方我都检测不到 圆class:

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

屏幕 class :

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 **/
}

你可以使用libgdx的圆圈触摸检测class.Only触摸圆圈会影响。

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

如果您使用了很多圆圈,那么将触摸位置作为参数可以获得更好的性能。

圈内class

public boolean is_touched(float xx,float yy) {
            float x = position.x;
            float y = position.y;
            return (xx - x) * (xx - x) + (yy - y) * (yy - y) < radius * radius;
        }
    }

在另一个 class

if (Gdx.input.justTouched()) {
            float xx = Gdx.input.getX();
            float yy = Gdx.input.getY();
            if (circle0.is_touched(xx, yy)) {
                // do something about circle0
            }
            if (circle1.is_touched(xx, yy)) {
                // do something about circle1
            }
            if (circle2.is_touched(xx, yy)) {
                // do something about circle2
            }
        }

当两个圆圈重叠并且用户触摸重叠区域时,您也可以忽略触摸的圆圈之一。

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 (hit detection in the documentation)

public Actor hit (float x, float y, boolean touchable) {
    if (touchable && getTouchable() != Touchable.enabled) return null;
    return x >= 0 && x < width && y >= 0 && y < height ? this : null;
}

您尚未为 Circle_Obj 演员设置尺寸,因此 hit 将始终 return 为空。

现在,您的 actor 是一个圆圈,因此您可能想要覆盖 hit 以便它检查给定坐标是否在圆圈中,而不是检查坐标是否在框中的默认实现演员的大小。

类似于:

@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;
}

如果继承Actor,需要设置bounds,否则不会click/touch-able!。 只需设置边界以匹配您的 Actor 包含的纹理。

    //add this Set bounds the x, y, width, and height
    circle_obj.setBounds(0, 0,texture.getWidth(), texture.getHeight());