actionscript-3flash-10

swap different colour rectangle in movieclip


I am trying to create a basic example. Using frames i know how to do that but i want to know how this can be done using action script 3.

Using frame:

A movieclip in which there are 6 frames

Can someone please tell me how to do this using AS3?


Solution

  • There are many ways to achieve this, you could use Timer , Tween etc... here's a basic example.

     var _count:int;
     var red:Boolean = true;
     var rectangle:Sprite = new Sprite();
     var rectWidth:int = 300;
     var rectHeight:int = 120;
    
     addChild( rectangle );
    
     addEventListener( Event.ENTER_FRAME , enterFrameListener );
    
     function enterFrameListener(event:Event):void
     {
         if( _count > 0 && _count % 3 == 0 )
            colorChange();
    
         _count++;
     }
    
     function colorChange():void
     {
        var color:uint; 
    
        if( red )  
           color = 0x990000;
         else
           color = 0xfadd00;
    
        with( rectangle.graphics )
        {
            clear(); 
            beginFill(color);
            drawRect( 0 , 0 , rectWidth , rectHeight );
            endFill();
    
        }
    
         red = !red;
     }