【问题标题】:Libgdx Actor undetected touch inputLibgdx Actor 未检测到的触摸输入
【发布时间】:2015-07-21 05:33:35
【问题描述】:

我正在寻找触摸检测。下面显示的代码是我在我的应用程序中设置圆圈的方法。我想检测这个圆圈上的触摸,而不是整个纹理周围或整个纹理。奇怪的是没有检测到触摸,我无法检测到它 圈子类:

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

【问题讨论】:

  • 你试过“stage = new Stage();

标签: java libgdx touch actor


【解决方案1】:

您可以在圆形类中使用libgdx的触摸检测。只有触摸的圆形会受到影响。

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

如果您使用很多圆圈,那么最好将触摸位置作为参数来提高性能。

在圈子类

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

在另一个班级

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

【讨论】:

  • 它不起作用编辑:">半径*半径";错了,正确的是“
【解决方案2】:

为了检测添加到舞台中的演员的触摸,调用演员的方法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 将始终返回 null。

现在,你的演员是一个圆圈,所以你可能想要覆盖 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;
}

【讨论】:

    【解决方案3】:

    如果您从 Actor 继承,您需要设置边界,否则它将无法点击/触摸!。 只需设置边界以匹配您的 Actor 包含的纹理。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      相关资源
      最近更新 更多