0

私は現在、Android開発とlibgdx/scene2d atmに関するいくつかのチュートリアルを行っていますが、toucheventsで行き詰まりました:

手始めに、ボードゲームを実装しています。ここまでで、ボードといくつかの meeple (プレイヤー トークン) を描画し、それらの位置を正しい座標に調整することができました。libgdx の使用を開始したとき、タッチ入力が機能しているかどうかを確認するための最初のテストも正常に実装しました。テストは次のようになりました。

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 を含むクラスに 1 つ追加したので、アクターが実際にタッチされた場合、これは 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();
    }
}

タッチイベントを機能させるために何が欠けているのかわかりません。だから私はあなたが私を助けてくれることを願っています:) よろしくお願いします!

4

1 に答える 1