0
\$\begingroup\$

I am looking for detect touch in an actor. The touch in my game is detected in wrong part of the screen. I want to detect touch only on the red circle, here is the code:

Static_values

public class Static_values {

    static public float Width = Gdx.graphics.getWidth();
    static public float Height = Gdx.graphics.getHeight();


}

Circle class:

public class Circle_Obj extends Actor{

    private Vector2 position;
    private float radius;

    private float speedX;
    private float speedY;


    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;

        speedX = 5.5f;
        speedY = 5.5f;

        circle = new com.badlogic.gdx.math.Circle(x,y,radius);
        texture = new Texture(Gdx.files.internal("texture.png"));

        this.setSize(Static_values.Width,Static_values.Height);
        this.setPosition(position.x,position.y);
        this.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) {

        this.setSize(Static_values.Width,Static_values.Height);
        this.setPosition(position.x,position.y);
        batch.draw(texture, position.x, position.y, radius, radius);
}

Screen class :

public class GameScreen implements Screen {

    private Stage stage;
    private Circle_Obj circle_obj;


    public GameScreen() {

        circle_obj = new Circle_Obj(Static_values.Width/3, Static_values.Height/3, Static_values.Width / 100 * 10);

        stage = new Stage();
        stage.addActor(circle_obj);

        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();

    }
/** other methods **/

Colored area = area where touch is detected : enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Can you please show the code for the static values? \$\endgroup\$ May 10, 2015 at 22:26
  • \$\begingroup\$ @StrongJoshua done \$\endgroup\$
    – user65825
    May 10, 2015 at 22:27
  • 1
    \$\begingroup\$ Well of course the touch is being detected everywhere! You set the circle's size to the be the size of the window! Check out my answer (once it is written). \$\endgroup\$ May 11, 2015 at 2:02
  • \$\begingroup\$ If your question is resolved, please either post your own answer or, if my answer is what solved the problem, please accept it as correct. \$\endgroup\$ May 16, 2015 at 3:10

1 Answer 1

1
\$\begingroup\$

If you want your hit detection to be as accurate as possibly, you need to correctly set your actor's size and position. For your example, this would probably go something like this:

The circle's constructor:

public Circle(Texture tex, Vector2 position) {
    this.tex = tex;
    this.setPosition(position.x, position.y);
    this.setSize(tex.getWidth(), tex.getHeight());
    this.addListener(new ClickListener() {
        public void clicked()...
    });
}

Then the circle's draw method:

public void draw(Batch batch, float parentalpha) {
    batch.draw(tex, this.getX(), this.getY(), this.getWidth(), this.getHeight());
}

This way, the actor will be the size of the texture that is being drawn, and even if you have the constructor accept values for height and width, the texture will be adjusted and drawn to fit the actor.

Try using the code I provided (more or less) directly and let me know if you have any trouble.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .