Chunky Chunk
Chunky Chunk

Reputation: 17217

Actionscript Receiving Mouse Events For Lower Indexed And Partially Covered Display Objects?

i have 2 sprites on stage. bottomSprite is added to the display list first, followed by topSprite. topSprite partially covers bottomSprite.

i've added an event listener to bottomSprite for MouseEvent.MOUSE_MOVED notifications to simply trace the mouseX and mouseY coordinates. however, the notification doesn't work for the parts of bottomSprite that are covered by topSprite.

var bottomSprite:Sprite = new Sprite();
bottomSprite.graphics.beginFill(0x666666, 0.5);
bottomSprite.graphics.drawRect(150,150, 150, 150);
bottomSprite.graphics.endFill();
addChild(bottomSprite);

var topSprite:Sprite = new Sprite();
topSprite.graphics.beginFill(0x00FFFF, 0.5);
topSprite.graphics.drawRect(250,50, 150, 150);
topSprite.graphics.endFill();
addChild(topSprite);

bottomSprite.addEventListener(MouseEvent.MOUSE_MOVE, traceCoords);
function traceCoords(evt:MouseEvent):void
    {
    trace ("Coord = X:" + bottomSprite.mouseX + ", Y:" + bottomSprite.mouseY);
    }

Upvotes: 1

Views: 252

Answers (1)

Amarghosh
Amarghosh

Reputation: 59451

topSprite.mouseEnabled = false;

Note that the topSprite won't receive any more mouse events. You cannot make both of them receive mouse events simultaneously. Because mouse movement happens either on top of bottomSprite or topSprite, not both of them - they are siblings.

Had topSprite been a child of bottomSprite, later will receive mouse events when you hover across the former - because a child is part of its parent.

Upvotes: 2

Related Questions