Actionscript 3 动作脚本3.0记分计数器

Actionscript 3 动作脚本3.0记分计数器,actionscript-3,flash-cs5,Actionscript 3,Flash Cs5,我一直在尝试为一个小游戏制作一个分数计数器,在这个小游戏中,用户单击舞台上的按钮,每次用户按下按钮,分数都会增加10,但是我无法在动态文本字段中显示分数。我做错了什么 var score:uint; //scoreCounter is the instance name of the dynamic text box function updateScore():void{ score += 10; scoreCounter.text = score.toStrin

我一直在尝试为一个小游戏制作一个分数计数器,在这个小游戏中,用户单击舞台上的按钮,每次用户按下按钮,分数都会增加10,但是我无法在动态文本字段中显示分数。我做错了什么

var score:uint;
//scoreCounter is the instance name of the dynamic text box

    function updateScore():void{
     score += 10;
     scoreCounter.text = score.toString();
}

很难说问题出在哪里,因为您确实没有发布足够的代码。确保你的方法看起来像这样。此代码经过测试并正常工作

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();


    }
}
}

很难说问题出在哪里,因为您确实没有发布足够的代码。确保你的方法看起来像这样。此代码经过测试并正常工作

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();


    }
}
}

确保在文本字段中嵌入字体。要测试其是否工作正常,请执行以下操作:

 scoreCounter.embedFonts = false;

然后看看它是否显示

确保在文本字段中嵌入字体。要测试其是否工作正常,请执行以下操作:

 scoreCounter.embedFonts = false;

然后看看它是否显示

您需要显示其余的代码,包括按钮事件回调。//事件侦听器var score:uint;函数updateScore():void{score+=10;scoreCounter.text=score.toString();}函数stonePoint(e:MouseEvent):void{if(MouseEvent.MOUSE_DOWN){updateScore();scoreCounter.text=score.toString();}//函数hatPoint(e:MouseEvent):void{if(MouseEvent.MOUSE_DOWN){updateScore();scoreCounter.text=score.toString();}对不起,这很混乱,但我不能回答我自己的问题,我需要至少100个声誉或等待8小时,你应该能够编辑你的问题,但可以包括在那里。。。但这可能是一个从未将分数设置为0的简单问题?您需要初始化变量,或者将10添加到“谁知道是什么”中。@M.Laing这是一个很好的观点,但不会设置.text to score.toString()使“null”或“undefined”在这种情况下显示在文本字段中?您需要显示其余代码,包括按钮事件回调。//事件侦听器var score:uint;函数updateScore():void{score+=10;scoreCounter.text=score.toString();}函数stonePoint(e:MouseEvent):void{if(MouseEvent.MOUSE_DOWN){updateScore();scoreCounter.text=score.toString();}//函数hatPoint(e:MouseEvent):void{if(MouseEvent.MOUSE_DOWN){updateScore();scoreCounter.text=score.toString();}对不起,这很混乱,但我不能回答我自己的问题,我需要至少100个声誉或等待8小时,你应该能够编辑你的问题,但可以包括在那里。。。但这可能是一个从未将分数设置为0的简单问题?您需要初始化变量,或者将10添加到“谁知道是什么”中。@M.Laing这是一个很好的观点,但不会设置.text to score.toString()使“null”或“undefined”在这种情况下显示在文本字段中?