Libgdx矩形检测触摸

时间:2016-01-29 12:35:37

标签: java onclick libgdx touch rectangles

我想检测用户是否点击了美洲驼并增加了钱,但是它没有工作(如果我点击(移动)美洲驼没有任何反应)。我没有在互联网上找到解决方案。

我在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;

    }


}

1 个答案:

答案 0 :(得分:1)

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);
            }
        }
    }

除此之外,您在Gdx.input.getX()方法中传递contains两次。