ActionScript 3 Image Mask Animation

February 6, 2009 by: smonte

Learn how to create a cool image mask animation!

1. Make/download an image of size 500×300. I downloaded my image from N.Design Studio.

2. Create a new document (500×300).

3. Import your image into the library.

4. Drag that image to the stage (add it to the center of the stage).

5. Convert the image into a movieclip.

Instance name

6. Give your movie clip an instance name of “cityMC”.

Instance name

7. Now we are ready to create the rectangles that reveal the picture. So draw a rectangle with rounded corners. You can position the rectangle anywhere you like on the stage.

8. Make the rectangle of size 40×40.

9. Convert the rectangle into a movie clip. Note the registration point!

Create a mask symbol

10. Remove the rectangle from the stage.

11. Linkage the rectangle to a class ”MaskRectangle”.

Linkage

Linkage warning

Don’t worry about the warning above .

12. Now we can start ActionScripting. With ActionScript 3 we position the mask boxes onto the image and then tween them to reveal the image. Create a new layer for ActionScript and type the following.

//We need these classes for the animation
import fl.transitions.Tween;
import fl.transitions.easing.*;
 
//These are the mask rectangle's width and height
var boxWidth:Number = 40;
var boxHeight:Number = 40;
 
//We want nine rows and 14 columns to make the animation look nice
var rows:Number = 9;
var columns:Number = 14;
 
//We will add the rectangle's into an array (we need this array in the animation)
var rectangles:Array = new Array();
 
//We add the tweens into an array so they don't get carbage collected
var tweens:Array = new Array();
 
//This container will hold all the mask rectangles
var container:Sprite = new Sprite();
 
//Add the container onto the stage
addChild(container);
 
//Set the container to be the image's mask
cityMC.mask = container;
 
//Loop through the rows
for (var i=0; i < rows; i++) {
 
        //Loop through the columns
        for (var j=0; j < columns; j++) {
 
                //Create a new mask rectangle
                var maskRectangle:MaskRectangle = new MaskRectangle();
 
                //Position the mask rectangle
                maskRectangle.x = j * boxWidth;
                maskRectangle.y = i * boxWidth;
 
                //Set the scaleX to be 0, so the rectangle will not be visible
                maskRectangle.scaleX = 0;
 
                //Add the rectangle onto the container
                container.addChild(maskRectangle);
 
                //Add the mask rectangle to the rectangles array
                rectangles.push(maskRectangle);
        }
}
 
//Create and start a timer.
//This timer is called as many times as there are rectangles on the stage.
var timer = new Timer(35,rectangles.length);
timer.addEventListener(TimerEvent.TIMER, animateMask);
timer.start();
 
//This function is called every 0.035 seconds
function animateMask(e:Event):void {
 
        //Save the rectangle to a local variable
        var rectangle = rectangles[timer.currentCount - 1];
 
        //Tween the scaleX of the rectangle
        var scaleTween:Tween = new Tween(rectangle,"scaleX",Regular.easeOut,0,1,1,true);
        tweens.push(scaleTween);
 
}

You are done, test your movie!

  • Digg
  • Sphinn
  • del.icio.us
  • Mixx

Comments

One Response to “ActionScript 3 Image Mask Animation”
  1. mark says:

    Can I get fla file of this lesson? It would be grate to see example file at the end of lesson…

    Have a nice day!

    Reply