2012-11-09 3 views
7

내 Libgdx 게임 내 수준의 현재 렌더링 방법입니다. 내 레벨의 오른쪽 상단에 BitmapFont를 그려 넣으려고하고 있지만 모두 흰색 상자가 잔뜩 있습니다. 그때 배우의 일종으로 점수 글꼴을 추가하고 싶습니다libgdx의 스테이지에서 bitmapfont를 그리는 방법은 무엇입니까?

@Override 
    public void render(
      float delta) { 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

     this.getBatch().begin(); 

      //myScore.getCurrent() returns a String with the current Score 
     font.draw(this.getBatch(), "Score: 0" + myScore.getCurrent(), 600, 500); 
     stage.act(Gdx.graphics.getDeltaTime()); 
     stage.draw(); 
     this.getBatch().end(); 
     } 

는 scene.addActor (myScore)를 수행하지만 할 방법을 모르겠어요. 나는 Steigert의 튜토리얼을 따라이 레벨에 의해 확장 된 AbstractLevel 클래스에서 scene, font를 인스턴스화하는 메인 게임 클래스를 만들었다.

지금까지 사용자 지정 글꼴을 사용하지 않고 비어있는 새로운 BitmapFont() 만 사용했습니다. Arial 글꼴을 기본값으로 사용합니다. 나중에 나는 더 멋진 글꼴을 사용하고 싶습니다.

답변

12

font.draw를 stage.draw 다음으로 이동해보십시오. 배우에 추가하는 것은 단지 새로운 클래스를 생성하고이 도움이

import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.scenes.scene2d.Actor; 

public class Text extends Actor { 

    BitmapFont font; 
    Score myScore;  //I assumed you have some object 
         //that you use to access score. 
         //Remember to pass this in! 
    public Text(Score myScore){ 
     font = new BitmapFont(); 
      font.setColor(0.5f,0.4f,0,1); //Brown is an underated Colour 
    } 


    @Override 
    public void draw(SpriteBatch batch, float parentAlpha) { 
     font.draw(batch, "Score: 0" + myScore.getCurrent(), 0, 0); 
     //Also remember that an actor uses local coordinates for drawing within 
     //itself! 
    } 

    @Override 
    public Actor hit(float x, float y) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

같은 희망처럼 배우를 확장 매우 간단 할 것입니다!

편집 1 : 또한 시도해보십시오. 문제가되지 않습니다. 부동 소수점 또는 int를 반환 할 수 있습니다. "Score:"+ 비트를 사용하면 문자열 자체가됩니다.

+1

감사합니다! 이것은 트릭을 할 수도 있지만 ** Label이라는 **라는 멋진 유틸리티를 발견했습니다. 나는 이제 Label을 내 Score로 확장하고 정규 배우로 무대에 추가했습니다. 매력처럼 작동합니다. –

3

글쎄,이 경우 먼저 this.getBatch().end으로 전화해야 할 수도 있습니다. 이와 같이 :

mSpriteBatch.begin(); 
mStage.draw(); 
mSpriteBatch.end(); 
//Here to draw a BitmapFont 
mSpriteBatch.begin(); 
mBitmapFont.draw(mSpriteBatch,"FPS",10,30); 
mSpriteBatch.end(); 

나는 왜 그런지 모르지만 그것은 나를 위해 작동합니다.

-2

Scene2D를 사용하는 경우 스테이지 및 액터를 사용해보십시오. 긴 코드를 작성할 필요가 없으며 사용하기 쉬운 MTX 플러그인을 확인하십시오. 당신은 멋진 사용자 경험

http://moribitotechx.blogspot.co.uk/

1

내가 그리는 단계를 시작하기 전에 배치를 닫아 흰색 상자와 비슷한 문제를 해결 한을 만들 수 있습니다. 이는 stage.draw()가 다른 배치를 시작하고 end()로 끝나지 않은 이전 배치를 무효화하기 때문입니다. .

그래서 현재의 예에서 나는 무대를 그리기 전에 this.getBatch() 종료() 이동합니다 :

... 
    font.draw(this.getBatch(), "Score: 0" + myScore.getCurrent(), 600, 500); 
    this.getBatch().end(); 
    stage.act(Gdx.graphics.getDeltaTime()); 
    stage.draw(); 
    } 
관련 문제