简体   繁体   English

libgdx Clicklistener不起作用,缺少什么?

[英]libgdx Clicklistener doesnt work, What is missing?

Im new with libgdx. 我是libgdx的新手。 Im going to code a android game. 我要编写一个Android游戏。 The Player is moving by itself, but if you touch the screen(or click with the mouse) the player will stop. 播放器本身在移动,但是如果您触摸屏幕(或用鼠标单击),播放器将停止。 I want to add a pause button. 我想添加一个暂停按钮。 It do render, but if I touch/click on the button the listener doesnt react, just the player stops. 它确实可以渲染,但是如果我触摸/单击按钮,则听众没有反应,只是播放器停止了。

Thats the class with the imporant methods about the button: 多数民众赞成在类与按钮的重要方法:

public class PlayState extends State {

private EntityManager entityManager;
private Player player;
private Texture background;
private Texture filter;
private BitmapFont font;
private Sound click, boost;
private Drug drug;
private Border border;
private House house;

public static final int FIELD_SIZE_HEIGHT = 1000;
public static final int FIELD_SIZE_WIDTH = 500;

private int collectedDrugs;
private boolean pause, renderLost;
private boolean boostSound;

private ImageButton button;
private Drawable drawable;
private Texture textureBtn;

public PlayState(GameStateManager gsm) {
    super(gsm);
    entityManager = new EntityManager();
    player = new Player(this, 100, 100);
    border = new Border();
    background = new Texture("bggame.png");
    filter = new Texture("filter.png");
    cam.setToOrtho(false, MinniMafia.WIDTH, MinniMafia.HEIGHT);
    click = Gdx.audio.newSound(Gdx.files.internal("Click.mp3"));
    boost = Gdx.audio.newSound(Gdx.files.internal("boost.mp3"));
    drug = new Drug();
    house = new House();
    collectedDrugs = 0;
    font = new BitmapFont();
    entityManager.addEntity(player);
    entityManager.addEntity(drug);
    entityManager.addEntity(border);
    entityManager.addEntity(house);

    pause = false;
    renderLost = false;
    boostSound = false;

    Stage stage;
    textureBtn = new Texture("pausebtn.png");
    drawable = new TextureRegionDrawable(new TextureRegion(textureBtn));
    button = new ImageButton(drawable);
    button.setPosition(cam.position.x + cam.viewportWidth/2 - 50, cam.position.y + cam.viewportHeight/2 - 50);
    button.addListener(new ClickListener() {
        @Override
        public void     clicked(InputEvent event, float x, float y)  {
            System.out.println("Button Pressed");
        }
    });
}

@Override
protected void handleInput() {
    if (Gdx.input.justTouched()) {
        click.play(0.2f);
    }
    if (Gdx.input.isTouched()) {
        player.setStop(true);
        boostSound = false;
    } else {
        if (!boostSound) {
            boost.play(0.2f);
            boostSound = true;
        }
        player.setStop(false);

    }

}

@Override
public void update(float dt) {
    if (!pause) {
        handleInput();
        entityManager.updateEntities(dt);
        setCam();
        button.setPosition(cam.position.x + cam.viewportWidth/2 - 60, cam.position.y + cam.viewportHeight/2 - 60);
        if (drug.collides(player.getBounds())) {
            entityManager.disposeEntity(drug);
            player.setGotDrug(true);
        }
        if (border.collides(player.getBounds()) && !border.isOpen()) {
            pause = true;
            renderLost = true;

        }
        if (house.collides(player.getBounds()) && player.isGotDrug()) {
            player.setGotDrug(false);
            collectedDrugs++;
            drug = new Drug();
            entityManager.addEntity(drug);
        }
    } else if (renderLost = true) {
        if (Gdx.input.isTouched()) {
            gsm.set(new MenuState(gsm));
            dispose();
        }
    }
}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(background, 0, 0);
    entityManager.renderEntities(sb);
    button.draw(sb, 10f);
    font.draw(sb, "" + collectedDrugs, cam.position.x - cam.viewportWidth / 2 + 10, cam.position.y - cam.viewportHeight / 2 + 20);
    if (renderLost) {
        sb.draw(filter, cam.position.x - cam.viewportWidth / 2, cam.position.y - cam.viewportHeight / 2);
        font.draw(sb, "LOST! SCORE:" + collectedDrugs, cam.position.x - 50, cam.position.y);
    }
    sb.end();
}

I suppose that there is missing sth, but I can not figure it out. 我想有某事失踪了,但我无法弄清楚。 Can sb help me please? 某人可以帮我吗?

LG bttl LG BTTL

You have to set your Stage as InputProcessor for Scene2d to work. 您必须将Stage设置为Scene2d的InputProcessor才能工作。 Documentation 文档

For using Stage and your own code as InputProcessor, add both to an InputMultiplexer and use this. 为了将Stage和您自己的代码用作InputProcessor,请将两者都添加到InputMultiplexer并使用它。

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

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