我重写了你的类,因为这比解释逻辑错误更容易:
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip{
private var days:Array = new Array();
private var selectedDay:Day;
public function Main() {
stop();
days = new Array();
var weekDayTitles:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var across:int = 7;
for (var i:int=0; i<31; i++){
var row:int=Math.floor(i/across);
var col:int = i%7;
var d:Day = new Day();
addChild(d);
d.x = col * (d.width);
d.y = row * (d.height);
d.setWeekDay(weekDayTitles[col]);
d.setDate(""+(i+1));
days.push(d);
d.addEventListener(MouseEvent.CLICK, onClick);
}
}
public function onClick(e:MouseEvent):void{
if (selectedDay != null){
selectedDay.setAsUnSelected();
}
selectedDay = Day(e.target);
selectedDay.setAsSelected();
}
}// Class
你的走读课应该是这样的:
// make sure this class is linked to a movieclip
// in your library that has two frames
// frame 1 = "unselected"
// frame 2 = "selected";
// make sure to have 2 textfields in the linked symbol, one named : "weekdayTxt" and the other "dateTxt"
public class Day extends MovieClip{
public var weekday_txt:TextField;
public var date_txt:TextField;
public function Day() {
gotoAndStop(1);
weekday_txt = this.weekdayTxt;
date_txt = this.dateTxt;
}
public function setWeekDay(_day:String):void{
weekday_txt.text = _day;
}
public function setDate(_date:String):void{
day_txt.text = _date;
}
public function setAsSelected():void{
this.gotoAndStop(2);
}
public function setAsUnSelected():void{
this.gotoAndStop(1);
}
}// Class