Home>

Make a simple game for Android on libgdx, and now it was necessary to make buttons to control, before that I tested the application in a desktop version, tied to the keys, tried to add a button, but instead just a black and white strip on the screen.

package com.mygdx.game.screens;
Import com.Badlogic.gdx.gdx;
Import com.Badlogic.gdx.input;
Import com.Badlogic.gdx.screen;
Import com.Gadlogic.gdx.graphics.gl20;
Import com.Badlogic.gdx.graphics.orthographiccamera;
import com.Badlogic.gdx.graphics.g2d.Bitmapfont;
Import com.Badlogic.gdx.graphics.g2d.TextureAtlas;
Import com.Badlogic.gdx.maps.tiled.tiledmap;
Import com.Badlogic.gdx.maps.tiled.tmxmaploader;
Import com.Badlogic.gdx.maps.tiled.renderers.orthogonaltiledmaprenderer;
Import com.Badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.box2ddebugrenderer;
import com.Badlogic.gdx.physics.Box2d.World;
Import com.Badlogic.gdx.scenes.scene2d.stage;
Import com.Badlogic.gdx.scenes.scene2d.ui.table;
Import com.Badlogic.gdx.scenes.scene2d.ui.TextButton;
Import com.Badlogic.gdx.utils.viewport.extendViewPort;
import com.Badlogic.gdx.utils.viewport.FitViewPort;
Import com.Badlogic.gdx.utils.viewport.viewport;
Import com.mygdx.game.main;
Import com.mygdx.game.scenes.hud;
Import com.mygdx.game.sprites.samurai;
Import com.mygdx.game.tools.b2worldcreator;
import com.mygdx.game.tools.worldcontactlistener;
Public Class PlayScreen IMPLEMENTS SCREEN {
    PRIVATE MAIN GAME;
    Private TextureAtlas Atlas;
    Private Stage Stage;
    Private TextButton Button;
    Private TextButton.TextButtonStyle TextButtonStyle;
    Private Bitmapfont font;
    Private Orthographiccamera Gamecam;
    Private ViewPort Gameport;
    Private HUD HUD;
    Private TmxMaploader Maploader;
    Private Tiledmap Map;
    Private OrthogonaltiledMaprenderer Renderer;
    Private World World;
    Private box2ddebugrenderer B2DR;
    Private Samurai Player;
    Public PlayScreen (Main Game) {
        Atlas= new textureatlas ("Fighters /Samurai /samurai.pack");
        this.game= Game;
        gamecam= new orthographiccamera ();
        Gameport= New FitViewport (main.v_width /main.ppm, main.v_height /main.ppm, gamecam);
        HUD= New HUD (Game.Batch);
        Maploader= new tmxmaploader ();
        Map= maploader.load ("Map /map.tmx");
        Renderer= New OrtogonaltileDMaprenderer (Map, 1 /Main.ppm);
        gamecam.position.set (gameport.getWorldwidth () /2, gameport.getWorldheight () /2, 0);
        World= New World (New Vector2 (0, -10), True);
        b2dr= new box2ddebugrenderer ();
        //B2DR.SETDRAWBODies (FALSE);
        Player= New Samurai (World, This);
        New B2WorldCreator (WORLD, MAP);
        world.setcontactlistener (new worldcontactlistener ());
        Stage= New Stage (Gameport);
        Gdx.input.setInputProcessor (Stage);
        font= new bitmapfont ();
        textbuttonstyle= new textbutton.textButtonStyle ();
        textbuttonstyle.font= font;
        Button= New TextButton ("Button", TextButtonStyle);
        Table Table= New Table ();
        Table.add (Button);
        table.row ();
        table.setfillParent (TRUE);
        table.center (). Center ();
        Stage.addactor (Table);
    }
    Public TextureAtlas Getatlas () {
        RETURN ATLAS;
    }
    @OVerride
    Public void show () {
    }
    Public Void HandleInput (Float DT) {
        if (gdx.input.iskeyjustpressed (input.keys.space)) {
            player.b2body.applyLineArimpulse (new vector2 (0, 2.5f), player.b2body.getWorldCenter (), TRUE);
        }
        if (GDX.Input.isKeypressed (input.keys.d) &
&
 Player.b2body.getLineArveLocity (). X <
= 2) {
            player.b2body.applyLineArimpulse (new vector2 (0.1f, 0), player.b2body.getWorldCenter (), TRUE);
        }
        If (GDX.Input.isKeypressed (input.keys.a) &
&
 player.b2body.getlinearvelocity (). X >
= -2) {player.b2body.applyLineArimpulse (new vector2 (-0.1f, 0), player.b2body.getworldcenter (), TRUE); }
    }
    Public Void Update (Float DT) {
        HandleInput (DT);
        Player.Update (DT);
        world.step (1 /60f, 6, 2);
        gamecam.position.x= player.b2body.getposition (). X;
        gamecam.update ();
        Renderer.SetView (Gamecam);
    }
    @OVerride
    Public Void Render (Float Delta) {
        UPDATE (DELTA);
        Gdx.gl.glclearcolor (0, 0, 0, 1);
        Gdx.gl.glclear (GL20.GL_COLOR_BUFFER_BIT);
        renderer.render ();
        B2DR.Render (World, Gamecam.combined);
        game.batch.setprojectionmatrix (gamecam.combined);
        game.batch.begin ();
        Player.DRAW (game.batch);
        game.batch.end ();
        Stage.Act (GDX.GRAPHICS.getDeltaTime ());
        Stage.Draw ();
    }
    @OVerride
    Public Void Resize (int width, int height) {
        gameport.update (Width, Height);
    }
    @OVerride
    Public void Pause () {
    }
    @OVerride
    Public Void Resume () {
    }
    @OVerride
    Public Void Hide () {
    }
    @OVerride
    Public void Dispose () {
        map.dispose ();
        renderer.dispose ();
        world.dispose ();
        b2dr.dispose ();
        hud.dispose ();
}

}

  • Answer # 1

    Add to variables:

    Private Stage Stage;
    Private TextButton Button;
    Private TextButton.TextButtonStyle TextButtonStyle;
    Private Bitmapfont font;
    Private Skin Button_skin;
    Private textureatlas button_textureatlas;
    

    PlayScreen () :

    Public PlayScreen (Main Game) {
            Atlas= new textureatlas ("Fighters /Samurai /samurai.pack");
            this.game= Game;
            gamecam= new orthographiccamera ();
            Gameport= New FitViewport (main.v_width /main.ppm, main.v_height /main.ppm, gamecam);
            HUD= New HUD (Game.Batch);
            Maploader= new tmxmaploader ();
            Map= maploader.load ("Map /map.tmx");
            Renderer= New OrtogonaltileDMaprenderer (Map, 1 /Main.ppm);
            gamecam.position.set (gameport.getWorldwidth () /2, gameport.getWorldheight () /2, 0);
            World= New World (New Vector2 (0, -10), True);
            b2dr= new box2ddebugrenderer ();
            //B2DR.SETDRAWBODies (FALSE);
            Player= New Samurai (World, This);
            New B2WorldCreator (WORLD, MAP);
            world.setcontactlistener (new worldcontactlistener ());
            //Button:
            Stage= New Stage ();
            Gdx.input.setInputProcessor (Stage);
            font= new bitmapfont ();
            Button_skin= New Skin ();
            button_textureatlas= new textureatlas (GDX.Files.Internal ("interface /buttons_atlas.atlas"));
            button_skin.addregions (Button_TextureAtlas);
            textbuttonstyle= new textbuttonstyle ();
            textbuttonstyle.font= font;
            textbuttonstyle.up= button_skin.getdrawable ("Button"); //not pressed button
            textbuttonstyle.down= button_skin.getdrawable ("Button_pressed"); //Pressed button
            Button= New TextButton ("Play", TextButtonStyle);
            Button.setsize (button.getwidth () * 16, Button.getHeight () * 2); //Button size, most likely need to change
            //Place buttons on the screen:
            Button.setposition (GDX.graphics.getWidth () /2 -Button.GetWidth () /2, GDX.graphics.getHeight () -Button.getHeight () * 2);
            Stage.Addactor (Button);
            //Button Pressing Processor
            Button.Addlistener (New Changelistener () {
                @OVerride
                Public Void Changed (CHANGEEVENT EVENT, ACTOR ACTOR) {
                    //Your action
                }
            });
            //------
        }
    

    Render () :

    @OVerride
        Public Void Render (Float Delta) {
            UPDATE (DELTA);
            Gdx.gl.glclearcolor (0, 0, 0, 1);
            Gdx.gl.glclear (GL20.GL_COLOR_BUFFER_BIT);
            renderer.render ();
            B2DR.Render (World, Gamecam.combined);
            game.batch.setprojectionmatrix (gamecam.combined);
            game.batch.begin ();
            Player.DRAW (game.batch);
            game.batch.end ();
            Stage.Draw ();
        }