代码之家  ›  专栏  ›  技术社区  ›  Sarah Proper

日历操作脚本3

  •  1
  • Sarah Proper  · 技术社区  · 11 年前

    我需要在AS3中定期制作一个日历,看起来像这样:

    regular calendar

    当点击日期时:

    clicked a date

    我想我已经掌握了基本知识,但我不知道该从哪里开始,我也无法弄清楚我的代码出了什么问题,从而使这些日子正常工作。

    主要代码:

    package code {
    
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    
    public class Main extends MovieClip {
    
        private var days:Array = new Array();
        public var selectedDay:Day = null;
    
        public function Main() {
            // constructor code
            var across: int = 7;
    
            for( var i:int = 0; i < 31; i++)
            {
                var row:int = Math.floor( i / across );
                var col:int = i % across;
    
                var d:Day = new Day();
                addChild(d);
                d.x = col * (d.width);
                d.y = row * (d.height);
                days.push(d);
                d.addEventListener(MouseEvent.CLICK, onClick);
            }
    
    
    
        }
    
        public function onClick(e:MouseEvent):void{
            if (selectedDay == null){
                trace("meow!");
                days[1].gotoAndStop(2);
            }
            else if (selectedDay != null){
    
            }
    }
    }
    
    }
    

    以及日期代码:

    package code {
    
    import flash.display.MovieClip;
    import flash.text.TextField;
    
    
    public class Day extends MovieClip {
    
        public var weekday_txt:TextField;
        public var date_txt:TextField;
    
        public function Day() {
            // constructor code
            for (var num:int = 0; num < 7; num++){
    
                var weekDays:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
                weekday_txt.text = weekDays[num];
                //trace(weekDays[num]);
                date_txt.text = ""+42;
            }
        }
    }
    
    }
    

    谢谢你的帮助!

    2 回复  |  直到 11 年前
        1
  •  1
  •   Mike    11 年前

    我重写了你的类,因为这比解释逻辑错误更容易:

    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
    
        2
  •  0
  •   Mike    11 年前

    好吧,我想你确实有点奇怪,但我注意到你的onClick方法总是以数组的第二天为目标,并将其设置为转到第2帧,因为它看起来从来没有初始化过“selectedDay”…:

    public function onClick(e:MouseEvent):void{
        if (selectedDay == null){
            trace("meow!");
            days[1].gotoAndStop(2);
        }
        else if (selectedDay != null){
    
        }
    }
    

    将函数更改为:

    public function onClick(e:MouseEvent):void{
        if (selectedDay != null){
           selectedDay.gotoAndStop(1);  //assuming frame 1 is an "unselected" state 
        }  // you don't really need an else, if you are not going to do anything in it
        selectedDay = e.target;
        selectedDay.gotoAndStop(2);
    }
    
    
    public function getSelectedDayInfo():Object{
        var _object = new Object();
        _object.weekday = "nothing selected";
        _object.date = "nothing selected";
        if(selectedDay != null){
            _object.weekday = selectedDay.weekday_txt.text;
            _object.date = selectedDay.date_txt.text;
        }
        return (_object);
    }
    

    希望能有所帮助。。。