嘿,我知道之前已经问过这个问题,但那里的人有不同的问题。
所以这是我的简单代码,这个程序将根据今天的日期显示我为每一天创建的符号。
var day:Date = new Date();
var today:int = day.day;
//trace(day);
function addpic(){
switch(today){
case 1:
{
var pic:Monday = new Monday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
case 2:
{
var pic:Tuesday = new Tuesday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
case 3:
{
var pic:Wednesday = new Wednesday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
case 4:
{
var pic:Thurday = new Thursday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
case 5:
{
var pic:Friday = new Friday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
case 6:
{
var pic:Saturday = new Saturday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
case 7:
{
var pic:Sunday = new Sunday();
addChild(pic);
pic.x = 640;
pic.y = 200;
}
break;
}
}
addpic();
我认为它的原因是我多次声明了pic变量,但是我没有在一个案例中声明它,所以它不应该这样做吗?
答案 0 :(得分:-1)
你应该这样做:
var pic:*;
function addpic():void
{
switch (today)
{
case 1 :
pic = new Monday();
break;
case 2 :
pic = new Tuesday();
break;
case 3 :
pic = new Wednesday();
break;
case 4 :
pic = new Thursday();
break;
case 5 :
pic = new Friday();
break;
case 6 :
pic = new Saturday();
break;
case 7 :
pic = new Sunday();
break;
}
pic.x = 640;
pic.y = 200;
addChild(pic);
}
addpic();
或者更简单,有一个电影剪辑“pic”(图片类)包含你的日子(7帧):
var pic:Pic = new Pic();
pic.x = 640;
pic.y = 200;
addChild(pic);
function addpic():void
{
pic.gotoAndStop(today);
}
addpic();