有条件的 Flash / AS3 重复变量警告
Flash / AS3 duplicate vars warning in conditional
我在 AS3 中编写了这个函数,将毫秒转换为可读的时间格式:00:00:00 (hh/mm/ss).
function convertTime(millis:Number):String {
var Seconds = ((millis / 1000) % 60);
var Minutes = (((millis / 1000) / 60) % 60);
var Hours = ((((millis / 1000) / 60) / 60) % 24);
if ( Math.floor(Seconds) < 10 ) {
var newSeconds = "0" + Math.floor(Seconds);
} else {
var newSeconds = Math.floor(Seconds);
}
if ( Math.floor(Minutes) < 10 ) {
var newMinutes = "0" + Math.floor(Minutes);
} else {
var newMinutes = Math.floor(Minutes);
}
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
} else {
var newHours = Math.floor(Hours);
}
return (newHours + ":" + newMinutes + ":" + newSeconds);
}
这一切似乎都有效,除了秒数,它只有 returns 个数字,我确定与以下内容相关:
对于 else 语句中设置的变量的每个实例,flash 编译器都会抛出 "Warning 3596: Duplicate variable definition"?
我做错了吗?
当然这些在条件语句中的事实应该意味着每个变量只设置一次对吗?
或者我是否必须在 AS3 中非常明确地删除 else(s)?例如:
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
}
if ( Math.floor(Hours) >= 10 ) {
var newHours = Math.floor(Hours);
}
啊,我找到了。 AS3 对 var 声明非常严格。
变量已经在 if 中设置,所以不需要在 else 中重新声明它:
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
} else {
newHours = Math.floor(Hours);
}
[编辑]
为了避免所有这些 if 和 else(假设使用 get Timer),您可以改用 Date Class,并使用 flash.globalization.DateTimeFormatter Class.
要格式化输出,只需使用 setDateTimePattern() 方法。
在你的情况下,使用 setDateTimePattern("hh:mm:ss");除非你想检索毫秒...
DateTimeFormatter Class 真的很有用 但是当你尝试获取 Millisecons("hh:mm:ss:SSS") 时似乎有问题,所以我尝试改进代码以获取 Milliseconds 如果你需要它.
[编辑 2]
import flash.utils.getTimer;
import flash.globalization.DateTimeFormatter;
import flash.globalization.DateTimeStyle;
var currentTime = new Date();
function getMS():String{
var ms = currentTime.milliseconds;
if (ms<10)
{
ms = "000" + ms;
}
if (ms<100)
{
ms = "00" + ms;
}
return ms.toString();
}
function formatDate(date:Date) {
var dtf:DateTimeFormatter = new DateTimeFormatter("en,EN");
dtf.setDateTimePattern("yyyy-MM-dd HH:mm:ss:");
var longDate:String = dtf.format(date);
trace(longDate.toString() + getMS());
//trace("***LocaleID requested=" + dtf.requestedLocaleIDName);
//trace("***Format requested (" + dtf.getDateTimePattern() + ")");
}
trace("setDateTimePattern example");
formatDate(currentTime);
// output the current time formated as "hh:mm:ss:SSS"
所以在这种情况下输出是:
setDateTimePattern example
2016-10-23 11:53:32:979
[/编辑 2]
[/编辑]
此致。
尼古拉斯.
如您所见,这是由于 AS3 处理变量声明的方式所致。它源于使用函数级作用域但没有块级作用域的 AS3 作用域模型。代码中的变量声明 提升 到函数的顶部,相当于:
function convertTime(millis:Number):String {
var Seconds = ((millis / 1000) % 60);
var Minutes = (((millis / 1000) / 60) % 60);
var Hours = ((((millis / 1000) / 60) / 60) % 24);
var newSeconds; //Seconds 'if' case
var newSeconds; //Seconds 'else' case
var newMinutes; //Minutes 'if' case
var newMinutes; //Minutes 'else' case
var newHours; //Hours 'if' case
var newHours; //Hours 'else' case
if ( Math.floor(Seconds) < 10 ) {
newSeconds = "0" + Math.floor(Seconds);
} else {
newSeconds = Math.floor(Seconds);
}
if ( Math.floor(Minutes) < 10 ) {
newMinutes = "0" + Math.floor(Minutes);
} else {
newMinutes = Math.floor(Minutes);
}
if ( Math.floor(Hours) < 10 ) {
newHours = "0" + Math.floor(Hours);
} else {
newHours = Math.floor(Hours);
}
return (newHours + ":" + newMinutes + ":" + newSeconds);
}
所以您当然会看到警告 3596。
另请参阅 http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html 了解更多详细信息。
我在 AS3 中编写了这个函数,将毫秒转换为可读的时间格式:00:00:00 (hh/mm/ss).
function convertTime(millis:Number):String {
var Seconds = ((millis / 1000) % 60);
var Minutes = (((millis / 1000) / 60) % 60);
var Hours = ((((millis / 1000) / 60) / 60) % 24);
if ( Math.floor(Seconds) < 10 ) {
var newSeconds = "0" + Math.floor(Seconds);
} else {
var newSeconds = Math.floor(Seconds);
}
if ( Math.floor(Minutes) < 10 ) {
var newMinutes = "0" + Math.floor(Minutes);
} else {
var newMinutes = Math.floor(Minutes);
}
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
} else {
var newHours = Math.floor(Hours);
}
return (newHours + ":" + newMinutes + ":" + newSeconds);
}
这一切似乎都有效,除了秒数,它只有 returns 个数字,我确定与以下内容相关:
对于 else 语句中设置的变量的每个实例,flash 编译器都会抛出 "Warning 3596: Duplicate variable definition"?
我做错了吗?
当然这些在条件语句中的事实应该意味着每个变量只设置一次对吗?
或者我是否必须在 AS3 中非常明确地删除 else(s)?例如:
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
}
if ( Math.floor(Hours) >= 10 ) {
var newHours = Math.floor(Hours);
}
啊,我找到了。 AS3 对 var 声明非常严格。
变量已经在 if 中设置,所以不需要在 else 中重新声明它:
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
} else {
newHours = Math.floor(Hours);
}
[编辑]
为了避免所有这些 if 和 else(假设使用 get Timer),您可以改用 Date Class,并使用 flash.globalization.DateTimeFormatter Class.
要格式化输出,只需使用 setDateTimePattern() 方法。 在你的情况下,使用 setDateTimePattern("hh:mm:ss");除非你想检索毫秒...
DateTimeFormatter Class 真的很有用 但是当你尝试获取 Millisecons("hh:mm:ss:SSS") 时似乎有问题,所以我尝试改进代码以获取 Milliseconds 如果你需要它.
[编辑 2]
import flash.utils.getTimer;
import flash.globalization.DateTimeFormatter;
import flash.globalization.DateTimeStyle;
var currentTime = new Date();
function getMS():String{
var ms = currentTime.milliseconds;
if (ms<10)
{
ms = "000" + ms;
}
if (ms<100)
{
ms = "00" + ms;
}
return ms.toString();
}
function formatDate(date:Date) {
var dtf:DateTimeFormatter = new DateTimeFormatter("en,EN");
dtf.setDateTimePattern("yyyy-MM-dd HH:mm:ss:");
var longDate:String = dtf.format(date);
trace(longDate.toString() + getMS());
//trace("***LocaleID requested=" + dtf.requestedLocaleIDName);
//trace("***Format requested (" + dtf.getDateTimePattern() + ")");
}
trace("setDateTimePattern example");
formatDate(currentTime);
// output the current time formated as "hh:mm:ss:SSS"
所以在这种情况下输出是:
setDateTimePattern example
2016-10-23 11:53:32:979
[/编辑 2]
[/编辑]
此致。 尼古拉斯.
如您所见,这是由于 AS3 处理变量声明的方式所致。它源于使用函数级作用域但没有块级作用域的 AS3 作用域模型。代码中的变量声明 提升 到函数的顶部,相当于:
function convertTime(millis:Number):String {
var Seconds = ((millis / 1000) % 60);
var Minutes = (((millis / 1000) / 60) % 60);
var Hours = ((((millis / 1000) / 60) / 60) % 24);
var newSeconds; //Seconds 'if' case
var newSeconds; //Seconds 'else' case
var newMinutes; //Minutes 'if' case
var newMinutes; //Minutes 'else' case
var newHours; //Hours 'if' case
var newHours; //Hours 'else' case
if ( Math.floor(Seconds) < 10 ) {
newSeconds = "0" + Math.floor(Seconds);
} else {
newSeconds = Math.floor(Seconds);
}
if ( Math.floor(Minutes) < 10 ) {
newMinutes = "0" + Math.floor(Minutes);
} else {
newMinutes = Math.floor(Minutes);
}
if ( Math.floor(Hours) < 10 ) {
newHours = "0" + Math.floor(Hours);
} else {
newHours = Math.floor(Hours);
}
return (newHours + ":" + newMinutes + ":" + newSeconds);
}
所以您当然会看到警告 3596。
另请参阅 http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html 了解更多详细信息。