1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ Did you check the bounds of your image as opposed to a button? Did you compare printing the bounds of the image and the ones of the button? Also, please have a look at some Java naming conventions (e.g. classes should start uppercase, while methods start lowercase). \$\endgroup\$
    – Ignatiamus
    Feb 8, 2019 at 10:07
  • \$\begingroup\$ So I have printed out the bounds that i am setting before and believe them to be correct. When i change the bounds, the size of the Image also changes so it seems the bounds are set to the same size as the graphical representation of the image. Is the setting of bounds correct? \$\endgroup\$
    – Cameron
    Feb 8, 2019 at 11:19

1 Answer 1

0
\$\begingroup\$

In libgdx each actor should belongs to a parent (for example a stage). in each frame (render method) you should call stage.act() for running touch and stuff for each actor in stage. and for drawing you can call stage.draw(). you call stage.act() one time when you are on init stage. just add stage.act() on render method and it should fix your problem.

\$\endgroup\$

You must log in to answer this question.

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