A week or so ago, I ran into a little problem while working on an extensive Flash-based project. (It would be Flex-based, as I was developing it using Flex builder; however, I am completely sidestepping any and all Flex framework libraries -- as such, it is Flash-based).
I experienced a bit of a problem with mouseOver. Consider this code (please forgive any errors; I've been using C++ the last few days):
//create two containers
var container1 : Sprite = new Sprite();
var container2 : Sprite = new Sprite();
//add an item to each container
var item1 : Sprite = new Sprite();
var item2 : Sprite = new Sprite();
container1.addChild(item1);
container2.addChild(item2);
//put some content in each item - a 10px square
item1.graphics.beginFill(0xFF0000);
item1.graphics.drawRect(0, 0, 10, 10);
//have item2 be transparent
item2.graphics.beginFill(0xFF0000, 0);
item2.graphics.drawRect(0, 0, 20, 20);
//set mouseEnabled for item2 to false
item2.mouseEnabled = false;
//add an event listener to item1.
item1.addEventListener(MouseEvent.MOUSE_DOWN, function(event : MouseEvent) { trace("test"); });
//and to container2
container2.addEventListener(MouseEvent.MOUSE_DOWN, function(event : MouseEvent) {
trace("oops");
});
//add both objects to the stage
stage.addChild(item1);
stage.addChild(item2);
This is pretty straightforward. I had, mistakenly, expected that, since item2 did not have mouse enabled, that events in its target range would pass through to the object below (item1). However, instead, the object containing item2 was called. Thus, "oops" will be printed in the above example.
In my situation, I had an overlay sitting on top of my actual content; sometimes (if the mouse was enabled), I wanted that overlay to get mouse events. Other times, not so much. As all overlays were grouped together in a special container, and that container did support mouse events, that container's mouse events were called.
The Reason
The reason, I have managed to deduce, is due to the way Flash detects hit areas. The hit area of any Sprite is determined by what pixels have been set in that sprite. It does not matter if the pixels have been set by a child sprite or the sprite itself (and it does not matter if those pixels are transparent or opaque, either).
It took me an hour or two to debug. I don't like bugs, I really don't like it if they take more than a minute to fix, I dislike it much more if they take more than ten minutes, and I hate it if they take more than twenty minutes. Truthfully, I don't care for code-writing itself all that much (I prefer art), but as I don't have an army of developers at my disposal, I have to make do with my skills (which I like to think are pretty significant - I have been programming since I was 10 years old).