我一直试图为小型游戏制作一个分数计数器,用户点击舞台上的按钮,每次用户按下按钮时分数增加10,但我无法得到分数显示在动态文本字段。我做错了什么?
var score:uint;
//scoreCounter is the instance name of the dynamic text box
function updateScore():void{
score += 10;
scoreCounter.text = score.toString();
}
答案 0 :(得分:2)
很难说出你的问题在哪里,因为你确实没有发布足够的代码。确保您的方法看起来像这样。此代码已经过测试并正常运行
package{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
public class Test extends Sprite{
private var txt:TextField;
private var score:uint = 0;
public function Test()
{
//creating my text field
txt = new TextField();
addChild(txt);
//drawing a black rectangle to use as a "button"
var spt:Sprite = new Sprite();
spt.graphics.beginFill(0x000000);
spt.graphics.drawRect(0, 0, 50, 50);
spt.graphics.endFill();
addChild(spt);
spt.y = 50;
//adding the click event to the "button"
spt.addEventListener(MouseEvent.CLICK, handleClick);
}
protected function handleClick(event:MouseEvent):void
{
//adding 10 to score
score += 10;
//setting the txt text field to score
txt.text = score.toString();
}
}
}
答案 1 :(得分:2)
确保在文本字段中嵌入字体。测试它是否可以正常工作:
scoreCounter.embedFonts = false;
看看它是否显示。