繁体   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