2016-04-28 13 views
1
public class GameScreen implements Screen { 

    public void show() 
    { 
     buttonsAtlas = new TextureAtlas("Color.pack"); //button atlas image 
     buttonSkin = new Skin(); 
     buttonSkin.addRegions(buttonsAtlas); 
     font = new BitmapFont(Gdx.files.internal("CustomFont.fnt"), false); //the font 

     stage = new Stage(); // window is stage 
     stage.clear(); 

     TextButton.TextButtonStyle style = new TextButton.TextButtonStyle(); // button properties 
     style.up = buttonSkin.getDrawable("Red"); 
     style.down = buttonSkin.getDrawable("Blue"); 
     style.font = font; 

     gameButton = new TextButton("Game Screen", style); //button text and style 
     gameButton.setPosition(-250, 500); //button location 
     gameButton.setHeight(600); //button height 
     gameButton.setWidth(1200); //button width 

     shopButton = new TextButton("Shop Menuuuu", style); //button text and style 
     shopButton.setPosition(100, 500); //button location 
     shopButton.setHeight(600); //button height 
     shopButton.setWidth(1200); //button width 

     stage.addActor(shopButton); 
     stage.addActor(gameButton); 

     Gdx.input.setInputProcessor(stage); 

     gameButton.addListener(new InputListener() { 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       gameButton.setBounds(-250, 500, 100, 100); 
       Gdx.app.log("my app,", "pressed"); 
       game.setScreen(new inGame(game)); 
       return true; 
      } 

      public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       Gdx.app.log("my app", "released"); 
      } 
     }); 

     shopButton.addListener(new InputListener() { 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       shopButton.setBounds(100, 500, 100, 100); 
       Gdx.app.log("my app,", "pressed"); 
       game.setScreen(new Shop(game)); 
       return true; 
      } 

      public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       Gdx.app.log("my app", "released"); 
      } 
     }); 
    } 

    public void render(float x) 
    { 
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     stage.act(); 
     game.batch.begin(); 
     stage.draw(); 
     game.batch.end(); 
    } 

    public void dispose() 
    { 
     batch.dispose(); 
     buttonSkin.dispose(); 
     buttonsAtlas.dispose(); 
     stage.dispose(); 
    } 

TextButtonを同時に動作させようとしています。ただし、画面には常に1つのボタンが表示されます。これはステージに追加された最後のボタンです。この場合は、ステージに追加された最後のボタンなので、gameButtonです。LibGDXで2つの異なるテキストボタンが反応しない

どのようにボタンの操作を行うのですか?座標負の設定にはポイントがありません

+0

良いアイデアではありませんが、私はこの問題は、あなたのボタンのサイズであると仮定します。なぜボタンの幅と高さが大きいのですか? – Enigo

答えて

0

は、あなたがゲームのための座標系を理解する必要があることがあります。 Oを、oはあなたが負の値を与えるときに、その一部が表示されていない画面 の左下隅にあります。

あなたのケースでは2つの画像が重なり、ステージはアクタにzインデックスを与え、最後に追加されたオブジェクトは最高のzインデックスを持ちます。つまり、最後に追加されたオブジェクトは他のオブジェクトの上に描画されます。あなたがする必要がどのような

gameButton.setPosition(0, 0); //x,y --absolute to bottom left corner 
gameButton.setSize(200,200); //width, height 

shopButton.setPosition(200, 0); //x,y --absolute to bottom left corner 
shopButton.setSize(200,200); //width, height 

もactionlistner内setboundやってはあなたのコードが正常に動作します

+0

実際には互いにオーバーラップしています。今私は座標系についてよく理解しています。どうもありがとうございます! – user21478621

関連する問題