Areas Behind Display Objects and Mouse Events in Flash CS3

If you assign a mouse event listener to a display object, then portions of the object that are behind another interactive display object will not respond to the events. That is because AS3 notifies about a mouse event only the front-most object.

Our movie contains static text, several dynamic text boxes created at autoring time (globalCoords, localCoords, numClicks), and two Sprites created programmatically. These Sprites, spBoard and spDisk are children of the MainTimeline. spDisk is added second, so it is in front of spBoard.

As you can see from the source code below, we add to the Stage the listener that listens to MouseEvent.MOUSE_MOVE and displays global coordinates of the mouse pointer with respect to the Stage and local coordinates with respect to spBoard. We add a listener to spBoard that listens to MouseEvent.CLICK. The listener counts the number of clicks and displays that number in numClicks box.

We notice that when the area of spDisk is clicked, the click is not registered. This is because AS3 notifies of the event only the front-most InteractiveObject; that is, spDisk. spBoard is not notified. To combat this often undesirable effect, we use spDisk.mouseEnabled=false setting.

 

var spBoard:Sprite = new Sprite();

spBoard.graphics.lineStyle(0,0x333333);

spBoard.graphics.beginFill(0xFFFF99);

spBoard.graphics.drawRect(0,0,200,200);

spBoard.graphics.endFill();

 

var spDisk:Sprite = new Sprite();

spDisk.graphics.lineStyle(0,0x000000);

spDisk.graphics.beginFill(0x660000);

spDisk.graphics.drawCircle(0,0,40);

spDisk.graphics.endFill();

 

this.addChild(spBoard);

spBoard.x = 50;

spBoard.y = 65;

 

this.addChild(spDisk);

spDisk.x = 120;

spDisk.y = 120;

 

var clicksCounter:Number=0;

numClicks.text=String(clicksCounter);

 

stage.addEventListener(MouseEvent.MOUSE_MOVE,stageMove);

 

function stageMove(evt:MouseEvent):void {

 

globalCoords.text="( "+String(Math.round(mouseX*100)/100)+

" , "+String(Math.round(mouseY*100)/100)+" )";

 

localCoords.text="( "+String(Math.round(spBoard.mouseX*100)/100)+

" , "+String(Math.round(spBoard.mouseY*100)/100)+" )";

 

}

 

spBoard.addEventListener(MouseEvent.CLICK,boardClicked);

 

function boardClicked(evt:MouseEvent):void {

clicksCounter++;

numClicks.text=String(clicksCounter);

}

 

butDisable.addEventListener(MouseEvent.CLICK,disableClicked);

 

function disableClicked(evt:MouseEvent):void {

spDisk.mouseEnabled=false;

}

 

butReset.addEventListener(MouseEvent.CLICK,resetClicked);

 

function resetClicked(evt:MouseEvent):void {

clicksCounter=0;

numClicks.text=String(clicksCounter);

spDisk.mouseEnabled=true;

}

 

Download

The fla file linked below contains complete, commented source code for the Flash movie above.

Back to Intermediate Tutorials              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.