LibGDX scene2d阶段不显示按钮

时间:2012-12-11 16:39:04

标签: java libgdx scene2d

我有一个scene2d舞台,它应该显示按钮和触摸板,但它没有。它只是显示黑屏。这就是我正在做的事情:

skins.json

{
    "com.badlogic.gdx.scenes.scene2d.ui.ImageButton$ImageButtonStyle": {
        "buttonA": { "down": "buttonA", "up": "buttonA" },
        "buttonB": { "down": "buttonB", "up": "buttonB" },
        "buttonX": { "down": "buttonX", "up": "buttonX" },
        "buttonY": { "down": "buttonY", "up": "buttonY" },
    },
    "com.badlogic.gdx.scenes.scene2d.ui.Touchpad$TouchpadStyle": {
        "touchpad": {"background": "touchpadBG", "knob": "touchpadKnob"}
    }
}

controller.java

package com.letigames.ananse.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad.TouchpadStyle;
import com.letigames.ananse.entity.Player;
import com.letigames.ananse.entity.util.Position;
import com.letigames.ananse.entity.util.Size;
import com.letigames.ananse.entity.util.TouchpadDirectional;
import com.letigames.ananse.weaponsSystem.util.Direction;

public class Controller {

    public Controller(Stage stage, Player player,Size size){
        this.stage = stage;
        this.playerDirectional = new TouchpadDirectional(false, false, false, false);
        this.screenSize = size;
        this.MapButtons();
        this.GenerateAtlas();
        this.StyleButtons();
        this.PlaceButtons();
        this.AddButtonsToStage();
        this.AssignActions(player);
        System.out.println("* controller setup *");
    }

    private void MapButtons(){
        this.buttonBStagePosition = new Position(this.screenSize.GetWidth() - (Controller.separator + (Controller.separator/4)), 
                this.screenSize.GetHeight() - (this.screenSize.GetHeight() - (Controller.separator/4))); 
        this.buttonYStagePosition = new Position(this.screenSize.GetWidth() - ((Controller.separator/4) + Controller.separator), 
                this.screenSize.GetHeight() - (this.screenSize.GetHeight() - ((Controller.separator/4) + Controller.separator + (Controller.separator/2)))); 
        this.buttonAStagePosition = new Position(this.screenSize.GetWidth() - (((Controller.separator  * 2)+ (Controller.separator/4)  + (Controller.separator/2))), 
                this.screenSize.GetHeight() - (this.screenSize.GetHeight() - (Controller.separator/4))); 
        this.buttonXStagePosition = new Position(this.screenSize.GetWidth() - (((Controller.separator  * 2)+ (Controller.separator/4)  + (Controller.separator/2))), 
                this.screenSize.GetHeight() - (this.screenSize.GetHeight() - ((Controller.separator/4) + Controller.separator + (Controller.separator/2)))); 
        this.touchpadStagePosition = new Position((Controller.separator/4), (Controller.separator/4));

        this.buttonXRegionPosition = new Position(90, 100);
        this.buttonYRegionPosition = new Position(90, 100);
        this.buttonARegionPosition= new Position(90, 100); 
        this.buttonBRegionPosition = new Position(90, 100);
        this.touchpadRegionPosition = new Position(90, 50);

        System.out.println("* mapped buttons to their positions *");
    }

    private void GenerateAtlas(){
        this.controllerAtlas = new TextureAtlas();
        this.controllerTexture = new Texture(Gdx.files.internal("data/fishes.jpeg"));
        this.controllerAtlas.addRegion("buttonA", new TextureRegion(this.controllerTexture, this.buttonARegionPosition.GetX(), 
                this.buttonARegionPosition.GetY(), Controller.size.GetWidth(), Controller.size.GetHeight()));
        this.controllerAtlas.addRegion("buttonB", new TextureRegion(this.controllerTexture, this.buttonBRegionPosition.GetX(), 
                this.buttonBRegionPosition.GetY(), Controller.size.GetWidth(), Controller.size.GetHeight()));
        this.controllerAtlas.addRegion("buttonX", new TextureRegion(this.controllerTexture, this.buttonXRegionPosition.GetX(), 
                this.buttonXRegionPosition.GetY(), Controller.size.GetWidth(), Controller.size.GetHeight()));
        this.controllerAtlas.addRegion("buttonY", new TextureRegion(this.controllerTexture, this.buttonYRegionPosition.GetX(), 
                this.buttonYRegionPosition.GetY(), Controller.size.GetWidth(), Controller.size.GetHeight()));
        this.controllerAtlas.addRegion("touchpadBG", new TextureRegion(this.controllerTexture, this.touchpadRegionPosition.GetX(), 
                this.touchpadRegionPosition.GetY(), (Controller.size.GetWidth() * 2), (Controller.size.GetHeight() * 2)));
        this.controllerAtlas.addRegion("touchpadKnob", new TextureRegion(this.controllerTexture, this.touchpadRegionPosition.GetX(), 
                this.touchpadRegionPosition.GetY(), (Controller.size.GetWidth()/2), (Controller.size.GetHeight()/2)));

        System.out.println("* generated texture atlas*");
    }

    private void StyleButtons(){
        this.skin = new Skin(Gdx.files.internal("data/skins.json"), this.controllerAtlas);
        ImageButtonStyle buttonAStyle = this.skin.get("buttonA", ImageButtonStyle.class);
        ImageButtonStyle buttonBStyle = this.skin.get("buttonB", ImageButtonStyle.class);
        ImageButtonStyle buttonXStyle = this.skin.get("buttonX", ImageButtonStyle.class);
        ImageButtonStyle buttonYStyle = this.skin.get("buttonY", ImageButtonStyle.class);
        this.buttonA = new ImageButton(buttonAStyle);
        this.buttonB = new ImageButton(buttonBStyle);
        this.buttonX = new ImageButton(buttonXStyle);
        this.buttonY = new ImageButton(buttonYStyle);
        TouchpadStyle touchpadStyle = this.skin.get("touchpad", TouchpadStyle.class);
        this.touchPad = new Touchpad(10f, touchpadStyle);
        System.out.println("* styled buttons *");
    }

    private void PlaceButtons(){
        this.buttonA.setX(this.buttonAStagePosition.GetX());
        this.buttonB.setX(this.buttonBStagePosition.GetX());
        this.buttonX.setX(this.buttonXStagePosition.GetX());
        this.buttonY.setX(this.buttonYStagePosition.GetX());
        this.touchPad.setX(this.touchpadStagePosition.GetX());
        this.buttonA.setY(this.buttonAStagePosition.GetY()); 
        this.buttonB.setY(this.buttonBStagePosition.GetY());
        this.buttonX.setY(this.buttonXStagePosition.GetY());
        this.buttonY.setY(this.buttonYStagePosition.GetY());
        this.touchPad.setY(this.touchpadStagePosition.GetY());

        System.out.println("* placed buttons on the stage *");
    }

    private void AddButtonsToStage(){
        this.stage.addActor(this.buttonA);
        this.stage.addActor(this.buttonB);
        this.stage.addActor(this.buttonX);
        this.stage.addActor(this.buttonY);
        this.stage.addActor(this.touchPad);
        System.out.println("* added buttons to the stage *");
    }

    private void AssignActions(final Player player){
        this.buttonA.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { return true;}});

        this.buttonB.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { return true; }});

        this.buttonX.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { return true; }});

        this.buttonY.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { return true; }});
        System.out.println("* assigned actions *");
    }

    public void DetermineDirection(){
        if(this.touchPad.isTouched()){
            boolean moveLeft = false;
            boolean moveRight = false;
            boolean moveUp = false;
            boolean moveDown = false;
            float xPercent = this.touchPad.getKnobPercentX();
            float yPercent = this.touchPad.getKnobPercentY();
            if((xPercent > 0) && (yPercent > 0) && (xPercent > yPercent)){ moveRight = true; }
            if((xPercent > 0) && (yPercent < 0) && (xPercent > Math.abs(yPercent))){ moveRight = true; }

            if((yPercent > 0) && (xPercent > 0) && (yPercent > xPercent)){ moveUp = true; }
            if((yPercent > 0) && (xPercent < 0) && (yPercent > Math.abs(xPercent))){ moveUp = true; }

            if((xPercent < 0) && (yPercent > 0) && (Math.abs(xPercent) > yPercent)){ moveLeft = true; }
            if((xPercent < 0) && (yPercent < 0) && (Math.abs(xPercent) > Math.abs(yPercent))){ moveLeft = true; }

            if((yPercent < 0) && (xPercent > 0) && (Math.abs(yPercent) > xPercent)){ moveDown = true; }
            if((yPercent < 0) && (xPercent < 0) && (Math.abs(yPercent) > Math.abs(xPercent))){ moveDown = true; }

            this.playerDirectional.SetDirections(moveLeft, moveRight, moveUp, moveDown);
        }
        else{ this.playerDirectional.SetDirections(Controller.defaultDirectional.moveLeft, Controller.defaultDirectional.moveRight, 
                Controller.defaultDirectional.moveUp, Controller.defaultDirectional.moveDown); }
    }

    public void UpdateMove(Player player){
        /* D-Pad */
        //System.out.println("move left ->" + this.playerDirectional.moveLeft);
        //System.out.println("move right ->" + this.playerDirectional.moveRight);
        //System.out.println("move up ->" + this.playerDirectional.moveUp);
        //System.out.println("move down ->" + this.playerDirectional.moveDown);
        if(this.playerDirectional.moveLeft){
            if(player.GetCanWalk()) { 
                if(!player.GetJumped()){  player.Walk(Direction.LEFT); }
                if(player.GetJumped() && player.GetWebStringReady()){ player.MoveInAir(Direction.LEFT); }
                player.SetIsFacingLeft(true);
            }
            if(!player.GetWebStringReady() && player.GetJumped()){ player.Swing(Direction.LEFT); player.SetIsFacingLeft(true); }
        }
        if(this.playerDirectional.moveRight){
            if(player.GetCanWalk()) { 
                if(!player.GetJumped()) { player.Walk(Direction.RIGHT); }
                if(player.GetJumped() && player.GetWebStringReady()){ player.MoveInAir(Direction.RIGHT); }
                player.SetIsFacingLeft(false);
            }
            if(!player.GetWebStringReady() && player.GetJumped()){ player.Swing(Direction.RIGHT); player.SetIsFacingLeft(false); }
        }
        if(this.playerDirectional.moveUp){
            if(player.GetStickToWallState()){ 
                player.GetBody().applyLinearImpulse(player.GetCrawlImpulse(), 
                player.GetBody().getWorldCenter()); 
                player.SetIsFacingDown(false); 
            }
            if(this.buttonA.isPressed() && player.GetWebStringReady() && (!player.GetCanJump())){ player.TryAttachString(true); }
            if(!player.GetWebStringReady()){ player.UpdateAttachedString(true); }
        }
        if(this.playerDirectional.moveDown){
            if(player.GetStickToWallState()){
                player.GetBody().applyLinearImpulse(player.GetCrawlImpulse().cpy().mul(-1), player.GetBody().getWorldCenter()); 
                player.SetIsFacingDown(true); 
            }
            if(!player.GetWebStringReady()){ player.UpdateAttachedString(false); }
        } 

        /* four  buttons */
        if(this.buttonB.isPressed()){
            if(this.playerDirectional.moveLeft || this.playerDirectional.moveRight){  
                if(this.playerDirectional.moveLeft){ player.Throw(Direction.LEFT); }
                else if(this.playerDirectional.moveLeft){ player.Throw(Direction.RIGHT); }
            }
            else { player.Throw(Direction.NONE); }
         }
         if(this.buttonA.isPressed()){
             if(player.GetCanJump()){ 
                 player.Jump();
                 player.SetCanJump(false);
             }
         }
         if(this.buttonX.isPressed()){
             if(!player.GetWebStringReady()){ player.TryDetachString(true); }
             if(player.GetStickToWallState()){ player.DetachFromWall(); }
         }
         if(this.buttonY.isPressed()){  player.Punch(); }
    }

    private Touchpad touchPad;
    public ImageButton buttonA;
    public ImageButton buttonB;
    public ImageButton buttonX;
    public ImageButton buttonY;
    public ImageButton dPadLeft;
    public ImageButton dPadRight;
    public ImageButton dPadUp;
    public ImageButton dPadDown;
    private TextureAtlas controllerAtlas;
    private Texture controllerTexture;
    private Skin skin;
    private Size screenSize;
    private Stage stage;

    private Position buttonARegionPosition;
    private Position buttonBRegionPosition;
    private Position buttonXRegionPosition;
    private Position buttonYRegionPosition;
    private Position touchpadRegionPosition;
    private Position touchpadStagePosition;
    private Position buttonAStagePosition; 
    private Position buttonBStagePosition;
    private Position buttonXStagePosition; 
    private Position buttonYStagePosition;
    public TouchpadDirectional playerDirectional;
    private static final int separator = 100;
    private static final TouchpadDirectional defaultDirectional = new TouchpadDirectional(false, false, false, false);
    private static final Size size = new Size(100, 100); /* set the default size here for all the buttons */
}

应用程序监听器类

package com.letigames.ananse;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.letigames.ananse.entity.util.Size;
import com.letigames.ananse.ui.Controller;

public class Ananse implements ApplicationListener {
    @Override
    public void create() {      
        this.screenWidth = Gdx.graphics.getWidth();
        this.screenHeight = Gdx.graphics.getHeight();   
        this.doSleep = true;
        this.gravity = new Vector2(0f, -10f);
        this.UIStage = new Stage();
        //Gdx.input.setInputProcessor(this.UIStage);
        this.world = new World(this.gravity, this.doSleep);
        this.camera = new OrthographicCamera(this.screenWidth, this.screenHeight);
        this.debugRenderer = new Box2DDebugRenderer();
        this.level = new Level(this.world);
        this.controller = new Controller(this.UIStage, this.level.GetPlayer(), new Size((int)this.screenWidth, (int)this.screenHeight));
        this.world.setContactListener(level.GetEntityContactListener());

        //batch = new SpriteBatch();
        //texture = new Texture(Gdx.files.internal("data/libgdx.png"));
        //texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        //TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
        //sprite = new Sprite(region);
        //sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
        //sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
        //sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
    }

    @Override
    public void dispose() {
        level.DestroyLevel(false);
        this.UIStage.dispose();

        //batch.dispose();
        //texture.dispose();
    }

    @Override
    public void render() {      
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        debugRenderer.render(world, camera.combined);

        //batch.setProjectionMatrix(camera.combined);
        //batch.begin();
        //sprite.draw(batch);
        //batch.end();

        level.UpdateLevel();
        world.step(1/20f, 6, 2);
        this.controller.DetermineDirection();
        this.controller.UpdateMove(this.level.player);
        this.camera.position.set(level.GetPlayer().GetBody().getWorldCenter().x, level.GetPlayer().GetBody().getWorldCenter().y, 0);
        this.camera.zoom = 0.1f;
        this.camera.update();
        this.UIStage.act(Gdx.graphics.getDeltaTime());
        this.UIStage.draw();
    }

    @Override
    public void resize(int width, int height) { this.UIStage.setViewport(width, height, true); }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    private OrthographicCamera camera;
    private World world;
    private Stage UIStage;
    private Controller controller;
    private Vector2 gravity;
    private boolean doSleep;
    private float screenWidth; 
    private float screenHeight;
    Box2DDebugRenderer debugRenderer;
    //private SpriteBatch batch;
    //private Texture texture;
    //private Sprite sprite;
    private Level level;
}

我正在使用LibGDX v 0.9.7。这段代码在几天前工作得很好,它不再出于某种原因而且不会生成错误消息。代码中引用的文件都存在:LibGDX assets文件夹中的data文件夹中。想法?

0 个答案:

没有答案