2 Replies Latest reply on Feb 20, 2010 11:09 PM by AttaBoy2

    How to run a simple as file

    AttaBoy2 Level 3

      I'm just beginning to learn Flex so I don't know much.  I don't know how to call this simple pixel dissolve file in a flex application.  Any help would be greatly appriciated.

       

      // ActionScript file
      import flash.display.BitmapData;
      import flash.display.Bitmap;
      import flash.geom.Point;
      import flash.geom.Rectangle;
      import flash.utils.Timer;
      import flash.events.TimerEvent;

       

      var bmd:BitmapData = new BitmapData(100, 80, false, 0x00CCCCCC);
      var bitmap:Bitmap = new Bitmap(bmd);
      addChild(bitmap);

       

      var tim:Timer = new Timer(20);
      tim.start();
      tim.addEventListener(TimerEvent.TIMER, timerHandler);

      function timerHandler(event:TimerEvent):void {
          var randomNum:Number = Math.floor(Math.random() * int.MAX_VALUE);
          dissolve(randomNum);
      }

       

      function dissolve(randomNum:Number):void {
          var rect:Rectangle = bmd.rect;
          var pt:Point = new Point(0, 0);
          var numberOfPixels:uint = 100;
          var red:uint = 0x00FF0000;
          bmd.pixelDissolve(bmd, rect, pt, randomNum, numberOfPixels, red);
          var grayRegion:Rectangle = bmd.getColorBoundsRect(0xFFFFFFFF, 0x00CCCCCC, true);
          if(grayRegion.width == 0 && grayRegion.height == 0 ) {
              tim.stop();
          }
      }

        • 1. Re: How to run a simple as file
          Gregory Lafrance Level 6

          You can't call an ActionScript file. And because you are adding children, its best to create a class extending UIComponent, then add an instance of the class in your Flex app <mx:Script> tag (there are a number of ways to add an instance).

           

          package {
            import flash.display.BitmapData;
            import flash.display.Bitmap;
            import flash.geom.Point;
            import flash.geom.Rectangle;
            import flash.utils.Timer;
            import flash.events.TimerEvent;
            import mx.core.UIComponent;
            
            public class MyClass extends UIComponent{
              private var bmd:BitmapData = new BitmapData(100, 80, false, 0x00CCCCCC);
              private var bitmap:Bitmap = new Bitmap(bmd);
              private var tim:Timer;
              
              public function MyClass(){
                tim = new Timer(20);
                tim.addEventListener(TimerEvent.TIMER, timerHandler);
                tim.start();
                addChild(bitmap);
              }
           
              private function timerHandler(event:TimerEvent):void {
                var randomNum:Number = Math.floor(Math.random() * int.MAX_VALUE);    
                dissolve(randomNum);
              }
           
              private function dissolve(randomNum:Number):void {
                var rect:Rectangle = bmd.rect;
                var pt:Point = new Point(0, 0);
                var numberOfPixels:uint = 100;
                var red:uint = 0x00FF0000;
                bmd.pixelDissolve(bmd, rect, pt, randomNum, numberOfPixels, red);
                var grayRegion:Rectangle = bmd.getColorBoundsRect(0xFFFFFFFF, 0x00CCCCCC, true);
                if(grayRegion.width == 0 && grayRegion.height == 0 ) {
                  tim.stop();
                }
              }
            }
          }
          
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();">
            <mx:Script>
              <![CDATA[
                private var mc:MyClass;
                
                private function init():void{
                  mc = new MyClass();
                  addChild(mc);
                }
              ]]>
            </mx:Script>
          </mx:Application>
          


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


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          1 person found this helpful
          • 2. Re: How to run a simple as file
            AttaBoy2 Level 3

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
                creationComplete="runDissolve()">
            <mx:Script>
                <![CDATA[
                    import mx.controls.Alert;
                    import UI.PixelDissolve;
                   
                    private var pd:PixelDissolve;
                   
                    private function runDissolve():void {
                        Alert.show("here we are");
                        pd = new PixelDissolve();
                    }
                   
                ]]>
            </mx:Script>

             

            </mx:Application>

             

            Thanks so much now I have a class.  I wrote a little application above to run an instance of the class but don't know how to make it run.