So I am attempting to make a an image of a closed door for a ship. On clicking this door, it would then change to an open door. However, I am struggling to get the click listener to work for the Image. Weirdly, if i use a textbutton instead it works, with pretty much the same code. Below is the class in which a stage is created and the Image actor added to it.
public class battleScreenActorOverlay implements Disposable {
public battleScreenActorOverlay(World world, SpriteBatch sb, battleShipScreen syst, float bottomButsH, float viewportWidth, float viewportHeight, List<Float> testDoor) {
viewport = new ExtendViewport(viewportWidth,viewportHeight, new OrthographicCamera());
stage = new Stage(viewport, sb);
Gdx.input.setInputProcessor(stage);
shipObjsAt = new TextureAtlas("batScreen/objects/shipObjects.atlas");
Skin skin2 = new Skin(shipObjsAt);
Image img = new Image(skin2.getDrawable("doorV"));
img.setTouchable(Touchable.enabled);
img.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
System.out.println("clicked");
}
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touch down");
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touch up");
}
});
// set Image position and dimensions, then boundaries
img.setPosition(testDoor.get(2),testDoor.get(3));
img.setSize(testDoor.get(1)*6, testDoor.get(0)*6);
System.out.println("gabriel "+img.getX()+" "+img.getY()+" "+img.getWidth()+" "+img.getHeight());
img.setBounds(img.getX(),img.getY(),img.getWidth(),img.getHeight());
// add to stage which is then rendered in main class
stage.addActor(img);
stage.act();
}
Then the main class basically does some stuff like this:
public class battleShipScreen implements Screen {
// a load of variable declarations here
public battleShipScreen(MyGdxGame game){
// create viewport etc
genShip();
}
public void genShip(){
// basically method for setting door positions
batScreenActs = new battleScreenActorOverlay(world,game.batch,this,margin,gameport.getWorldWidth(),gameport.getWorldHeight(),testDoor); // calls class which creates stage and adds door
}
@Override
public void show() {
}
public void update(float dt){
world.step(1/60f,6,2);
handleInput(dt);
gamecam.update();
renderer.setView(gamecam);
}
public void handleInput(float dt) {
// classic handle input
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor((float) 60/255, (float) 181/255, (float) 0/255, (float) 0); // clears screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // clears screen
// render game map
renderer.render();
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.setProjectionMatrix(batScreenHUD.stage.getCamera().combined);
game.batch.setProjectionMatrix(batScreenActs.stage.getCamera().combined);
batScreenActs.stage.act(Gdx.graphics.getDeltaTime());
batScreenActs.stage.draw();
}
@Override
public void resize(int width, int height) {
System.out.println("resizing "+height+" by "+width);
gameport.update(width,height);
gamecam.update();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
Sorry for the long post, but I have been trying to sort this out for a few days now and have literally no idea what i am doing wrong. Any help working out how to make an Image actor click listener work would be much appreciated!