简体   繁体   English

在 Android Studio Gdx 库上将图像设置为按钮

[英]Set an image as button on Android Studio Gdx library

I am trying to put a button using a play image.我正在尝试使用播放图像放置一个按钮。 I have followed some tutorials and cannot find the documentation to complete my project.我遵循了一些教程,但找不到完成我的项目的文档。 The game is actually done, and I just need to connect the gameover screen to the game screen so I can replay the game by pushing the play image.游戏实际上已经完成,我只需要将游戏结束屏幕连接到游戏屏幕,这样我就可以通过推送播放图像来重播游戏。 I got stuck in this longer than I expected.我陷入困境的时间比我预期的要长。 I cannot change the methods because I will mess up my code.我无法更改方法,因为我会弄乱我的代码。 Code and images below.下面的代码和图像。

package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;

public class GameOverScreen extends  BaseScreen {

    private Image background;
    private Stage stage;
    private Image gameOver;
    private Image replay;

    public GameOverScreen(final MainGame game) {
        super(game);
        stage = new Stage(new FitViewport(640, 320));
        //Preparing actors
        background = new Image(game.getManager().get("bg.png", Texture.class));
        gameOver = new Image(game.getManager().get("gameover.png", Texture.class));
        replay = new Image(game.getManager().get("plybtn.png", Texture.class));

        replay.addCaptureListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                game.setScreen(game.gameScreen);

            }
        });

        //Giving their positions and sizes
        background.setPosition(0,0);
        gameOver.setPosition(320 - gameOver.getWidth()/2, 180 - gameOver.getHeight()/2);
        replay.setPosition(320 - replay.getWidth()/2, 60 -replay.getHeight()/2);
        //Set them in stage
        stage.addActor(background);
        stage.addActor(gameOver);
        stage.addActor(replay);

    }


    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }

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

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.4f, 0.5f, 0.8f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
    }
}

[This is the screen that I get, it does not change the screen when I touch the button][1]


  [1]: https://i.stack.imgur.com/GxVLM.png

Update: This is the solution to my problem in case you need it.更新:如果您需要,这是我问题的解决方案。

// Declare statements at the beginning of the class 
  private Texture myTexture;
    private TextureRegion myTextureRegion;
    private TextureRegionDrawable myTexRegionDrawable;
    private ImageButton pbutton;


    // Make a texture out of the image in the MenuScreen constructor         
      myTexture = new Texture(Gdx.files.internal("plybtn.png"));
        myTextureRegion = new TextureRegion(myTexture);
        myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
        pbutton = new ImageButton(myTexRegionDrawable);

The event doesn't look like its being received.该事件看起来不像它被接收。 Change改变

replay.addCaptureListener(new ChangeListener() {

to

replay.addListener(new ChangeListener() {

or even use ClickListener instead.或者甚至使用ClickListener代替。 CaptureListener is to intercept events to disable them, not for handling and you need to return true in the capture handler to not break event handling down the tree. CaptureListener是拦截事件以禁用它们,而不是用于处理,您需要在捕获处理程序中返回 true 以不中断树下的事件处理。

https://gamedev.stackexchange.com/questions/151505/what-is-the-difference-between-a-listener-and-a-capturelistener-in-libgdx-scene2 https://gamedev.stackexchange.com/questions/151505/what-is-the-difference-between-a-listener-and-a-capturelistener-in-libgdx-scene2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM