4 Replies Latest reply on Apr 14, 2008 11:53 AM by timgerr1

    Flex project, action script classes

    timgerr1
      Hey all, I am learning flex and ActionScript. I have a Flex project that I am playing with, and I am trying to learn how to work with ActionScript classes. I get an error of Access of Undefined property in my code.
      Here is my Flex app:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[
      import com.Example;
      public var test:Example = new Example;
      test.Happy();

      ]]>
      </mx:Script>
      </mx:Application>

      Here is my action script class:
      package com
      {
      public class Example
      {
      public function Example()
      {
      trace('WTF');
      }
      public function Happy():void
      {
      trace('Is Happy Working????');
      }
      }
      }

      In my project when I create an instance of the class
      public var test:Example = new Example; I see the trace WTF work fine.
      When I call the Happy method test.Happy(); I get an error.... Access of Undefined property test.

      What am I doing wrong.
      Thanks for the help,
      timgerr1
        • 1. Re: Flex project, action script classes
          timgerr1 Level 1
          Ok, Like I might have said, I am new to Flex and action script. Is there a different way to add a ActionScript components to a Flex application than adding an ActionScript class component to an ActionScript Application?

          If I take the same component package and add it to an ActionScript application then all works but not doing the same with Flex. What is the difference?

          Thanks,
          Timgerr1
          • 2. Re: Flex project, action script classes
            timgerr1 Level 1
            Why am I unable to call methods when it is not in the main class method?

            Timgerr1
            • 3. Re: Flex project, action script classes
              ntsiii Level 3
              Because of the way mxml is generated into AS, thenthe AS ig compiled into the swf, you cannot access anything instantiated outside of a function immediately. "test" does not yet exist when you run:
              test.Happy();

              Put that call into an init function, or call it from a button and it will work.

              Avoid doing anything but declaring and initializing vars outside of a function.

              Tracy
              • 4. Re: Flex project, action script classes
                timgerr1 Level 1
                quote:

                Originally posted by: ntsiii
                Because of the way mxml is generated into AS, thenthe AS ig compiled into the swf, you cannot access anything instantiated outside of a function immediately. "test" does not yet exist when you run:
                test.Happy();

                Put that call into an init function, or call it from a button and it will work.

                Avoid doing anything but declaring and initializing vars outside of a function.

                Tracy


                Again, I am new, can you please post an example of using an int function or calling it from a button. I am not trying to be a pain, I just want to learn.

                Thanks for the help,
                Timgerr1