2013-05-19 88 views
1

我有5個按鈕,我需要做不同的事情,但他們不需要,我需要知道如何讓他們做到這一點。LIBGDX在一個屏幕上製作5個不同的按鈕

這是我的代碼;

public class MainMenu implements Screen { 

CrazyZombies game; 
Stage stage; 
TextureAtlas atlas; 
Skin skin; 
SpriteBatch batch; 
Button play, option, quit, custom, store, menu; 

public MainMenu(CrazyZombies game) { 
    this.game = game; 
} 

public void create() { 
    stage = new Stage(); 
} 

@Override 
public void render(float delta) { 
    Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1); 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

    stage.act(delta); 
    stage.draw(); 

    batch.begin(); 
    batch.end(); 
} 

@Override 
public void resize(int width, int height) { 
    if (stage == null) 
     stage = new Stage(width, height, true); 
    stage.clear(); 

    stage.setViewport(854, 480, true); 
    stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0); 

    Gdx.input.setInputProcessor(stage); 

    /** 
    * quit Button 
    */ 

    TextButtonStyle styleQuit = new TextButtonStyle(); 
    styleQuit.up = skin.getDrawable("8layer"); 
    styleQuit.down = skin.getDrawable("8layer"); 

    quit = new Button(styleQuit); 

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

     public void touchUp(InputEvent event, float x, float y, 
       int pointer, int button) { 

     } 
    }); 

    /** 
    * End quit Button 
    */ 

    /** 
     * store Button 
     */ 

    TextButtonStyle styleStore = new TextButtonStyle(); 
    styleStore.up = skin.getDrawable("9layer"); 
    styleStore.down = skin.getDrawable("9layer"); 

    store = new Button(styleStore); 

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

     public void touchUp(InputEvent event, float x, float y, 
       int pointer, int button) { 
      game.setScreen(new StoreScreen(game)); 
     } 
    }); 

    /** 
    * End store Button 
    */ 

    /** 
     * customs Button 
     */ 

    TextButtonStyle styleCustom = new TextButtonStyle(); 
    styleCustom.up = skin.getDrawable("10layer"); 
    styleCustom.down = skin.getDrawable("10layer"); 

    custom = new Button(styleCustom); 

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

     public void touchUp(InputEvent event, float x, float y, 
       int pointer, int button) { 
      game.setScreen(new CustomScreen(game)); 
     } 
    }); 

    /** 
    * End customs Button 
    */ 

    /** 
     * Options Button 
     */ 

    TextButtonStyle styleOptions = new TextButtonStyle(); 
    styleOptions.up = skin.getDrawable("11layer"); 
    styleOptions.down = skin.getDrawable("11layer"); 

    option = new Button(styleOptions); 

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

     public void touchUp(InputEvent event, float x, float y, 
       int pointer, int button) { 
      game.setScreen(new OptionScreen(game)); 
     } 
    }); 

    /** 
    * End Options Button 
    */ 

    /** 
     * Play Button 
     */ 

    TextButtonStyle stylePlay = new TextButtonStyle(); 
    stylePlay.up = skin.getDrawable("7layer"); 
    stylePlay.down = skin.getDrawable("7layer"); 

    play = new Button(stylePlay); 

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

     public void touchUp(InputEvent event, float x, float y, 
       int pointer, int button) { 
      Gdx.app.log(CrazyZombies.LOG, "un-touched"); 
      game.setScreen(new GameScreen(game)); 
     } 
    }); 

    /** 
    * End Play Button 
    */ 

    /** 
    * start Background 
    */ 

    TextButtonStyle styleMenu = new TextButtonStyle(); 
    styleMenu.up = skin.getDrawable("background"); 

    menu = new Button(styleMenu); 

    /** 
    * End Background 
    */ 

    stage.addActor(menu); 
    stage.addActor(play); 
    stage.addActor(option); 
    stage.addActor(store); 
    stage.addActor(custom); 
    stage.addActor(quit); 

} 

@Override 
public void show() { 
    Audio.playMusic(true); 
    batch = new SpriteBatch(); 
    atlas = new TextureAtlas("data/mainmenu/mainmenu.pack"); 
    skin = new Skin(); 
    skin.addRegions(atlas); 
} 

@Override 
public void hide() { 
    dispose(); 
} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

@Override 
public void dispose() { 
    batch.dispose(); 
    skin.dispose(); 
    atlas.dispose(); 
    stage.dispose(); 
    Audio.dispose(); 
} 

public void playButton(Button play) { 

} 
} 

所以我的5個按鈕都設置有操作和聽衆,但現在他們沒有做任何事情,當我挑選一個按鈕,以測試它的工作原理,但該按鈕可以從屏幕上的任何地方進行點擊,從而我認爲這是按鈕區域的問題,但我不知道如何設置它。

我試過.getheight(),.getWidth等,但它仍然是一樣的。雖然在我的紋理地圖集中,所有圖像的高度和寬度都相同,但都會造成一張圖像成爲我的問題?

+0

是連接到佈局資源的按鈕? – LanternMike

+0

你是什麼意思的佈局資源? –

+0

不清楚,如果你已經設置了像R.layout.main這樣的佈局或者是讓按鈕出現的東西。一個xml佈局文件資源。 – LanternMike

回答

0

其實它比這更簡單。您需要設置按鈕的邊界。使用按鈕的.setBounds(x,y,width,height)方法,它們還沒有尺寸。設置邊界後,點擊區域應該是正確的。

看看Table layout libgdx並使用表格。

0

爲了您第一個問題,這是你的按鈕不會做任何事情,我認爲這是因爲該事件被傳播到所有的按鈕,可能是在他們已經被添加到Stage的順序。問題是您的touchDown()方法返回true。這意味着傳播應該停止,然後其他演員的方法不會被調用。 你覺得它什麼也沒做,但實際上它只是做一些事情,只不過是你的touchDown()方法是空的。

對於你第二個問題,這可能是因爲你沒有設置你的演員的大小。

相關問題