2 Replies Latest reply on Nov 26, 2009 11:09 PM by Gregory Lafrance

    What is the 'main()' function in a flex app?  If not, how tap the 'message pump' to access the CPU?


      I'm learning Flex and trying to write a VERY simple app to put random dots or lines on a mx:Canvas.


      I can't figure out where to actually the code to canvas.graphics.lineTo(x,y) code.


      Either I want to grab the CPU in the 'main()' function of the app or draw a dot/line for every slice of CPU in a message pump (like for Windows apps).


      I tried adding this:


      <mx:Canvas id="canvas>



      canvas.graphics.lineTo(Math.random() * 300,Math.random() * 300);     


      but the Flex compiler says this:


      1120: Access of undefined property canvas.


      How come it can't find canvas?


      if I add the 'this' keyword like this:




      I get:

      1042: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code.


      Where do I put the actual graphics code or a loop to draw lines? Help!



        • 1. Re: What is the 'main()' function in a flex app?  If not, how tap the 'message pump' to access the CPU?
          paul.williams Level 4

          Flex is event driven, there is no "main" function or message loop as you might find with other languages. You can trigger logic from "lifecycle" events such as "creationComplete" which is dispatched when a component is created. Or you can trigger logic from a user event such as the "click" event of a button. I would suggest starting at the link below and running through the tutorials:



          1 person found this helpful
          • 2. Re: What is the 'main()' function in a flex app?  If not, how tap the 'message pump' to access the CPU?
            Gregory Lafrance Level 6

            If this post answers your question or helps, please mark it as such.


            Your drawing code must be in a function, and in the following code it gets called at application creationComplete. You can't draw directly onto a Canvas (at least I could not), so draw on a Sprite or SpriteAsset, wrap it in a UIComponent, so it can be added to containers, and then add the UIComponet to the Canvas.



            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  import mx.core.UIComponent;
                  import mx.core.SpriteAsset;
                  private function init():void{
                    var s:SpriteAsset = new SpriteAsset();
                    s.graphics.lineStyle(2, 0x990000, .75);
                    var uic:UIComponent = new UIComponent();
              <mx:Canvas id="canvas" width="800" height="600" backgroundColor="0xFFFFFF"/>