Friday, April 19, 2019

java - Libgdx Actor touch undetected



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



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...