Android libgdx scene2d - button disappears

62 views Asked by At

I need to create an ever visible in the viewport of the camera button, the camera moves around a tiledmap. Is there any way to add something like a cloak to avoid redrawing the button?

Right now I have another problem with the button, the button is visible when loading the game, but it disappears quickly. I have to create the button whenever the act () event to make this visible, causing problems ugly effects of movements on the button

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;


public abstract class GameButton extends Button {

    protected Rectangle bounds;
    private Skin skin;


    public GameButton(Rectangle bounds) {
        this.bounds = bounds;
        setWidth(bounds.width);
        setHeight(bounds.height);
        setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
        skin = new Skin();
        skin.addRegions(AssetsManager.getTextureAtlas());
        loadTextureRegion();
        addListener(new ClickListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                touched();
                loadTextureRegion();
                return true;
            }
        });
    }


    protected void loadTextureRegion() {
        ButtonStyle style = new ButtonStyle();
        style.up = skin.getDrawable(getRegionName());
        setStyle(style);
    }

    protected abstract String getRegionName();

    public abstract void touched();

    public Rectangle getBounds() {
        return bounds;
    }
}

public class PauseButton extends GameButton {
    public int indice;

    public interface PauseButtonListener {
        public void onPause();

        public void onResume();
    }

    private PauseButtonListener listener;

    public PauseButton(Rectangle bounds, PauseButtonListener listener, int ind) {
        super(bounds);
        this.listener = listener;
    }

    @Override
    protected String getRegionName() {
        return GameManager.getInstance().getGameState() == GameState.PAUSED ? Constants.PLAY_REGION_NAME : Constants.PAUSE_REGION_NAME;
    }

    @Override
    public void act(float delta) {
        super.act(delta);
        if (GameManager.getInstance().getGameState() == GameState.OVER) {
            remove();
        }
    }

    @Override
    public void touched() {
        if (GameManager.getInstance().getGameState() == GameState.PAUSED) {
            listener.onResume();
        } else {
            listener.onPause();
        }
    }
}

in the Stage

if(pauseButton != null) {
    pauseButton.remove();
}
Rectangle pauseButtonBounds = new Rectangle((stageCam2.position.x - Constants.APP_WIDTH / 2) + 2f,
        150f, 47f,
        48f);
pauseButton = new PauseButton(pauseButtonBounds, new GamePauseButtonListener(), 0);
addActor(pauseButton);
1

There are 1 answers

0
walter jr On

I solved the bug by removing the PauseButton and GameButton classes, and I added to the stage this code:

botonAccion1 = new Button();

Rectangle pauseButtonBounds = new Rectangle(
        getCamera().viewportWidth - 85f,
        getCamera().viewportHeight - 90f, 
        45f,
        45f);

botonAccion1.setBounds(
        pauseButtonBounds.x, pauseButtonBounds.y, 
        pauseButtonBounds.width, pauseButtonBounds.height);
botonAccion1.setWidth(pauseButtonBounds.width * 1.5f);
botonAccion1.setHeight(pauseButtonBounds.height * 1.5f);

skin = new Skin();
skin.addRegions(AssetsManager.getTextureAtlas());
ButtonStyle style = new ButtonStyle();
style.up = skin.getDrawable("wooditem");

botonAccion1.setStyle(style);

botonAccion1.addListener(new ClickListener() {
    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        add_action = "wooditem"; 
        return true;
    }
});

addActor(botonAccion1);

and in the draw() method I added this code before super.draw();:

botonAccion1.setPosition((stageCam2.position.x + Constants.APP_WIDTH / 2) - 85f, botonAccion1.getY());