Games Trying to put a "fire button" in my stage with LibGDX

Ja8

New Member
Hi everybody:
First of all, this is my first message in the forum. I have read the rules, but I'm sorry if I had some errors writing this.
I'm trying to make a very simple game in LibGDX with a main screen, a stage, an actor, a camera that follows the actor and a couple of buttons (to shot and to jump) in the bottom of the screen.
The problem is I haven't find too many examples with gameplay buttons and I don't understand well how they work in the examples I saw.
I 'll appreciate if you could put me in the right way about how to put the buttons and manage their inputs.

Thank you very much!!
 

ilan

Expert
Licensed User
Longtime User
Hi @Ja8 and welcome to b4x.com!

I'm trying to make a very simple game in LibGDX with a main screen, a stage, an actor, a camera that follows the actor and a couple of buttons (to shot and to jump) in the bottom of the screen.

this does not sound like a very simple game.
libgdx is very complex and has many many features.

i would recommend your first to go through the awesome examples of @Informatix (https://www.b4x.com/android/forum/threads/libgdx-game-engine.32594/#content)
have a look at the Scene2D_Buttons example to see how to add buttons to a lgScn2DStage and handle clicks.

basically you can do it in 2 ways.

1, work with libgdx Actors and work with them similar to simple views like button, etc..
2, you draw everything with lgSpriteBatch including buttons etc.. and handle the touches/clicks with lgInputProcessor so if a touch on x/y happens where the button texture is drawn then perform a click.

what you should avoid is mixing b4a views in your libgdx game. use only libgdx components and choose one of the two ways listed here.

So have a look at @Informatix examples and also check this great simple example from @andymc: https://www.b4x.com/android/forum/threads/cloney-bird-libgdx-version.38793/#content
 
Last edited:

Ja8

New Member
Finally, I've written 2 options to draw the buttons and receive the touchs and keys inputs:
Option1:
public class SuperController2 extends InputAdapter{
    public boolean shooting=false;
    private Rectangle recFire;
    private Sprite sprFire;
    private Stage escena;
    private Batch batch;
    private Vector3 pos;
    public SuperController2(Stage escena){
        this.escena=escena;
        this.batch=escena.getBatch();
        sprFire=new Sprite(new Texture("button.png"));
        recFire=new Rectangle(0,0,sprFire.getWidth(),sprFire.getHeight());               
    }
    public void actualizar() {
        batch.begin();
        sprFire.setBounds(escena.getCamera().position.x-(Gdx.graphics.getWidth()/2)+50, escena.getCamera().position.y-(Gdx.graphics.getHeight()/2)+50, 50, 50);
        sprFire.draw(batch);
        batch.end();
        if(Gdx.input.isTouched()){
            pos=new Vector3(Gdx.input.getX(),Gdx.input.getY(),0);
            recFire.setPosition(sprFire.getX(),sprFire.getY());
            Camera cam=escena.getCamera();
            cam.unproject(pos);
            if(recFire.contains(pos.x,pos.y)) {
                shooting=true;
            }
        }
        if(Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
            shooting=true;
        }
    }
}
Or the orther one:
Option2:
public class SuperController1  extends Actor implements InputProcessor{
    Texture texFire;
    Stage escena;
    public boolean shooting=false;
    private Rectangle recFire;
    
    public SuperController1(Stage escena){
        this.escena=escena;
        texFire=new Texture("button.png");
        recFire=new Rectangle(50,380,50,50);       
    }
    @Override
    public void draw(Batch batch,float alpha) {
        this.setSize(100, 50);
        batch.draw(texFire,this.getX()+50,this.getY()+50,50,50);
    }
    @Override
    public void act(float delta) {
        super.act(delta);
    }
    @Override
    public boolean keyDown(int keycode) {
        switch(keycode) {
        case Input.Keys.SPACE:
            shooting=true;
            return true;
        default:
            return false;
        }
    }
    @Override
    public boolean keyUp(int keycode) {
        switch(keycode) {
        case Input.Keys.SPACE:
            shooting=false;
            return true;
        default:
            return false;
        }
    }
        @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 pos=new Vector3(this.getX(),this.getY(),0);
        Camera cam=escena.getCamera();
        cam.unproject(pos);
        if(recFire.contains(screenX, screenY)) {
            izquierda=true;
            return true;
        }else {
            return false;
        }
    }
    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if(recFire.contains(screenX, screenY)) {
            shooting=false;
            return true;
        }else{
            return false;
        }
    }
    @Override
    public boolean keyTyped(char character) {    return false;    }
    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {    return false;    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {    return false;    }

    @Override
    public boolean scrolled(float amountX, float amountY) {    return false;    }
}

Which one do you think is better?
(I haven´t solved the resize window problem yet)

Thanks!
 
Top