尝试在android中使用libgdx开发打字游戏

时间:2017-01-26 07:01:06

标签: android libgdx

我想创建多个独立的Texture对象,其上面的关键词从上到下落下,并在底部显示一个键盘,在纹理对象上键入字母以捕获它并重复生成新对象时间间隔我已经通过wiki的代码寻求帮助,但当我试图在纹理对象上显示单词时,他们会更改Texture上每个Batch对象上的字母

package com.example.jtech.bubbletypinggame;

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.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
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 BubbleTypingGame extends ApplicationAdapter {
    private Texture [] dropImage = new Texture[5];
    private Texture bucketImage;
    private Texture background;
    private Sound dropSound;
    private Music rainMusic;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private Rectangle bucket;
    private Array<CustomRectangle> raindrops;
    private Array<String> rainKeyWords;
    private Sprite mySprite;
    CustomRectangle raindrop;

    private long lastDropTime;

    String[] chars = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    int i = 0;
    String captionString;
    private BitmapFont font;
    int j = 0;
    int x = 10, y = 740;


    @Override
    public void create() {

        font = new BitmapFont();
        font.setColor(Color.RED);


        //load backgroud image for the game
        background = new Texture("background_nebula.jpg");

        // load the images for the droplet and the bucket, 64x64 pixels each
        dropImage [0] = new Texture(Gdx.files.internal("balloon_a.png"));
        dropImage [1] = new Texture(Gdx.files.internal("balloon_b.png"));
        dropImage [2] = new Texture(Gdx.files.internal("balloon_c.png"));
        dropImage [3] = new Texture(Gdx.files.internal("balloon_d.png"));
        dropImage [4] = new Texture(Gdx.files.internal("balloon_e.png"));

        // 25JANwORKING//


        //25JANWORKING//

        bucketImage = new Texture(Gdx.files.internal("bucket.png"));

        // load the drop sound effect and the rain background "music"
        dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
        rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));

        // start the playback of the background music immediately
        rainMusic.setLooping(true);
        rainMusic.play();

        // create the camera and the SpriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);
        batch = new SpriteBatch();

        // create a Rectangle to logically represent the bucket
        bucket = new Rectangle();
        bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
        bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
        bucket.width = 64;
        bucket.height = 64;


        // create the raindrops array and spawn the first raindrop
        raindrops = new Array<CustomRectangle>();
        rainKeyWords = new Array<String>();
        spawnRaindrop();

    }


    private void spawnRaindrop() {
        raindrop = new CustomRectangle();
        raindrop.x = MathUtils.random(0, 800-100);
        raindrop.y = 480;
        raindrop.width = 100;
        raindrop.height = 50;
        raindrop.keyWord = "k";

        raindrops.add(raindrop);
        lastDropTime = TimeUtils.nanoTime();
        if(i==chars.length){
            i=0;
        }
        captionString = chars[i];
        rainKeyWords.add(captionString);
        i++;

    }

    private void displayKeyWord(){
        if(i==chars.length){
            i=0;
        }
        captionString = chars[i];
        rainKeyWords.add(captionString);
        lastDropTime = TimeUtils.nanoTime();
        i++;
    }

    @Override
    public void render() {
        // clear the screen with a dark blue color. The
        // arguments to glClearColor are the red, green
        // blue and alpha component in the range [0,1]
        // of the color to be used to clear the screen.
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // tell the camera to update its matrices.
        camera.update();

        // tell the SpriteBatch to render in the
        // coordinate system specified by the camera.
        batch.setProjectionMatrix(camera.combined);

        // begin a new batch and draw the bucket and
        // all drops
        batch.begin();

        if(j==5){
            j=0;
        }

        batch.draw(background,0,0);
        batch.draw(bucketImage, bucket.x, bucket.y);
        batch.draw(dropImage[j], raindrop.x, 400);

        /*for(Rectangle raindrop: raindrops) {
            batch.draw(dropImage[i], raindrop.x, raindrop.y);
            //font.draw(batch, captionString, raindrop.x+45, raindrop.y+30);
        }*/

        batch.end();

        /*// process user input
        if(Gdx.input.isTouched()) {
            Vector3 touchPos = new Vector3();
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(touchPos);
            bucket.x = touchPos.x - 64 / 2;
        }
        if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
        if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();

        // make sure the bucket stays within the screen bounds
        if(bucket.x < 0) bucket.x = 0;
        if(bucket.x > 800 - 64) bucket.x = 800 - 64;

        // check if we need to create a new raindrop*/
        if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();



        // move the raindrops, remove any that are beneath the bottom edge of
        // the screen or that hit the bucket. In the later case we play back
        // a sound effect as well.
        Iterator<CustomRectangle> iter = raindrops.iterator();
        while(iter.hasNext()) {
            Rectangle raindrop = iter.next();
            raindrop.y -= 100 * Gdx.graphics.getDeltaTime();
            if(raindrop.y + 64 < 0) iter.remove();
            if(raindrop.overlaps(bucket)) {
                dropSound.play();
                iter.remove();
            }
        }
    }

    @Override
    public void dispose() {
        // dispose of all the native resources
        dropImage[j].dispose();
        bucketImage.dispose();
        dropSound.dispose();
        rainMusic.dispose();
        batch.dispose();
    }
}

1 个答案:

答案 0 :(得分:0)

现在,看起来像你绘制字母的代码被注释掉了,但我认为这是你打算使用的。

for(Rectangle raindrop: raindrops) {
        batch.draw(dropImage[i], raindrop.x, raindrop.y);
        font.draw(batch, captionString, raindrop.x+45, raindrop.y+30);
}

这意味着:对于raindrops数组中的每个对象,绘制captionString。因此,如果雨滴中有20个对象,并且captionString =&#34; a&#34;,那么你将绘制字母&#34; a&#34; 20次。

您使用单个字符串表示20个字符串。 String只能包含单个值。因此,您给captionString的最后一个值是您可以使用它显示的唯一值。你不能改变循环中的值。

每个雨滴对象都需要拥有自己的字符串值。这个值是你需要绘制的值,而不是captionString。

[编辑]

看起来你已经有了这个。只需使用正确的字符串。

尝试:

for(Rectangle raindrop: raindrops) {
        batch.draw(dropImage[i], raindrop.x, raindrop.y);
        font.draw(batch, raindrop.keyWord, raindrop.x+45, raindrop.y+30);
}