7.7. Checking for Mouseover

Problem

You want to check whether the mouse pointer is over a movie clip.

Solution

Use the onRollover( ) event handler, or use the hitTest( ) method within an onEnterFrame( ) method.

Discussion

In many cases, the onRollOver( ) and onRollOut( ) event handler methods, as shown in Recipe 7.5, are the easiest way to respond to the mouse pointer rolling over a movie clip. However, this technique fails when you drag one clip over another, such as when using a clip as a custom mouse pointer. Here is an example to illustrate the point:

// Include the DrawingMethods.as file from Chapter 4 for its drawRectangle(  ) and
// drawCircle(  ) methods.
#include "DrawingMethods.as"

// Create a rectangle_mc movie clip on the Stage.
_root.createEmptyMovieClip("rectangle_mc", 1);
rectangle_mc.lineStyle(1, 0x000000, 100);
rectangle_mc.beginFill(0, 100);
rectangle_mc.drawRectangle(100, 200, 10);
rectangle_mc.endFill(  );
rectangle_mc._x = 100;
rectangle_mc._y = 100;

// Create onRollOver(  ) and onRollOut(  ) methods such that when the mouse rolls on and
// off the movie clip, it goes to 50% transparency and back.
rectangle_mc.onRollOver = function (  ) {
  this._alpha = 50;
};

rectangle_mc.onRollOut = function (  ) {
  this._alpha = 100;
};

// Create a circle_mc movie clip.
_root.createEmptyMovieClip("circle_mc", 2);
circle_mc.lineStyle(1, 0x000000, 100);
circle_mc.beginFill(0xFFFFFF, 100);
circle_mc.drawCircle(50);
circle_mc.endFill(  );

// Create onPress(  ) and onRelease(  ) methods such ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.