2014-09-10 75 views
0

我对此非常困惑。不明白的是舞台和桌面布局如何正确工作。我想要的只有3个按钮,它们会将我发送到另一个屏幕。有人能写出一个例子让我与之合作吗?这是我迄今为止的代码。LibGDX - 我如何添加一个简单的按钮?

public class Menu implements Screen { 
private SlingshotSteve game; 

private Stage stage; 
private TextButton button; 
private TextButtonStyle textButtonStyle; 
private BitmapFont font; 

{ 

stage = new Stage(new ExtendViewport(800, 840)); 
Gdx.input.setInputProcessor(stage); 

Table table = new Table(); 
table.setFillParent(true); 
table.center().center(); 
stage.addActor(table); 

font = new BitmapFont(); 
textButtonStyle = new TextButtonStyle(); 
textButtonStyle.font = font; 
button = new TextButton("This is a button!!!", textButtonStyle); 
stage.addActor(button); 

} 

// View Port Camera 
private Viewport viewport; 
PerspectiveCamera camera; 


public Menu(SlingshotSteve gam) { 
    this.game = gam; 

} 

@Override 
public void render(float delta) { 
    Gdx.gl.glClearColor(0, 0, 0.2f, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    stage.act(Gdx.graphics.getDeltaTime()); 
    stage.draw(); 

    camera.update(); 
    game.batch.setProjectionMatrix(camera.combined); 

    game.batch.begin(); 
    game.batch.end(); 


    if (Gdx.input.isTouched()) { 
     game.setScreen((Screen) new GameScreen(game)); 
     dispose(); 
    } 
} 

@Override 
public void resize(int width, int height) { 
    // View Port Camera 
    viewport.update(width, height); 

    stage.getViewport().update(width, height, true); 


} 

@Override 
public void show() { 

    // Viewport Camera 
    camera = new PerspectiveCamera(); 
    viewport = new FitViewport(800, 480, camera); 


} 

@Override 
public void dispose() { 
    stage.dispose(); 
} 

} 

回答

2

不要将按钮添加到Stage。请将其添加到您创建的Table

TextButton button1 = new TextButton("This is a button!!!", textButtonStyle); 
TextButton button2 = new TextButton("This is a button!!!", textButtonStyle); 
TextButton button3 = new TextButton("This is a button!!!", textButtonStyle); 

table.add(button1); 
table.row(); 
table.add(button2); 
table.row(); 
table.add(button3); 
table.row(); 
相关问题