8 Replies Latest reply on Dec 8, 2009 5:42 AM by SiHoop

    MVC

    SiHoop Level 1

      I've spent the day working on an example from Lott and Patterson's AS3 Design Patterns book. I keep getting an error on the chapter on mvc and cannot figure out the problem. I've checked my typing, but the error persists. Would someone mind looking at the files and tell me if there is an obvious error?

       

       

      Time.as

      package {
          public class Time{
              private var _hour:uint;
              private var _minute:uint;
              private var _second:uint;
              public function get hour():uint{
                  return _hour;
              }
              public function set hour(value:uint):void{
                  _hour=value;
              }
              public function get minute():uint{
                  return _minute;
              }
              public function set minute(value:uint):void{
                  _minute=value;
              }
              public function get second():uint{
                  return _second;
              }
              public function set second(value:uint):void{
                  _second=value;
              }
              public function Time(hour:uint; minute:uint; second:uint){
                  _hour=hour
                  _minute=minute
                  _second=second
              }
              public function clone():Time{
                  return new Time(_hour, _minute, _second)
              }

       

          }
      }

       

      ClockData.as

      package {
          import flash.events.Event;
          import flash.events.EventDispatcher;
         
          public class ClockData extends EventDispatcher{
          private var _time:Time;
         
              public function get time():Time{
                  if(_time==null){
                      var date:Date=new Date();
                      return new Time(date.hours, date.minutes, date.seconds);
                  }else{
                  return _time.clone();
                  }
              }
              public function set time(value:Time):void{
                  _time=value.clone();
                  dispatchEvent(new Event(Event.CHANGE));
              }
              public function ClockData(){
              }
          }
      }

       

      AbstractClockView.as

      package{
          import flash.display.Sprite;
          import flash.events.Event;
         
          public class AbstractClockView extends Sprite{
              protected var _data:ClockData
             
              public function Clock(data:ClockData){
                  _data=data
                  _data.addEventListener(Event.Change, draw)
              }
              public function draw(event:Event):void{
                 
              }

       

          }
      }

       

      ClockTest.as

      package{
          import flash.display.*;
          public class ClockTest extends Sprite{
              private var _clockData:ClockData;

       

              public function ClockTest(){
                  stage.align=StageAlign.TOP_LEFT;
                  stage.scaleMode=StageScaleMode.NO_SCALE;
                  _clockData=new ClockData;
                  var clock:AbstractClockView=new AnalogClock(_clockData);
                  clock.x=100;
                  clock.y=100;
                  addChild(clock);
              }

       

          }
      }

        • 1. Re: MVC
          msakrejda Level 4

          What's the error message (and stack trace if this is a runtime error)?

          • 2. Re: MVC
            SiHoop Level 1

            Here are the errors from the Problems panel:

             

            1008: Attribute is invalid.    Clock_mvc1/src    Time.as    line 24    1260220410104    19095

             

            1084: Syntax error: expecting identifier before rightparen.    Clock_mvc1/src    Time.as    line 24    1260220410104    19096

             

            1084: Syntax error: expecting rightbrace before leftbrace.    Clock_mvc1/src    Time.as    line 24    1260220410104    19097

             

            1084: Syntax error: expecting rightparen before semicolon.    Clock_mvc1/src    Time.as    line 24    1260220410089    19093

             

            1158: Syntax error: missing left brace ({) before the function body.    Clock_mvc1/src    Time.as    line 24    1260220410104    19094
            • 3. Re: MVC
              msakrejda Level 4

              You have semicolons separating your constructor arguments; those should be commas.

              • 4. Re: MVC
                SiHoop Level 1

                I've corrected the files and included below what I think is the correct code. It still is not working, and I've commented out the lines in the file that are causing problems(i.e. AbstractClockView.as). I don't understand them and they cause an error. The error I get is:

                TypeError: Error #1009: Cannot access a property or method of a null object reference.
                    at AnalogClock/draw()[C:\Documents and Settings\sxh12\My Documents\Flex Builder 3\Clock_mvc1\src\AnalogClock.as:37]
                    at AnalogClock()[C:\Documents and Settings\sxh12\My Documents\Flex Builder 3\Clock_mvc1\src\AnalogClock.as:33]
                    at Clock_mvc1/init()[C:\Documents and Settings\sxh12\My Documents\Flex Builder 3\Clock_mvc1\src\Clock_mvc1.mxml:12]
                    at Clock_mvc1/___Clock_mvc1_Application1_creationComplete()[C:\Documents and Settings\sxh12\My Documents\Flex Builder 3\Clock_mvc1\src\Clock_mvc1.mxml:2]
                    at flash.events::EventDispatcher/dispatchEventFunction()
                    at flash.events::EventDispatcher/dispatchEvent()
                    at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src \mx\core\UIComponent.as:9298]
                    at mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as :1169]
                    at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects \framework\src\mx\managers\LayoutManager.as:718]
                    at Function/http://adobe.com/AS3/2006/builtin::apply()
                    at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framew ork\src\mx\core\UIComponent.as:8628]
                    at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framewo rk\src\mx\core\UIComponent.as:8568]

                 

                 

                 

                AbstractClockView.as

                package{
                    import flash.display.Sprite;
                    import flash.events.Event;
                   
                    public class AbstractClockView extends Sprite{
                        protected var _data:ClockData;
                       
                        public function Clock(data:ClockData){
                            _data=data
                            _data.addEventListener(Event.CHANGE, draw)
                        }
                        protected function draw(event:Event=null):void{
                           
                        }

                 

                    }
                }

                 

                AbstractClockView.as

                package{
                    import flash.display.Sprite;
                    import flash.events.Event;
                   
                    public class AnalogClock extends AbstractClockView{
                        private var _face:Sprite;
                        private var _hourHand:Sprite;
                        private var _minuteHand:Sprite;
                        private var _secondHand:Sprite;
                       
                        public function AnalogClock(data:ClockData){
                            super();
                            _face=new Sprite;
                            _face.graphics.lineStyle(0,0x000000,1);
                            _face.graphics.drawCircle(0,0,100);
                            addChild(_face)

                 

                            _hourHand=new Sprite
                            _hourHand.graphics.lineStyle(5,0x000000,1)
                            _hourHand.graphics.lineTo(0,-50)
                            addChild(_hourHand)
                           
                            _minuteHand=new Sprite
                            _minuteHand.graphics.lineStyle(2,0x000000,1)
                            _minuteHand.graphics.lineTo(0,-80)
                            addChild(_minuteHand)
                           
                            _secondHand=new Sprite
                            _secondHand.graphics.lineStyle(0,0x000000,1)
                            _secondHand.graphics.lineTo(0,-80)
                            addChild(_secondHand)
                           
                            draw()
                           
                        }
                    override protected function draw(event:Event=null):void{
                    //    var time:Time=_data.time
                        //_hourHand.rotation=30 * time.hour +30 * time.minute/60;
                        //_minuteHand.rotation=6 * time.minute +6 * time.second/60;
                        //_secondHand.rotation=6 * time.second ;
                    }
                    }
                }

                 

                ClockData.as

                package {
                    import flash.events.Event;
                    import flash.events.EventDispatcher;
                   
                    public class ClockData extends EventDispatcher{
                       
                        private var _time:Time;
                        public function get time():Time{
                            if(_time==null){
                                var date:Date=new Date();
                                return new Time(date.hours, date.minutes, date.seconds);
                            }else{
                            return _time.clone();
                            }
                        }
                        public function set time(value:Time):void{
                            _time=value.clone();
                            dispatchEvent(new Event(Event.CHANGE));
                        }
                        public function ClockData(){
                        }
                    }
                }

                 

                Time.as

                package {
                    public class Time{
                        private var _hour:uint;
                        private var _minute:uint;
                        private var _second:uint;
                        public function get hour():uint{
                            return _hour;
                        }
                        public function set hour(value:uint):void{
                            _hour=value;
                        }
                        public function get minute():uint{
                            return _minute;
                        }
                        public function set minute(value:uint):void{
                            _minute=value;
                        }
                        public function get second():uint{
                            return _second;
                        }
                        public function set second(value:uint):void{
                            _second=value;
                        }
                        public function Time(hour:uint, minute:uint, second:uint){
                            _hour=hour
                            _minute=minute
                            _second=second
                        }
                        public function clone():Time{
                            return new Time(_hour, _minute, _second)
                        }

                 

                    }
                }

                • 5. Re: MVC
                  msakrejda Level 4

                  You have not shown us the mxml file that instantiates your

                  AnalogClock. It looks like it's passing null to the AnalogClock

                  constructor. You're getting an error because the _data property of

                  AnalogClock is not set when Flash Player tries to use it in the draw()

                  method. You may want to look at Flash Player error messages and their

                  explanations--Adobe has a fairly clear (though terse) reference on the

                  web.

                  • 6. Re: MVC
                    SiHoop Level 1

                    I'm not using an mxml file-- the main class file is AbstractClockView.as. I'm finding it difficult to follow the flow of information between the files and I'm not sure how to move forward with this. Can you suggest what I should try next?

                    • 7. Re: MVC
                      paul.williams Level 4

                      I would suggest using the debugger to fiqure out what's null in your AnalogClockView's draw method. My guess is that the _data property is null. You are passing an instance of ClockData (the model) into the constructor of AnalogClockView (the view), but you aren't doing anything with it.

                      1 person found this helpful
                      • 8. Re: MVC
                        SiHoop Level 1

                        The problem with the book example was 2 typos. The first problem was the constructor was named Clock instead of AbstractClockView. The second was that the event handler for the draw method should have included =null.

                         

                        Has anybody else used the Andanced AS3 with Design Patterns text by Lott and Patterson? I'd be interested in your experience if you ahve read it. I've just started to use it and have run into 2 pretty important errors in the first activity.

                         

                         

                        package{
                            import flash.display.Sprite;
                            import flash.events.Event;
                           
                            public class AbstractClockView extends Sprite{
                                protected var _data:ClockData;
                                public function AbstractClockView(data:ClockData):void{
                                    _data=data
                                    _data.addEventListener(Event.CHANGE, draw)
                                }
                                protected function draw(event:Event=null):void{
                                }

                         

                            }
                        }