2017-01-26 2 views
-1

zu entwickeln Ich möchte mehrere unabhängige Texture Objekte mit Schlüsselwörtern darauf von oben nach unten fallen und eine Tastatur unten angezeigt, um den Buchstaben auf dem Texturobjekt eingeben, um es zu erfassen und zu generieren neue Objekte wiederholt gegebenen Zeitintervall ich habe durch die Wiki Code um Hilfe weg, aber wenn ich versuche, die Worte auf den Textur Objekte, die sie den Buchstaben angezeigt werden auf dem BatchIch versuche, Tippspiel mit libgdx in Android

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

eine Zeichenfolge mehr Orte geteilt über so Ihren gesamten Text gleichzeitig geändert. – Aryan

Antwort

0

nun auf jedem Texture Objekt zu ändern, der Code, in dem es aussieht, als ob du die Buchstaben zeichnest, ist auskommentiert, aber ich nehme an, das ist es, was du verwenden willst.

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

Was bedeutet dies: Für jedes Objekt im Regentropfen-Array zeichnen Sie die BeschriftungString. Also, wenn Sie 20 Objekte in Regentropfen und captionString = "a" haben, dann werden Sie den Buchstaben "a" 20 mal zeichnen.

Sie verwenden eine einzelne Zeichenfolge, um 20 Zeichenfolgen darzustellen. Ein String kann nur einen einzelnen Wert enthalten. Der letzte Wert, den Sie captionString geben, ist also der einzige Wert, den Sie anzeigen können. Sie ändern den Wert in der Schleife nicht.

Jedes Regentropfenobjekt muss einen eigenen Zeichenfolgenwert haben. Und dieser Wert ist derjenige, den Sie zeichnen müssen, nicht captionString.

[Bearbeiten]

Sieht aus wie Sie bereits in diesem Ort in haben. Benutze einfach die richtige Zeichenfolge.

Versuche:

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