1

这是一个非常简单的问题,我在任何地方都找不到它,也许我没有正确地问它。

我一直在搜索与 Google 和 stackOverflow 上的 TextView 和可变文本有关的内容

我使用andengine 制作了一个游戏,很像俄罗斯方块/宝石迷阵,游戏运行良好,我需要添加一个评分系统并希望将分数输出到屏幕上。

许多与 android 中的文本相关的问题都在谈论 TextView,但是当我使用它时,我会看到一个空白的白色屏幕,上面有我的文本。

我希望能够在整个游戏过程中创建标签、定位并定期更改文本。

我希望能够在不更改 xml 文件的情况下做到这一点,因为我不太擅长。谢谢你。

4

1 回答 1

1

在朋友的帮助下,我想出了如何做到这一点。我确信这是非常基本的,但这是我想要的方式,不使用 XML 文件,也没有可以随时更改的文本。

// Text related fields.
private ChangeableText mText;
private Font mFont;

在 OnLoadResources

public void onLoadResources() {

this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR);
this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);

//Don't forget to load the font (along with other images)
this.mEngine.getTextureManager().loadTextures(this.mTimerImage, this.mbackgroundImage,  this.mFontTexture);
this.mEngine.getFontManager().loadFont(this.mFont);

在 OnLoadScene 中

public Scene onLoadScene() 
{   

final Scene scene = new Scene();

//Initialise your other variables...

//Give your text a position on screen, font, and fill it with some text and character count
//I used it for a score to be shown.
mText = new ChangeableText(490, 800, GameActivity.this.mFont, "", 20);

scene.attachChild(mText);
return scene;
}

我将我的文本用于稍后在游戏中显示的可变分数,并且每次玩家的分数增加时,我都会调用 Score 方法。

private void Score(int value)
{
    playerScore += value;

    mText.setText(String.valueOf(playerScore));
}

这更新了每一帧的分数,因为我从我的更新方法中调用它。

于 2012-11-27T10:01:32.513 回答