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?

    remoteportal

      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>

      ...

        <mx:Script>
        <![CDATA[

      canvas.graphics.lineTo(Math.random() * 300,Math.random() * 300);     
          ]]>
        </mx:Script>

       

      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:

       

      this.canvas.graphics.lineTo(...)

       

      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!

       

      Pete

        • 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:

           

          http://learn.adobe.com/wiki/display/Flex/Getting+Started

          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"
              creationComplete="init();">
              <mx:Script>
                <![CDATA[
                  import mx.core.UIComponent;
                  import mx.core.SpriteAsset;
                  private function init():void{
                    var s:SpriteAsset = new SpriteAsset();
                    s.graphics.lineStyle(2, 0x990000, .75);
                    s.graphics.moveTo(100,100);
                    s.graphics.lineTo(600,200);
                    var uic:UIComponent = new UIComponent();
                    uic.addChild(s);
                    canvas.addChild(uic);
                  }      
                ]]>
              </mx:Script>
              <mx:Canvas id="canvas" width="800" height="600" backgroundColor="0xFFFFFF"/>
            </mx:Application>