home   |   about   |   time   |   appearance   |   space   |   other   |   tips & tricks    

Lighting Effect

In this chapter we will discuss the situation where the effect (the lightning effect in our case) takes longer than one frame (also another way of looping everything).

First thing you need is an image with a lightning (if you download the .zip version at the bottom of the page, you will get the PNG version of our lightning). Create a new file in Flash and use one of the next methods to create a black background for the effect: select the desired color for the background in the Properties Panel of the Stage or draw a black rectangle (or any shape) over the entire Stage (everything else will be placed on the layers above). Place the image in the stage (File/Import to Stage).

Let's create the effect. Convert the image into a Movie Clip, and then double click it to open the symbol for editing. Create a new layer above the existing one and convert it into a mask layer (right click on the layer). Be sure you know a few things about mask layers (the main thing about this special type of layer is that it does not matter what we draw inside, only the area covered by the shapes inside, because in that area, the masked layers are visible). The easiest way to recreate the lightning effect is to work frame by frame: in the first frame we uncover a bit of the image, in the next we extend that area to the right and so one until we got the entire lightning picture covered. We suggest you to use the Brush Tool (does not matter what color because it won't be visible in the final Movie). Whenever you pass to a new frame, convert it into a Keyframe, in that way you will continue drawing and the movie will have continuity.

Download the source file (bottom of the page) for a better understanding of the technique.

If you compile the movie at this time, you will see that the lightning takes place over and over, but we all know that it is a random effect so let's make it that way. But we need another way of looping so we turn to some special Action Script functions of the Movie Clips.

onClipEvent(load) = invoked right after the Movie Clip finishes loading and prepares to play;
onClipEvent(enterFrame) = invoked each time the Clip enters a new frame;
                          (this will be our looping method)

Read more about this in the Tips & Tricks section of the website.

Don't forget that these 2 functions have to be declared on the Movie Clip's Actions Panel (be sure you click the Movie Clip and then write the code; these is the most common error so be careful).

Here is the script for the Movie Clip:

onClipEvent( load ) {
	//declarations before playing the Movie Clip
	var effect:Boolean=false;	//shows us if the Movie Clip is playing or not
	var ok,Value,desiredValue:Number;	//these variables have the same meaning
	Value=30;	//there are 30 possibilities 0..29 
	desiredValue=1;	//the desired value is 1
}

onClipEvent( enterFrame ) {
	//this loops over and over
	if( effect == false ) {
		//if the MC is not playing
		ok=random(Value);	//we generate a number between 0..29
		trace(ok);	//print the value on the screen(compile the source)
		if (ok==desiredValue) {	//if the number "ok" and desired Value are equal
			trace("bingo");	//print "bingo" in the "Output Panel"
			play();	//play the Movie Clip
			effect=true;	//assign the Boolean variable the right value 
		
		}
	}
}

Also in the first frame of the Movie Clip add a "stop();" statement, meaning that the clip does not play until we want, and in the last frame "effect=false;", meaning the effect has finished playing.

Here is how the script works. After the declaration of the needed variables we need to assign values for "Values" and "desiredValue". In our example, we have 30 possibilities and only one valid so the chances are somewhere near 3.3% (you can change the value for "Values" to fit your needs). We added a new Boolean variable, "effect", which shows the state of the Movie Clip, if the value is "false", means we are in the first frame of the clip (nothing appears on the screen) and if the value is "true" the Clip is playing (we are not in the first frame).

If the Clip is stopped (effect==false), we will generate a number, and if this number is the desired one, we will play the lightning effect, making sure the Boolean variable becomes "true". During the effect, the statements inside "onClipEvent(enterFrame)" have no effect because we first check if the Clip stopped. When the Clip reaches the last frame, we signal this ending by modifying the Boolean Variable so that we can generate the numbers and prepare for the next time the effect appears in the Movie.

That's about all. Use your imagination and expand our examples. Remember that you can share your ideas by contacting us and we will provide the web space for your creations.

The next chapter is the last about randomly generated time and teaches you how to control multiple events triggered by a random number generator. Also you will see how one Movie Clip (we called it "The Brain") can coordinate all the Stage (learn how to organize the Movie Clips).

Download .ZIP Download .FLA
< Natural Moving EyesRandom Hamsters >