If you’re like me and enjoy working only with movie clip symbols, you’ll quickly come to realize that giving them the functionality of a button symbol is not a simple task.
This short tutorial will demonstrate how you can make any movie clip act like a button *. Here’s a quick rundown of the steps you need to take.
- In frame 1 on layer called actions in the root level, write the following in a new key frame.
MovieClip.prototype.mouseOver() {
if (this.hitTest (_root._x, _root._y) == true) {
return true;
} else {
return false;
}
}
This type of function is a prototype function that is automatically inhereted by all movie clips when an SWF is loaded.
-
To illustrate how to use the function we created, create a new movie clip on the root level. We’re going to make the color of the movie clip change when the mouse goes over the movie clip, and make it change again when you click it.
-
Insert the following script as an object action. That is, right-click the movie clip you created (while on the stage) and click Actions.
onClipEvent (load) {
var mouseOverColor = 0xFF0000; //red
var mouseDownColor = 0x00FF00; //green
var mouseOutColor = 0x0000FF; //blue
var init = false;
var mouseDown = false;
}
onClipEvent (mouseMove) {
if (this.mouseOver() == true) {
if (this.init == false) {
this.temp = new Color(this);
this.temp.setRGB(this.mouseOverColor);
this.init = true;
}
}
else {
if (this.init == true) {
this.temp.setRGB(this.mouseOutColor);
this.init = false;
}
}
onClipEvent (mouseDown) {
if (this.mouseOver() == true) {
if (this.mouseDown == false) {
this.temp.setRGB(this.mouseDownColor);
this.mouseDown = true;
}
}
}
onClipEvent (mouseOut) {
if (this.mouseDown == true) {
temp.setRGB(this.mouseOutColor);
this.mouseDown = false;
}
}
That’s it! Note that you can do whatever you like within the if/else statements. For example, you could play a movie clip within your button movie clip, or simply go to another frame of the clip instead of using the Color object to change the mc’s color.
If something doesn’t work please let me know, I pretty much did this off the top of my head. I’m sure this works in Flash MX and it should also work in Flash 5 as well.
Movie clips rock
Matt
*Note that by using the hitTest() method, you restrict the hit area of the button to the square containing the movie clip. This isn’t always a bad thing however.