Jan
Jan

Reputation: 43169

How to set Mouseenabled in AS3 properly

I have a background movie clip with objects flying on top of it (it is a game). Now, I'd like to shoot holes in the movie clip when no object lying on top has been hit. I was fiddling around with mouseEnabled on both the background mc and the children mcs but did not have any success. What is the best way to distinguish between these two events (background hit vs. flying object hit)?

Upvotes: 1

Views: 658

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

If you are listening for a mouse event on a common parent of both the background and the flying objects, you can use the event.target property to see which item dispatched the event.

Setting a display object's mouseEnabled property to false, keeps it from dispatching mouse events, however, it does not keep it's children from doing so. mouseChildren = false will accomplish that.

so if, on your flying things and your background, you set their mouseChildren property to false, when you click them, the target property will always be the object itself. (otherwise the target could be one it's children).

so your event handlers could look like this:

function(e:Event):void {
    if(e.target == myBackgroundInstance){
        //do something with the background
    }

    if(e.target is myCommonFlyingThingClass){
        var flyingThing:myCommonFlyingThingClass = e.target as myCommonFlyingThingClass;
        //do something with the flying thing
    }
}

Upvotes: 1

Related Questions