Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Why does my texture-based object picking detect the wrong object? [closed]

$
0
0

I am a LibGDX beginner implementing a RISK-like game. I want to let the player select territories by clicking on them, but for some reason, the wrong territory is sometimes detected.

The clicked territory is determined like this: I have a Territory class which extends Actor. Every territory is the size of the game map, but the constructor takes a .png which determines its shape, by making everything not within that territory transparent. I then ignore clicks on transparent regions of the image.

(I also tried doing it by overriding hit but then my clicks wouldn’t even be detected.)

All clicks on transparent areas are ignored, but regardless of whether I click on territory 1 or 2, the game reports Territory T1 selected!.

What might be the issue?


Here’s are the relevant parts of my Territory class:

public Territory(String filename, String name) {
    pixmap = new Pixmap(Gdx.files.internal("Actors/" + filename)); 
    region = new Texture(pixmap);
    this.setName(name);
}

public void draw(Batch batch, float parentAlpha) {
    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a*parentAlpha);
    batch.draw(region, 0, 0);
}


public void setStage(Stage stage) {
    super.setStage(stage);

    if(stage != null) {
        stage.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y,
                    int pointer, int button) {
                int cX = (int)x;
                int cY = pixmap.getHeight() - (int)y;
                if((pixmap.getPixel(cX, cY) & 0x000000ff) != 0) {
                    selected = true;
                    return true;
                }
                return false;
            }
        });
    }
}

Here is the part of my Game Screen, where I create the territory objects and add them to the stage:

public GameMap(WorldDom mygame) {
    this.game = mygame;
    GAMESTATE = 0; 

    listT = new Territory []{
        new Territory("territory1.png", "t1"),
        new Territory("territory2.png", "t2")
    };

    texture = new Texture("DEMO.png");
    map = new Image(texture);
    table = new Table();
    stage = new Stage();

}

public static void draw(){
    stage.draw();
    //stage.hit(Gdx.input.getX(), Gdx.input.getY(), true);
    for(int i = 0; i < listT.length; i++) {
        if(listT[i].isSelected()){
            System.out.println("Territory " + listT[i].getName()
                + " selected!");
            listT[i].setSelected(false);
        }
    }
}

public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if(GAMESTATE == 0) {
        draw();
    }
    if(GAMESTATE == 1) {
        /* ... */
    }
}

public void resize(int width, int height) {
    /* ... */
}

public void show() {

    table.setWidth(Gdx.graphics.getWidth());
    table.setHeight(Gdx.graphics.getHeight());
    table.add(map);

    stage.addActor(table);
    for(int i = 0; i < listT.length; i++) {
        stage.addActor(listT[i]);
    }

    Gdx.input.setInputProcessor(stage);
}

Viewing all articles
Browse latest Browse all 434

Trending Articles