簡體   English   中英

Libgdx矩形檢測觸摸

[英]Libgdx Rectangle detect Touch

我想檢測用戶是否單擊了駱駝並增加了錢,但是它不起作用(如果單擊(移動的)駱駝,什么也不會發生)。 我沒有在互聯網上找到解決方案。

我在render()中添加了這個:

    if(Gdx.input.justTouched()){

        for (Lama lama : lamas) {
            if(lama.lamarect.contains(Gdx.input.getX(), Gdx.input.getX())){
                money+=100;
            }else{
                Gdx.app.setLogLevel(Application.LOG_DEBUG);
                Gdx.app.debug("POSITION", "X touched: " + Gdx.input.getX() + " Y touched: " + Gdx.input.getY());

            }
        }

    }

編輯:

@Override
public void create() {
spawnLama();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/lamination.pack"));
    animation = new Animation(1/15f, textureAtlas.getRegions());
camera = new OrthographicCamera(1280, 720);

...

private void spawnLama() {

    Lama lama = new Lama();

    lama.x = MathUtils.random(-1000, -200);
    lama.y = MathUtils.random(-350, 100);
    lama.speedx = MathUtils.random(-5, 5);
    if(lama.speedx >= -1 && lama.speedx <=1){
        lama.speedx = 2;
    }
    lama.speedy = MathUtils.random(-5, 5);
    if(lama.speedy >= -1 && lama.speedy <=1){
        lama.speedy = 2;
    }

    Rectangle livinglama = new Rectangle();

    livinglama.x = lama.x;
    livinglama.y =  lama.y;
    livinglama.width = 64;
    livinglama.height = 64;


    lama.lamarect = livinglama;
lamas.add(lama);
    lastLamaTime = TimeUtils.nanoTime();
}


@Override
    public void render() {
        Gdx.gl.glClearColor(110 / 255F, 211 / 255F, 43 / 255F, 1 / 255F);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(camera.combined);

        elapsedTime += Gdx.graphics.getDeltaTime();
        if(spawnlama == true){
            spawnLama();
            spawnlama = false;
        }

        for (Lama lama : lamas) {
            if(lama.x <= -1000){
                lama.speedx = -lama.speedx;
            }
            else if(lama.x >= -200){
                lama.speedx = -lama.speedx;
            }

            if(lama.y <= -350){
                lama.speedy = -lama.speedy;
            } else if(lama.y >=100){
                lama.speedy = -lama.speedy;
            }

            if(lama.x == -500){
                if(lama.speedx < 0){
                    lama.speedx --;
                }
                if(lama.speedx >= 0){
                    lama.speedx++;
                }
            }
            lama.move();
        }


        if(Gdx.input.justTouched()){
            for (Lama lama : lamas) {
                Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
                camera.unproject(touch);
                if(lama.lamarect.contains(touch.x, touch.y)){
                    money+=100;
                }else{
                    Gdx.app.setLogLevel(Application.LOG_DEBUG);
                    Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
                }
            }
        }



       batch.begin();



        for (Lama lama : lamas) {
            TextureRegion                   currentFrame;
            currentFrame = animation.getKeyFrame(elapsedTime, true);
            if(!currentFrame.isFlipX()) {
                if (lama.speedx >= 0) {
                    currentFrame.flip(true, false);
                }
            }else{
                if (lama.speedx < 0) {
                    currentFrame.flip(true, false);
                }
            }


            batch.draw(currentFrame, lama.x, lama.y, currentFrame
                            .getRegionWidth(), currentFrame.getRegionHeight());

        }

        elapsedTime += Gdx.graphics.getDeltaTime();

喇嘛班:

public class Lama {
    public int x, y, speedx, speedy;
    public Rectangle lamarect;
 //   float delay = 1; // seconds

    void move() {
        x += speedx;
        y += speedy;

        lamarect.x = x;
        lamarect.y = y;

    }


}

Gdx.input.getXGdx.input.getY給出屏幕坐標,這些需要轉換回世界坐標。 如果使用的是相機,則可以使用camera.unproject(touchVector)如果使用的舞台沒有指定的相機,則可以通過stage.getCamera()獲取相機。

如果沒有,則必須自己將其轉換回原處,我認為您必須先翻轉y坐標,然后將其添加到游戲世界中要查看的左下角。 但是,通過安裝相機,您可以使生活更輕松。

if(Gdx.input.justTouched()){
        Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touch);
        for (Lama lama : lamas) {
            if(lama.lamarect.contains(touch.x, touch.y)){
                money+=100;
            }else{
                Gdx.app.setLogLevel(Application.LOG_DEBUG);
                Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
            }
        }
    }

否則,您將在contains方法中傳遞Gdx.input.getX()兩次。

暫無
暫無

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

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