2017-01-26 5 views
-1

複数の独立した単語を上から下に向けて作成するオブジェクトと、私は助けのためのwikiのコードを介して行っているが、私は、彼らがBatchandroidでlibgdxを使ってタイピングゲームを開発しようとしています

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(); 
    } 
} 
+0

あなたのすべてのテキストは、同じ時間を変更するように、複数の場所で共有する1つの文字列。 – Aryan

答えて

0

上のすべてのTextureオブジェクト上の文字を変更テクスチャオブジェクトの単語を表示しようとしていたときに与えられた時間間隔で繰り返し新しいオブジェクト、文字を描画するように見えるコードはコメントアウトされていますが、これはあなたが使用しようとしているものと仮定しています。

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

これは、雨滴アレイの各オブジェクトに対して、captionStringを描画することを意味します。したがって、雨滴に20個のオブジェクトがあり、captionString = "a"の場合、文字 "a"を20回描画します。

1つの文字列を使用して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); 
} 
関連する問題