【问题标题】:Syntax problem with textfield as3文本字段 as3 的语法问题
【发布时间】:2011-04-01 14:42:15
【问题描述】:

我有这个代码

gameOver1.text = (count1.currentCount / 100).toString();

但我想在显示当前计数之前添加一些文本。例如在gameOver1.text中我想说

分数:(然后是计数)

谢谢。

【问题讨论】:

    标签: flash actionscript-3 syntax textfield


    【解决方案1】:

    只需使用: gameOver1.text = "Score" + (count1.currentCount / 100).toString();

    动作脚本源自 ECMA 脚本。为什么它类似于具有字符串操作等功能的 javascript 的原因之一。除字符串连接外,其他示例包括:

    • myString.charAt(0);

    • myString.substring(0,4);

    • myString.substring(4);

    • myString.slice(4,9);

    • myString.search("Word");

    • myString.toLowerCase();

    • myString.toUpperCase();

    • myString.length;

    • myString.replace(myString.substring(0,3), “新”);

    【讨论】:

    • 你也不需要最后的toString(),它会自动进行转换。即gameOver1.text = "Score" + (count1.currentCount / 100);
    • 是的仪式。我想我没有实现我在自己的第二行中提出的建议。
    【解决方案2】:
    var score:Number = count1.currentCount / 100;
    gameOver1.text = "Score:"+score;
    

    您可以将字符串与 + 运算符一起添加。当您在那里使用数字或整数(或任何其他类型)时,将调用它们的 toString() 函数。

    【讨论】:

    • ...当且仅当第一项是字符串时。如果第一项是数字并且你使用 + 运算符,那么它会混淆。
    【解决方案3】:

    你可能想要

    gameOver1.text = "Score: " + int(count1.currentCount/100);
    

    如果你的分数需要显示整数,没有小数部分。或者可能:

    gameOver1.text = "Score: " + int((count1.currentCount+50)/100);
    

    gameOver1.text = "Score: " + Math.round(count1.currentCount/100);
    

    如果您想要一个整数分数,但想要四舍五入而不是截断为 int(与 floor 相同)。

    gameOver1.text = "Score: " + (count1.currentCount/100).toFixed(2);
    

    如果要显示四舍五入到某个固定的小数位数(示例中为 2 位)。您甚至可能需要:

    gameOver1.text = "Score: " + Math.ceil(count1.currentCount/100);
    

    如果你总是想四舍五入。

    【讨论】:

      【解决方案4】:

      您可以简单地通过将字符串相加来附加字符串。所以写

      gameOver1.text = "Score: " + (count1.currentCount / 100).toString();
      

      【讨论】:

        猜你喜欢
        • 2013-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 2010-11-09
        • 1970-01-01
        相关资源
        最近更新 更多