如何在Gdx安卓游戏编程中获得矩形的真实触摸位置?

时间:2018-06-13 22:14:23

标签: java android libgdx touch

  

这是一款带有水桶和雨滴的简单安卓游戏。游戏是关于将雨滴捕获到水桶中。问题在于铲斗,因为铲斗的位置与触摸输入的位置不对应。

     

第二个问题是因为我不知道如何插入带雨滴计数器的小窗口。我需要带有许多雨滴的柜台。我抓住了桶。

     

这是我的安卓游戏的代码:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import java.util.Iterator;


public class MyGdxGame extends ApplicationAdapter {

    private Texture dropImage;
    private Texture bucketImage;

    private Sound dropSound;
    private Music rainMusic;

    private OrthographicCamera camera;

    private SpriteBatch spriteBatch;

    private Rectangle bucket;
    private Array<Rectangle> raindrops;

    private long lastDropTime;




    @Override
    public void create () {
        dropImage = new Texture(Gdx.files.internal("droplet.png"));
        bucketImage = new Texture(Gdx.files.internal("bucket.png"));
        dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
        rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));

        rainMusic.setLooping(true);
        rainMusic.play();

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 400);

        spriteBatch = new SpriteBatch();

        bucket = new Rectangle();
        bucket.height = 64;
        bucket.width = 64;
        bucket.x = 800 / 2 - 64 / 2;
        bucket.y = 20;

        raindrops = new Array<Rectangle>();
        createRainDrops();


    }



    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1); 
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        spriteBatch.setProjectionMatrix(camera.combined);

        spriteBatch.begin();
        spriteBatch.draw(bucketImage, bucket.x, bucket.y);

        for (Rectangle raindrop: raindrops){
            spriteBatch.draw(dropImage, raindrop.x, raindrop.y);
        }
        spriteBatch.end();
  

触摸输入hendling:

if (Gdx.input.isTouched()){  
            bucket.x = Gdx.input.getX() - 64 / 2;
            bucket.x = Gdx.input.getX();



        }
  

我知道我的解决方案是使用Vector3但不知道具体如何?   当我尝试玩游戏时,我的触摸位置不是真正的位置!

        if (bucket.x < 0){
            bucket.x = 0;
        }
        if (bucket.y > 800 - 64){
            bucket.x = 800 - 64;
        }


        if (TimeUtils.nanoTime() - lastDropTime > 1000000000){
            createRainDrops();
        }

        Iterator<Rectangle> iterator = raindrops.iterator();
        while (iterator.hasNext()){
            Rectangle raindrop = iterator.next();
            raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
            if (raindrop.y + 64 < 0){
                iterator.remove();
            }
            if (raindrop.overlaps(bucket)){
                dropSound.play();
                iterator.remove();
            }

        }


    }

    @Override
    public void dispose () {
        dropSound.dispose();
        rainMusic.dispose();
        bucketImage.dispose();
        dropImage.dispose();
        spriteBatch.dispose();

    }

    private void createRainDrops() {
        Rectangle raindrop = new Rectangle();
        raindrop.width = 64;
        raindrop.height = 64;
        raindrop.y = 480;
        raindrop.x = MathUtils.random(0, 800 - 64);
        raindrops.add(raindrop);
        lastDropTime = TimeUtils.nanoTime();
    }


}

1 个答案:

答案 0 :(得分:0)

你忘记了y坐标:

if (Gdx.input.isTouched()){  
        bucket.x = Gdx.input.getX() - bucket.getWidth() / 2;
        bucket.y = Gdx.input.getY() - bucket.getHeight() / 2;
}

对于雨滴计数器,您可以使用BitmapFont

spriteBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("Font.fnt"),
Gdx.files.internal("Font.png"), false);

和渲染方法

spriteBatch.begin();
font.draw(spriteBatch, "some string", x, y);
spriteBatch.end();