Link to home
Start Free TrialLog in
Avatar of rosedewar
rosedewar

asked on

Using addEventListener in ActionScript 3

Hello

I am struggling to make the transition from AS2 to AS3 and I need some help understanding how Flash AS3 handles mouse actions using the addEventListener.

I am authoring a simple Flash presentation which has a movie clip on the stage ('myMainMovieClip') and this contains a second movie clip ('myChildMovieClip').

In frame 1 of the main timeline (on the stage), I have two event listeners detecting MOUSE_OVER and MOUSE_OUT actions. (see code below).

When I test this, if the mouse cursor passes over myChildMovieClip which is inside myMainMovieClip, the parent movie clip then starts looping wildly.

This problem goes away when I change myChildMovieClip to a graphic instance instead of a movie clip instance. So why is this, and is there any way to have a movieclip within a movieclip and so it doesn't affect its parent when the addEventListener is used?

Can someone please explain to me what is going on here and why this happens?

Many thanks!
function mainOver(myevent:MouseEvent):void {
        // Navigate to "Over" label of myMainMovieClip
	myMainMovieClip.gotoAndPlay("Over");
}
 
function mainOut(myevent:MouseEvent):void {
       // Navigate to "Out" label of myMainMovieClip
	myMainMovieClip.gotoAndPlay("Out");
}
 
myMainMovieClip.addEventListener(MouseEvent.MOUSE_OVER, mainOver);
myMainMovieClip.addEventListener(MouseEvent.MOUSE_OUT, mainOut);

Open in new window

Avatar of Eaddy Barnes
Eaddy Barnes
Flag of United States of America image

try gotoAndStop  and not gotoAndPlay

function mainOver(myevent:MouseEvent):void {
        // Navigate to "Over" label of myMainMovieClip
        myMainMovieClip.gotoAndStop("Over");
}
 
function mainOut(myevent:MouseEvent):void {
       // Navigate to "Out" label of myMainMovieClip
        myMainMovieClip.gotoAndStop("Out");
}
 
myMainMovieClip.addEventListener(MouseEvent.MOUSE_OVER, mainOver);
myMainMovieClip.addEventListener(MouseEvent.MOUSE_OUT, mainOut);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of john_hollings
john_hollings
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rosedewar
rosedewar

ASKER

Brilliant. That nailed it. Thank you so much.