【发布时间】:2011-04-01 14:42:15
【问题描述】:
我有这个代码
gameOver1.text = (count1.currentCount / 100).toString();
但我想在显示当前计数之前添加一些文本。例如在gameOver1.text中我想说
分数:(然后是计数)
谢谢。
【问题讨论】:
标签: flash actionscript-3 syntax textfield
我有这个代码
gameOver1.text = (count1.currentCount / 100).toString();
但我想在显示当前计数之前添加一些文本。例如在gameOver1.text中我想说
分数:(然后是计数)
谢谢。
【问题讨论】:
标签: flash actionscript-3 syntax textfield
只需使用:
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);
var score:Number = count1.currentCount / 100;
gameOver1.text = "Score:"+score;
您可以将字符串与 + 运算符一起添加。当您在那里使用数字或整数(或任何其他类型)时,将调用它们的 toString() 函数。
【讨论】:
你可能想要
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);
如果你总是想四舍五入。
【讨论】:
您可以简单地通过将字符串相加来附加字符串。所以写
gameOver1.text = "Score: " + (count1.currentCount / 100).toString();
【讨论】: