RSS link icon

.

Apply shadow with actionscript 3


This is a very neat trick, one I actually learned somewhere on the internet a while ago, (sorry cant remember where), but basically what is does is apply dropshadow dynamically through your code, to any object you tell it to.

dropshadow in flash

So here we go, its quite easy to the function looks quite messy so try to follow my explanation comments for each line of code.

First we need to draw something on the stage to apply this dropshadow to, I just drew a simple shape, then right click and convert it to a movie clip.

Now in the properties panel give it an instance name, I named mine "my_object".

dropshadow in flash

Now just go to the actionscipt panel and type in the following code. my comments start with //.

//First we declare an object, a dropshadow filter and name it my_shadow for further reference.
var my_shadow:DropShadowFilter = new DropShadowFilter();
//Now we apply some properties to our new filters object, this first property is the color, and we set that to black, as most shadows are.
my_shadow.color = 0x000000;
//These next two properties we set, are the position of our dropshadow relative to the object,
//This means 8 px from the object on both the x and y axis.
my_shadow.blurY = 8;
my_shadow.blurX = 8;
//And here we set an angle for the dropshadow, also relative to the object.
my_shadow.angle = 100;
//Setting an alpha for the shadow. This is to set the strength of the shadow, how "black" it should be.
my_shadow.alpha = .5;
//and here we set the distance for our shadow to the object.
my_shadow.distance = 6; 
//Now we define an array for our filter with its properties to hold it. This will be the final object we refer to when we need to apply it to something.
var filtersArray:Array = new Array(my_shadow);
//The last step is to take our movie clip that we made at the beginning, so we take our object and apply the filtersArray.
my_object.filters = filtersArray;

Now test your movie and see, you should have a dropshadow.


Admin Bob says: 2008-05-14

Sure you can, just give the text field an instance name.

Eric says: 2008-05-14

Hi there I want to apply a dropshadow to text im my movie, can i use this script for it eric

Admin Bob says: 2008-03-02

Hi Son of Bryce, you thanks, your right if this is done in an external .as file you will need to import the filter class first. :-)

Son of Bryce says: 2008-02-27

I found this to be very good quick reference but you left out a very important part! None of this will work in code unless you include the filter classes. import flash.filters.DropShadowFilter; or import flash.filters.; IChr(34)m using all of my code in external Actionscript files and your code wonChr(34)t work unless you include the filter classes. Thanks for the reference!