libgdx按钮向上没有效果

时间:2017-06-17 17:01:25

标签: button libgdx kotlin

val playTexture = assetManager?.get(A.image.gamePlayBtn, Texture::class.java)

val skin: Skin = Skin()
skin.add("up", TextureRegion(playTexture, 0, 0, 296, 96))
skin.add("down", TextureRegion(playTexture, 0, 96, 296, 96))

val playButton: ImageTextButton = ImageTextButton("PLAY THE GAME", with(ImageTextButton.ImageTextButtonStyle()) {
    up = skin.getDrawable("up")
    down = skin.getDrawable("down")
    font = BitmapFont()
    font.data.setScale(3f)
    font.color = Color.ORANGE
    this
})

OnClick事件工作正常,但onClicked状态(向下)没有按钮背景更改。哪里我错了?

2 个答案:

答案 0 :(得分:1)

我已经测试了你的代码,这工作正常。添加了完整的文本代码:

class Main : ApplicationAdapter(){

   internal lateinit var stage: Stage

   override fun create() {

    stage= Stage()
    stage.setDebugAll(true)
    Gdx.input.setInputProcessor(stage)

    var skin= Skin()
    var tex1 =Texture("badlogic.jpg")
    skin.add("up", TextureRegion(tex1, 0, 0, 100, 50))
    skin.add("down", TextureRegion(tex1, 0, 96, 100, 50))

    var playButton: ImageTextButton = ImageTextButton("Play The Game", with(ImageTextButton.ImageTextButtonStyle()){

        up = skin.getDrawable("up")
        down = skin.getDrawable("down")
        font = BitmapFont()
        font.data.setScale(3f)
        font.color = Color.ORANGE
        this
    });

    playButton.setPosition(0F,100F)
    playButton.addListener(object : ClickListener(){

        override fun clicked(event: InputEvent?, x: Float, y: Float) {
            print("clicked")
            super.clicked(event, x, y)
        }
    })

    stage.addActor(playButton)
  }

  override fun render() {

      Gdx.gl.glClearColor(1f, 0f, 0f, 1f)
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

      stage.draw()
      stage.act()
  }
}

答案 1 :(得分:0)

您必须更新按钮。 有updateImage()函数,但它受保护,所以你必须使用Stage。

所以这就是你所做的:

  1. 创建舞台。
  2. 将您的按钮添加到舞台。
  3. 在渲染功能中调用stage.draw()。

    ...
    Stage stage = new Stage();
    //create button
    stage.addActor(playButton);
    ...
    
    
    @Override
    public void render() {      
        stage.draw();
    }