2016-08-24 1 views
0

여러 단어 (무작위로 생성됨)가 화면 상단에 나타나고 아래쪽으로 떨어지도록하려고합니다. 이것은 글꼴이 화면에 여러 번 그려 짐을 의미합니다. 그러나 새로운 단어가 등장 할 때마다 그 단어는 이전 단어와 다른 무언가로 바뀌어야합니다. 이 방법이 효과적이지만 어떤 이유로 든 첫 단어가 바뀝니다. 여기 LibGDX : 한 글꼴을 다른 텍스트로 여러 번 그립니다.

내 코드입니다 :

PlayState 클래스 :

public class PlayState extends State { 

// TODO: add a background 

private BitmapFont font; 
private Word word; 

public PlayState(GameStateManager gsm) { 
    super(gsm); 

    cam.setToOrtho(false, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2); 

    // TODO: change the font of the random word 

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

    word = new Word(); 
} 

@Override 
protected void handleInput() { 

} 

@Override 
public void update(float dt) { 
    handleInput(); 
} 

@Override 
public void render(SpriteBatch batch) { 

    // TODO: make it so it changes the next word that spawns 

    // Check how much time has passed since a new word has been spawned 
    // and create a new one if necessary 
    if (TimeUtils.nanoTime() - word.lastWordTime > 1000000000) { 
     word.spawnWord(); 
    } 

    Iterator<Rectangle> iter = word.words.iterator(); 
    while (iter.hasNext()) { 
     Rectangle word = iter.next(); 
     word.y -= 200 * Gdx.graphics.getDeltaTime(); // move the words at a constant speed of 200 pixels/units per second 

     // If the word reaches the bottom, remove it from the array 
     if (word.y + 64 < 0) { 
      iter.remove(); 
     } 
    } 

    batch.setProjectionMatrix(cam.combined); 

    batch.begin(); 
    for (Rectangle word1 : word.words) { 
     font.draw(batch, word.getWordString(), word1.x, word1.y); 
    } 
    batch.end(); 
} 

@Override 
public void dispose() { 
    font.dispose(); 
} 
} 

말씀 클래스 :

public class Word { 

public Array<Rectangle> words; 
public long lastWordTime; 

private FileHandle file, file2; 
private BufferedReader reader, reader2; 
private List<String> lines, lines2; 
private String line, line2; 
private Random random; 
private String wordString; 

public Word(){ 
    words = new Array<Rectangle>(); 
    spawnWord(); 
} 

// Set the new Rectangle to a random position at the top of the screen 
// and adds it to the words array 
public void spawnWord() { 
    Rectangle word = new Rectangle(); 
    word.x = MathUtils.random(0, Gdx.graphics.getWidth()/2 - 64); 
    word.y = Gdx.graphics.getHeight(); 

    words.add(word); 
    lastWordTime = TimeUtils.nanoTime(); 

    // Read the file and put it into a list of strings 
    file = Gdx.files.internal("words/wordsEn.txt"); 
    file2 = Gdx.files.internal("words/swearWords.txt"); 
    reader = new BufferedReader(file.reader()); 
    reader2 = new BufferedReader(file2.reader()); 
    lines = new ArrayList<String>(); 
    lines2 = new ArrayList<String>(); 

    try { 
     line = reader.readLine(); 
     line2 = reader2.readLine(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    while (line != null && line2 != null){ 
     lines.add(line); 
     lines2.add(line2); 

     try { 
      line = reader.readLine(); 
      line2 = reader.readLine(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    // Choose a random string from the list 
    random = new Random(); 
    wordString = lines.get(random.nextInt(lines.size())); 

    // Filter out bad words 
    if (lines2.contains(wordString)) { 
     wordString = lines.get(random.nextInt(lines.size())); 
    } 
} 

public String getWordString(){ 
    return wordString; 
} 
} 

나는 내가 PlayState 클래스의 렌더링 방법으로 좁혀 생각 :

batch.begin(); 
    for (Rectangle word1 : word.words) { 
     font.draw(batch, word.getWordString(), word1.x, word1.y); 
    } 
batch.end(); 

getWordString() 메서드에서 올바른 값을 가져 오지만 새로 나타나는 단어 대신 화면의 글꼴과 관련된 모든 사항을 변경합니다.

편집 :

스네의 예를 구현하기 위해 노력하고 후 : 내가 말한다 words.add(wordRectangle.word)에 오류를 얻고있다

public void spawnWord() { 
    wordRectangle.word.x = MathUtils.random(0, Gdx.graphics.getWidth()/2 - 64); 
    wordRectangle.word.y = Gdx.graphics.getHeight(); 

    words.add(wordRectangle.word); 
    lastWordTime = TimeUtils.nanoTime(); 

을 "사각형이 WordRectangle로 변환 할 수 없습니다." 나는 잘못된 것들을 추가하고 있다고 생각합니다.

+1

word.getWordString()은 wordString에 대한 참조입니다. 한 곳에서 wordString을 변경하면 다른 모든 곳에 반영됩니다. – Sneh

답변

0

이것은 그리기 호출에서 참조하는 변수 wordString을 업데이트하기 때문입니다. 따라서 값을 업데이트하면 모든 곳에서 반영됩니다.

이 문제를 해결하려면 사각형과 단어를 별도의 클래스로 유지해야합니다.

public class WordRectangle { 

    public String word; 

    public Rectangle rectangle; 

} 

지금 워드 클래스에 위 클래스의 컬렉션을 가지고 새로운 단어를 생성 할 때 다음 컬렉션에 새 개체를 추가 할 수 있습니다. 당신의 렌더링 방법

batch.begin(); 
    for (WordRectangle word1 : word.words) { 
     font.draw(batch, word1.word, word1.rectangle.x, word1.rectangle.y); 
    } 
batch.end(); 

에서 다음

는 또한 당신을 위해 일 우리의 선택을 취소 this 유용한 기사를 읽어 제안합니다.

+0

기사를 읽고 솔루션을 사용해 보았습니다. 그러나 render 메서드에 오류가 발생했습니다. 즉, 'WordRectangle'은 'Rectangle'이어야합니다. 나는 무슨 일이 일어나고 있는지 완전히 이해하지 못한다. – GeeSplit

+0

단어 사각형에 rectange를 저장하려고 할 수 있습니다. 내가 말했듯이 당신은 Array 을 사용하고 WordRectangle의 내부와 객체를 추가 한 다음 마지막으로 컬렉션에 사각형과 단어를 추가해야합니다. – Sneh

+0

나는 그 부분을 고의적으로 남겨 두었다. – Sneh

관련 문제