2015-08-13 103 views
0

我正在使用Screen-2D来构建按钮。我想给按钮一个函数,当它是点击一个精灵将被绘制我怎么能做到这一点。这不是我所有的代码,但足以显示我在说什么。如何将绘图功能添加到libgdx中的按钮?

public void create() { 
    buttonStyle = new TextButtonStyle(); 
    buttonStyle.up = skin.getDrawable("button"); 
    buttonStyle.over = skin.getDrawable("buttonpressed"); 
    buttonStyle.down = skin.getDrawable("buttonpressed"); 
    buttonStyle.font = font; 
    button = new TextButton("START", buttonStyle); 

    stage.addActor(button); 
    Gdx.input.setInputProcessor(stage); 
    button.addListener(new InputListener() {  
     @Override 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      drawTile(200,50); 
      return true; 
     } 
    }); 
} 

// method used to draw a sprite when passing certain coordinates 
public void drawTile(int x , int y) {  
    spriteBatch.draw(sprite, x , y ); 
} 

public void render() { 
    Gdx.gl.glClearColor(1f, 0f, 0f, 1f); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    camera.update(); 
    spriteBatch.begin(); 
    spriteBatch.draw(background, 0, 0); 
    drawGrid(); 
    spriteBatch.draw(startButton, 0, 0); 
    stage.draw(); 
    spriteBatch.end() 
} 

回答

0

我认为你需要阅读有关LibGDX和Scene2D如何工作的详细教程:事件处理您的渲染方法之前完成,因此任何绘图将被删除,当你清晰的屏幕。

正确的做法是在点击触发时将精灵(作为Drawable)添加到控件组。然后阶段渲染将呈现你的所有组件,包括你的精灵。

比较MVC模式:舞台是你的模型,当事件发生时你修改你的模型,渲染方法是你的模型的视图(绘制你的模型)。

相关问题