有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

This is a simple 安卓 game with a bucket and raindrop. The game is about catching a raindrop into the bucket. The problem is with the bucket because the position of the bucket is not corresponding to the position of touch input.

The second problem is because I don't know how to insert a small window with raindrops counter. I need the counter with a number of raindrops I caught into the bucket.

Here is the code of my 安卓 game:

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

Touch input hendling:

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



        }

I know that my solution is with Vector3 but don't know exactly how? When I tried to play the game my touch position is not in a real position!

        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) 个答案

  1. # 1 楼答案

    你忘了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();