LibGDX - 只有可拖動的運動 (LibGDX - only draggable movement)


問題描述

LibGDX ‑ 只有可拖動的運動 (LibGDX ‑ only draggable movement)

:D 我目前正在為 Android 創建我的第一個 LibGDX 遊戲。而且我遇到了一個關於我的小玩家(這是一個桶;))的運動的問題。我想讓桶(播放器)只能在 x 軸上拖動,而不能點擊來改變位置。y 軸不會成為問題,因為桶只能改變 x 軸上的位置。所以我基本上想讓桶只能拖動。抱歉我的寫作,英語不是我的母語。

這是我的代碼中的代碼片段,運動在其中運行:

    if (Gdx.input.isTouched()){
    Vector3 touchPos = new Vector3();
    touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);

    //Check the values
    float touchPosX = Gdx.input.getX();
    float touchPosY = Gdx.input.getY();
    String touchXString = Float.toString(touchPosX);
    String touchYString = Float.toString(touchPosY);
    Gdx.app.log("Movement ‑ X", touchXString);
    //Gdx.app.log("Movement ‑ Y", touchYString);
    Gdx.app.log("Movement ‑ BucketX", bucketXString);
    //Gdx.app.log("Movement ‑ BucketY", bucketYString)
    camera.unproject(touchPos);
    bucket.x = touchPos.x ‑ 64 / 2;;}

代碼有點亂……


參考解法

方法 1:

If I understand correctly, what you want is that the bucket only moves when you are dragging it, and right now what it does is move to whatever "x" you clicked/taped, even if that "x" is far from the bucket, thus "teleporting" it.

To do this, first have a boolean lets say, "btouched".

boolean btouched = false;

Then you will have to use an Input Processor (instead of InputPolling). to check in the touchdown event if the click/tap was inside the bucket, then only if it was, it will move in the drag event:

MyInputProcessor inputProcessor;

public class MyInputProcessor implements InputProcessor{

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button){
    camera.unproject(touchPos.set(Gdx.input.getX(pointer), Gdx.input.getY(pointer), 0));
    if(bucket.contains(touchPos)){
        btouched = true;
    }else{
        btouched = false;
    }
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer){
    camera.unproject(touchPos.set(Gdx.input.getX(pointer), Gdx.input.getY(pointer), 0));
    if(btouched){
        bucket.x = touchPos.x ‑ 64 / 2;
    }
    return false;
}

    ...
}

Dont forget to set the input processor:

inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);

(by Andreas JohanssonLestat)

參考文件

  1. LibGDX ‑ only draggable movement (CC BY‑SA 3.0/4.0)

#game-physics #libgdx #Android






相關問題

Android AndEngine:簡單的精靈碰撞 (Android AndEngine: Simple sprite collision)

Box2D - 收集硬幣 (Box2D - collect a coin)

LibGDX - 只有可拖動的運動 (LibGDX - only draggable movement)

Swift 2 中的遊戲 - “touchesBegan”? (Game in Swift 2 - "touchesBegan"?)

SKCameraNode 跟不上移動節點 (SKCameraNode doesn't keep up with moving node)

2D 平台遊戲:為什麼讓物理依賴於幀率? (2D platformers: why make the physics dependent on the framerate?)

基於脈衝的物理 - 在輕物體上堆疊重物體 (Impulse based physics - Stacking heavy object on light object)

建造一堵提高速度統一的牆 (Make a wall that boost speed unity)

Unity Golf 遊戲物理和運動控制器 (Unity Golf game physics and movement controller)

在 2D 汽車遊戲中模擬下壓力 (Simulate Downforce in a 2D Car Game)

向前和橫向球員運動 (Forward and Sideways Player movement)

如何在 Unity 中進行沒有物理的碰撞檢測? (How to have collision detection without physics in Unity?)







留言討論