0

我目前正在尝试显示需要文本在游戏中动态更改的分数。我四处搜索,发现大多数情况下人们使用 XML 布局文本。我的问题是我根本不为游戏使用 XML,因为一切都是位图图形。对我的情况有什么提示或建议吗?

这是绘制所有内容的draw方法

public void render(Canvas canvas){
    Bitmap bitmap;
    Graphics.Coordinate coords;
    canvas.drawBitmap(bgBitmap, 0, 0, null);
    canvas.drawBitmap(closeBtnBitmap, 700, 0, null);
    canvas.drawBitmap(groundBitmap, 0, 315, null);
    canvas.drawBitmap(petBitmap, petX, petY, null);
    for(Graphics pics : coins){
        bitmap = pics.getBitmap();
        coords = pics.getCoord();
        canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
    }
    canvas.drawBitmap(scoreBitmap, 300, 20, null);
    canvas.drawText(scoreString, 300, 20, null); //change null to paintObj
}

这是更新分数的方法

private void updateScore(int score){
    initScore += score;
    totalScore = initScore;
    scoreString = Integer.toString(totalScore);
}

它在 android.graphics.Canvas.drawText(Native Method) 处返回 NullPointerException。我尝试记录“scoreString”,它显示正确。

编辑:已解决,由 null 绘制对象引起的 NullPointerException。只需创建绘画对象Paint paintObj = new Paint();并设置对象paintObj.setTextSize(textSize)paintObj.setColor(Color.WHITE);

4

1 回答 1

2

If you are doing your drawing directly through a View or SurfaceView object, you may want to check the Canvas documentation:

http://developer.android.com/reference/android/graphics/Canvas.html

Specifically the draw text function. This is what I use.

http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String, float, float, android.graphics.Paint)

Enjoy!

If you're using an Open GL Surface, I'm not sure what APIs are available. On other platforms I've uploaded my characters as a texture atlas and just placed the textures for the correct text that I wanted on the scene.

于 2012-01-03T06:08:48.100 回答