Using Filters with ActionScript 3

February 6, 2009 by: smonte

In this tutorial, we’re going to apply blur and bevel filters to images. All the animation is done with ActionScript 3.0 of course. Move your mouse over to the images to see the effects. When you master these, you’ll be able to create other filter effects as well. So start your Flash and let’s get started.

Setting up the environment

1. Create a new document of size 500×250.

2. Import two images to the stage (about size 200×200). You can import one rectangular and one round image as I have done.

3. Convert both images into movie clips. You can name them to whatever you want. Set the registration points to the center.

4. Give them instance names of “apple01″ and “apple02″.

Moving into ActionScript 3

5. Open your actions panel and type the following.

//These are used for animating the filters
var blurSpeed:Number = 1;
var bevelSpeed:Number = 5;
 
//Add the MOUSE_OVER listener to each apple
apple01.addEventListener (MouseEvent.MOUSE_OVER, mouseOverApple01);
apple02.addEventListener (MouseEvent.MOUSE_OVER, mouseOverApple02);
 
//Add the MOUSE_OUT listener to each apple
apple01.addEventListener (MouseEvent.MOUSE_OUT, mouseOutApple01);
apple02.addEventListener (MouseEvent.MOUSE_OUT, mouseOutApple02);
 
/* Add the ENTER_FRAMEs for both apples, so we can animate them in each frame. */
apple01.addEventListener (Event.ENTER_FRAME, enterFrameApple01);
apple02.addEventListener (Event.ENTER_FRAME, enterFrameApple02);
 
// Create and assign the blur filter to apple01.
var blur:BlurFilter = new BlurFilter();
blur.blurX = 20;
blur.blurY = 20;
blur.quality = BitmapFilterQuality.HIGH;
apple01.filters = [blur];
 
//Create a BevelFilter for apple02
var bevelFilter:BevelFilter = new BevelFilter(10,
                                   45,
                                   0x000000,
                                   1,
                                   0xffffff,
                                   1,
                                   0,
                                   0,
                                   0,
                                   BitmapFilterQuality.HIGH,
                                   BitmapFilterType.INNER,
                                   false);
 
 
apple02.filters = [bevelFilter];
 
//Set the default values (mouse is not over the apples)
var mouseIsOverApple01:Boolean = false;
var mouseIsOverApple02:Boolean = false;
 
//This is called when mouse is over apple01
function mouseOverApple01 (event:MouseEvent):void {
        mouseIsOverApple01 = true;
}
 
//This is called when mouse is over apple02
function mouseOverApple02 (event:MouseEvent):void {
        mouseIsOverApple02 = true;
}
 
//This is called when mouse is out of apple01
function mouseOutApple01 (event:MouseEvent):void {
        mouseIsOverApple01 = false;
}
 
//This is called when mouse is out of apple02
function mouseOutApple02 (event:MouseEvent):void {
        mouseIsOverApple02 = false;
}
 
 
//This function animates apple01
function enterFrameApple01 (event:Event):void {
 
        //If mouse is over the apple, decrease the blur
        if (mouseIsOverApple01 == true) {
                blur.blurX -= blurSpeed;
                blur.blurY -= blurSpeed;
        }
 
        /* If the mouse is out of the apple, and the blur is not over 20, we increase the blur. */
        if (mouseIsOverApple01 == false && blur.blurX <= 20) {
                blur.blurX += blurSpeed;
                blur.blurY += blurSpeed;
        }
 
        /* We need to assign the filter again since we made changes to the blur filter. */
        apple01.filters = [blur];
}
 
//This function animates apple02
function enterFrameApple02 (event:Event):void {
 
        //If mouse is over the apple, increase the blur until we reach 100
        if (mouseIsOverApple02 == true && bevelFilter.blurX < 100) {
                bevelFilter.blurX += bevelSpeed;
                bevelFilter.blurY += bevelSpeed;
 
                //We need to assign a strength to the bevel to make it visible
                bevelFilter.strength = 5;
        }
 
        //If mouse is out of the apple02, decrease the blur
        if (mouseIsOverApple02 == false) {
                bevelFilter.blurX -= bevelSpeed;
                bevelFilter.blurY -= bevelSpeed;
        }
 
        /* If we are sure that no blur is applied, we make the whole bevel filter invisible (strength is 0). Otherwise we would see some ugly corners. */
        if(bevelFilter.blurX == 0) {
                bevelFilter.strength = 0;
        }
 
        /* We need to assign the filter again since we made changes to the bevelFilter filter. */
        apple02.filters = [bevelFilter];
}

You are done, test your movie! If you have any questions concerning this tutorial, please visit the forum. Have a nice day!

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

Related tutorials:

  1. Advanced Animation with Drawing API
  2. Snowfall Effect
  3. Magnifying Glass Effect
  4. Mask Animation with ActionScript 3
  5. MovieClip Scroller



Filed under: ActionScript 3 Advanced
Tags:

Comments

2 Responses to “Using Filters with ActionScript 3”
  1. Wow i have been searching for blur effects…

    And this is just so easy to use :D

    VERY good job

    Log in to Reply
  2. chad says:

    so if i want to have multiple movieclips that are blurred and while hovering over one, it focuses, how would i go about doing that? i’ve tried to just apply the blur functions and variables to multiple movieclips, but what happens is either only one focuses, or but both focus at the same time…

    Log in to Reply

Leave a Reply

You must be logged in to post a comment.