【问题标题】:Action Script 3.0 Score Counter动作脚本 3.0 计分器
【发布时间】:2012-03-28 18:21:29
【问题描述】:

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

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

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

【问题讨论】:

  • 您需要显示其余代码,包括您的按钮事件回调。
  • //事件监听器 var score:uint;函数 updateScore():void{ 分数 += 10; scoreCounter.text = score.toString(); } function 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 设置为 score.toString() 不会使“null”或“undefined”出现在文本字段中吗?

标签: actionscript-3 flash-cs5


【解决方案1】:

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

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


    }
}
}

【讨论】:

    【解决方案2】:

    确保在您的文本字段中嵌入字体。测试它是否可以正常工作:

     scoreCounter.embedFonts = false;
    

    看看它是否显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多