3 Replies Latest reply on Aug 30, 2008 3:27 PM by charger

    Why can't I put a Timer in a custom component

    charger
      Hi,

      I'm creating a custom mxml component in flex 3 called "timertest". When I try to create a timer object and build, I get:

      1120: Access of undefined property t.

      Here is the simple code so far:

      Component (timertest):
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300">
      <mx:Script>
      <![CDATA[
      import flash.utils.Timer;
      import flash.events.TimerEvent;
      public var t:Timer;
      t = new Timer(5);
      ]]>
      </mx:Script>
      </mx:Canvas>

      Application:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="420" height="302" xmlns:ns1="*" backgroundColor="#000000">
      <ns1:timertest>
      </ns1:timertest>
      </mx:Application>

      What am I doing wrong???

      Thanks

      M
        • 1. Re: Why can't I put a Timer in a custom component
          Level 7

          "charger" <webforumsuser@macromedia.com> wrote in message
          news:g999oj$6kq$1@forums.macromedia.com...
          > Hi,
          >
          > I'm creating a custom mxml component in flex 3 called "timertest". When I
          > try
          > to create a timer object and build, I get:
          >
          > 1120: Access of undefined property t.
          >
          > Here is the simple code so far:
          >
          > Component (timertest):
          > <?xml version="1.0" encoding="utf-8"?>
          > <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400"
          > height="300">
          > <mx:Script>
          > <![CDATA[
          > import flash.utils.Timer;
          > import flash.events.TimerEvent;
          > public var t:Timer;
          > t = new Timer(5);
          > ]]>
          > </mx:Script>
          > </mx:Canvas>

          Try:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400"
          height="300">
          <mx:Script>
          <![CDATA[
          import flash.utils.Timer;
          import flash.events.TimerEvent;
          public var t:Timer = new Timer(5);
          ]]>
          </mx:Script>
          </mx:Canvas>

          or

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400"
          height="300" creationComplete="initTimer()">
          <mx:Script>
          <![CDATA[
          import flash.utils.Timer;
          import flash.events.TimerEvent;
          public var t:Timer;
          private function initTimer():void{
          t = new Timer(5);
          }
          ]]>
          </mx:Script>
          </mx:Canvas>

          You probably also don't need or want t to be a public variable. Instead,
          you probably want an interval property with a getter/setter pair that
          creates a timer when the interval is set.

          HTH;

          Amy


          • 2. Re: Why can't I put a Timer in a custom component
            Gregory Lafrance Level 6
            You can declare (and initialize) variables outside functions, but you can initialize them after they have been created, or change/use the variables, only inside functions. Try this:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300"
            creationComplete="init()">
            <mx:Script>
            <![CDATA[
            import flash.utils.Timer;
            import flash.events.TimerEvent;
            public var t:Timer;
            private function init():void{
            t = new Timer(5);
            t.start();
            }
            ]]>
            </mx:Script>
            </mx:Canvas>
            • 3. Re: Why can't I put a Timer in a custom component
              charger Level 1
              problem solved!

              Thanks for the help.

              M