Skip Navigation.

IMAGE LAB Help: Flash Programming Tutorial: Bug Zapper

For this section, we'll make a very simple script that will let a user drag an object, then do a hit test and respond when the hit happens. In this case, we'll be making a bug zapper and a bug. This example is based on the example in the ActionScript Flash book (page 22-23) and we'll be using it, explaining it in greater detail, and expanding on it in this tutorial.

Making Symbols

Open Flash. Click on insert - new symbol. In the window that pops up, select "Movie Clip" and name this "bug". Draw some sort of bug shape on the movie clip - this will be the bug. Then, create another symbol the same way. This symbol should also be a movie clip. Name it "zapper". Draw an object here that will be the zapper.

Insert New Symbol
Adding a new symbol

Naming the Symbol
Naming the symbol

Once you've made the two symbol-movie clips and named each, open your library and drag an instance of each symbol to the stage of the main scene. Now, you're stage should have an two images on it - one of the bug and one of the zapper.

Actions Window

Click on the bug and open your actions window. Using the actions window, you can use Expert Mode to write the program without help, or you can use Normal Mode and select the quick buttons on the left to help write the code. For now, use the quick left buttons.

The code that needs to be in the right hand panel is:

onClipEvent (load) {
	startDrag (this, true);
	zap = new Color(this);
}
onClipEvent (enterFrame) {
	if (this.hitTest(_root.zapper)) {
		//makes color change to black if bug (this) hits zapper
		zap.setRGB(0);
		//makes the dead bug fall down by 50 when the zap occurs 
		//otherwise the bug remains stuck on the zapper 
		setProperty (_target, _y, _y+50);
		//drops bug zapper color to 50% alpha so it gets lighter when the
		//zap occurs
		setProperty (_root.zapper, _alpha, 50);
		//makes it so user can't drag around the dead bug
		stopDrag ();
	} else {
		//makes it so the game continues otherwise
		setProperty (_root.zapper, _alpha, 100);
	}
}

Notice the lines with the two slashes (//). These are comments to make the code easier to read, but they don't actually factor into the program.