簡體   English   中英

使用libgdx和scene2d的Touchevents

[英]Touchevents using libgdx and scene2d

我目前正在閱讀有關android開發和libgdx / scene2d atm的一些教程,但在touchevents上遇到了麻煩:

首先,我正在實施棋盤游戲。 到現在為止,我設法畫出了棋盤和一些麻將(玩家記號),並將它們的位置調整為正確的坐標。 當我開始使用libgdx時,我還成功實現了第一個測試,以查看touchinput是否正常工作。 測試看起來像這樣:

if(Gdx.input.isTouched()){
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);
        stone.x=touchPos.x - stone.width/2;
        stone.y=touchPos.y - stone.height/2;

到現在為止,由於一些非常方便的功能,我重新排列了我的代碼以使用scene2d。 但是,盡管我遵循了一些相當簡單的教程,但似乎無法設法使touchevents與scene2d一起使用。 由於其中一些參考了Scene2d的一些近期重大更改,我想知道這些教程是否過時了,希望你們能幫助我解決我的問題:)我將嘗試僅在此處粘貼代碼的相關部分,以提出一個最小的要求。我的非工作代碼示例。 當然,對於總體上如何“改進”我的代碼的提示,我也感到很高興,因為我仍在學習,並且可能在這里破壞了一些約定^^

讓我們從我的演員班開始:

//deleted: imports

public class Player extends Actor{
    private int xval; //x Position of the Player on the board
    private int yval; //y Position of the Player on the board
    private Color color;
    private TextureRegion playerTexture;
    //deleted: some variables that are unimportant for touchevents

    public Player(int x, int y, TextureRegion texture){
        //initializing some Variables with default values
            this.xval = x;
            this.yval = y;
            color = new Color(1,1,1,1);

            playerTexture = texture;
            this.setTouchable(Touchable.enabled); //this should be set by default, but i added it just to be sure.

        //setBounds seems to be necessary in addition to the "touchable" flag to be able to "grab" or "touch" Actors.
        //getX and getY are methods of the actor class which i do not overwrite. I use distinct getters and setters to modify the Variables xval and yval.
            setBounds(getX(), getY(), playerTexture.getRegionWidth(), playerTexture.getRegionHeight()); 

        //now i implement the InputListener. This doesnt seem to get called, since the "touch started" message doesn't appear in LogCat.
        //In Theory the testevent should move the meeple one space to the right. 
        //To do that i call event.getTarget() which returns the actor of which the TouchDown is called. I then cast it to my Actor class (Player) and set xval to xval+1.
            this.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
                Gdx.app.log("Example", "touch started at (" + x + ", " + y + ")");
                ((Player)event.getTarget()).setXf(((Player)event.getTarget()).getXf()+1);
                return true;
                }
            });
    }

    //This is my draw method, which just sets the color of my meeple and then draws it to its current position.
    public void draw(Batch batch, float alpha){
        batch.setColor(color);
        batch.draw(playerTexture, getX(), getY());      
    }

    //deleted: several getters and setters
}

因此,如果我做對了,則gdx inputProcessor將管理所有輸入。 如果我將Gdx InputProcessor設置到包含參與者的舞台,則在發生事件(例如touchDown)的情況下,它將調用舞台上所有參與者的inputProcessors。 因此,由於我剛剛在我的類中添加了一個包含touchDown的類,因此應該在實際接觸actor的情況下處理touchDown事件。 用來驗證由我的Player類中的語句“ setBounds”設置的復選框。

我在ApplicationListener類中實現了這一點:

//deleted: imports

public class QuoridorApplicationListener implements ApplicationListener{

    Stage stage;
    OrthographicCamera camera;
    //deleted: several variable declarations.       

    //deleted: constructor - this only fills some of my variables with values depending on game settings.

    @Override
    public void create() {
        //set stage - since "stage = new Stage(800,400,false)" doesn't seem to work anymore, i did set up a viewport manually. If you got suggestions how to this straightforward, please leave a note :)
        Gdx.app.log("Tag", "game started");
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);
        Viewport viewport = new Viewport() {};
        viewport.setCamera(camera);
        stage = new Stage(viewport);
        Gdx.input.setInputProcessor(stage); //like i described above, this should forward incoming touchevents to the actors on the stage.

        //deleted: setting textures, music, setting up the board (the boardtiles are actors which are added to the stage) 

        //deleted: setting TextureRegion, player colors, player positions and player attributes.
            for (int i = 0; i < playercount; i++){
                stage.addActor(players[i]);
            }
    }

    //deleted: dispose(), pause(), resume(), resize() methods.

    @Override
    public void render() {
        camera.update();

        for (int i=0; i< playercount; i++){
            players[i].setX(/*Formula to determine x-coordinates from Player.xval*/);
            players[i].setY(/*Formula to determine x-coordinates from Player.yval*/);
    } 

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
    }
}

我無法弄清楚要使touchevents正常工作所缺少的東西。 因此,我希望您能為我提供幫助:)提前謝謝!

我認為您的問題源於您手動創建的Viewport對象,該對象內部處理着未投影的對象。 代替

stage = new Stage(800,400,false);

你應該使用

stage = new Stage();

要在發生調整大小事件后將視口調整為正確的大小,您需要調用

stage.getViewport().update( newWidth, newHeight )

在您的ApplicationListener的resize方法中。

暫無
暫無

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

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