簡體   English   中英

為什么我的點擊偵聽器無法正常工作?

[英]Why isn't my clicklistener working?

這是我的代碼

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;


public class MainMenuScreen extends SpaceInvaderScreen {
    SpriteBatch batch;
    TextureRegion background;
    Stage stage;
    TextButton playbutton;
    float time = 0;

    public MainMenuScreen(Game game){
        super(game);
    }

    @Override
    public void show(){
        batch = new SpriteBatch();
        batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        background = new TextureRegion(new Texture("data/cool.jpg"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        stage = new Stage(new FitViewport(1280, 1080));

        Skin uiskin = new Skin(Gdx.files.internal("data/skin/uiskin.json"));

        playbutton = new TextButton("Play", uiskin);
        playbutton.setWidth(200);
        playbutton.setHeight(100);
        playbutton.setPosition(980, 620);
        playbutton.setBounds(playbutton.getX(), playbutton.getY(),      playbutton.getWidth(), playbutton.getHeight());
        playbutton.addListener( new ClickListener() {              
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen( new GameScreen(game));
                return true;
            };
        });
        stage.addActor(playbutton);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.15f, 0.15f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(background, 0, 0);
        batch.end();
        stage.act();
        stage.draw();

        time += delta;

    }

    @Override
    public void hide(){
        batch.dispose();
        background.getTexture().dispose();
    }
}

我不確定clicklistener為何無法正常工作。 我也嘗試使用touchDown方法,但是它也不起作用。 抱歉,這聽起來像是一個愚蠢的問題,對我的圖書館來說還算是新事物

只需嘗試以下兩件事:我可以看到您正在show方法中構建按鈕。 為什么? 顯示任何內容之前 ,擁有一種設置方法並構建該應用程序/游戲/該設置方法中的任何內容的整個布局會更加有意義。 但這只是黑暗中的一槍,我不知道您使用的是這個框架,我可能會犯錯。

當您保留同一游戲的多個實例時,另一件事可能會引起問題! 注意僅一次構建游戲,然后傳遞該單個實例。 為什么這么重要? 因為在game.setScreen( new GameScreen(game));情況下您可能正在調用game.setScreen( new GameScreen(game)); 但是在錯誤的情況下!

請同時發布您的SpaceInvaderScreen ,以便於調試。

而且,無論如何,您是否嘗試調試touchDown方法? 真的叫嗎?

還有兩件事: ClickListener是一個類。 不是界面。 因此,當您覆蓋touchDown方法時,基本上就繞過了超類中的父代碼。

嘗試在達陣方法中作為第一行調用:

super.touchDown(event, x, y, pointer, button);

另外, ClickListener文檔中還說:

當返回true時,將處理事件(...),這不會影響scene2d內部的事件傳播,但會導致Stage事件方法返回true,這將占用事件,因此不會將其傳遞給舞台下的應用程序。

因此,您應該嘗試返回false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM