fcOnTheWeb Logo Web technologies, made easy.

fcOnTheWeb Red DotFilters in ActionScript - Drop shadow and blur

Filters are a great way to add a bit of effect to a MovieClip, and are really quick and easy to do with Flash. But what if you're adding your MovieClips dynamically and you want to add a filter to them? Well today we look at how you can add the same filters with ActionScript that you can through the normal interface. And it's really easy!

We can use the same code for both ActionScript 2 and ActionScript 3, so we only need one set of instructions.

We're going to look at two filters today - a drop shadow and a blur. These filters are really easy to add to a MovieClip with ActionScript, so let's get started.

First let's take a look at the blank MovieClip, and then what it looks like with the filters applied:

Your version of Flash Player is lower than the required 10.0.0.

Click below to upgrade to the latest version and then try again.

Get Adobe Flash player

On the left we have the plain MovieClip - just a blue square. In the centre is the same MovieClip but with our drop shadow filter applied to it through ActionScript. And on the right is the blue square again, but this time with a blur filter applied to it.

First we'll look at how to apply the drop shadow filter. It's quite simple and just a matter of adding in the right parameters. For the sake of simplicity, we've added all the parameters to match the default values by Flash when you choose the filter through the interface. Let's see the code:

import flash.filters.DropShadowFilter;

var fltDropShadow = new DropShadowFilter(5, 45, 0x000000, 1, 5, 5, 1, 3, false, false, false);

my_mc_dropshadow.filters = [fltDropShadow];

And that's all the code we need to add our drop shadow filter to our MovieClip. That sure is a long string of values though. Here's a break down of what they all are, in order obviously:

So in our example we've used the same values as Flash gives as default when you choose a drop shadow filter through the interface.

We only need three lines of code to get the filter working. The first is to import the filter library so we can access it through ActionScript. The second line is where we declare our filter, and set up all the values which will dictate the attributes of our filter. Out filter is called fltDropShadow. And our third line simply adds the filter to our MovieClip with the instance name of my_mc_dropshadow.

Now let's take a look at the ActionScript we use to make our blur filter - the MovieClip on the right side of the example above.

import flash.filters.BlurFilter;

var fltBlur = new BlurFilter(5, 5, 3);

my_mc_blur.filters = [fltBlur];

This time it's even easier as we have less parameters to deal with.

Again we have to import the filter, this time the BlurFilter.

Next we declare our filter. We only need three parameters:

With our fltBlur declared, and Flash's default values put in, we just need to add our filter to our my_mc_blur MovieClip.

Let's take a look at what all this code looks like together, as we've used in our example:

import flash.filters.DropShadowFilter;

import flash.filters.BlurFilter;

var fltDropShadow = new DropShadowFilter(5, 45, 0x000000, 1, 5, 5, 1, 3, false, false, false);

var fltBlur = new BlurFilter(5, 5, 3);

my_mc_dropshadow.filters = [fltDropShadow];

my_mc_blur.filters = [fltBlur];

And that's all there is to it. We recommend you have a play with these filters, and change the values to see the effect it has on the output. When you're confident with these two, have a play with other filters in the library.

Adding filters with ActionScript it easy.

As always, you can download the source code we used for this example: filters_in_actionsript.zip.

ferrari_chris


Add your comment on this article below:

Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.

Form submitting now...

Name:

Website:

Email address (not displayed):

Enter your comment below: