libgdx阶段无法正常工作

时间:2017-03-15 15:39:35

标签: java android import libgdx

我正在安装Android工作室的游戏应用程序,但我的hud代码无法正常工作。当我尝试将我的第三个表添加到舞台,actionButtons时,它的位置与我的第二个表,控件相同,即使我已经为不同的位置指定了它们。关于如何解决这个问题的任何建议?这是我的hud代码。

package com.mygdx.marryinthemirrors.Scenes;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.marryinthemirrors.MarryInTheMirrors;

public class Hud {
    public Stage stage;
    private Viewport viewport;

    private Integer worldTimer;
    private float timeCount;
    private Integer score;
    private boolean leftPressed, rightPressed, upPressed;

    Label countdownLabel;
    Label scoreLabel;
    Label timeLabel;
    Label levelLabel;
    Label worldLabel;
    Label playerLabel;


    public Hud(SpriteBatch sb){
        worldTimer = 300;
        timeCount = 0;
        score = 0;

        viewport=new FitViewport(MarryInTheMirrors.V_WIDTH, MarryInTheMirrors.V_HEIGHT, new OrthographicCamera());
        stage = new Stage(viewport, sb);
        Gdx.input.setInputProcessor(stage);

        Table table=new Table();
        table.top();
        table.setFillParent(true);

        Table control = new Table();
        control.bottom().right();

        Table actionButtons = new Table();
        actionButtons.bottom().left();

        //general info
        countdownLabel=new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        scoreLabel=new Label(String.format("%06d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        timeLabel=new Label("TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        levelLabel=new Label("1-1", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        worldLabel=new Label("WORLD", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        playerLabel=new Label("PLAYER", new Label.LabelStyle(new BitmapFont(), Color.WHITE));

        table.add(playerLabel).expandX().padTop(10);
        table.add(worldLabel).expandX().padTop(10);
        table.add(timeLabel).expandX().padTop(10);
        table.row();
        table.add(scoreLabel).expandX();
        table.add(levelLabel).expandX();
        table.add(countdownLabel).expandX();

        Image upImg=new Image(new Texture("flatDark25.png"));
        upImg.setSize(100, 100);
        upImg.addListener(new InputListener(){

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                upPressed = true;
                return true;
            }

            @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                upPressed = false;
            }
        });

        Image leftImg=new Image(new Texture("flatDark23.png"));
        leftImg.setSize(100, 100);
        leftImg.addListener(new InputListener(){

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                leftPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                leftPressed = false;
            }
        });

        Image rightImg=new Image(new Texture("flatDark24.png"));
        rightImg.setSize(100, 100);
        rightImg.addListener(new InputListener(){

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                rightPressed = true;
                return true;
        }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                rightPressed = false;
           }
        });

        control.add(leftImg).pad(0,10,5,0);
        control.add().pad(0,10,0,10);
        control.add(rightImg).pad(0,10,5,0);
        control.pack();

        actionButtons.add(upImg).pad(0,0,10,5);
        actionButtons.pack();

        stage.addActor(table);
        stage.addActor(control);
        stage.addActor(actionButtons);

    }

    public boolean isLeftPressed() {
        return leftPressed;
    }

    public boolean isRightPressed() {
        return rightPressed;
    }

    public boolean isUpPressed(){ return  upPressed; }

}

0 个答案:

没有答案