Monday, 1 September 2008

The Stage is not the Document Class (or vice versa)

Just a quick one but this caught me out today. If you want to make the stage clickable from your Document Class you might include the following in your Main function.

public function Main()
{
addEventListener(MouseEvent.CLICK,handleClick);
}
To make any mouse click that happens anywhere on the stage trigger the handleClick function. However that's not going to work because although it's the top level class your Document Class isn't the same as your stage.

In order to get mouse clicks to register from an empty stage you'll need to add the event listener to the stage itself. Like so:
public function Main()
{
stage.addEventListener(MouseEvent.CLICK,handleClick);
}
I know this looks straightforward but it had me scratching my head for a while today. The stage is not the Document Class even though at that level you addChild to the Document Class to add it to the stage. I should find something technical regarding this to explain but I can't right now.

No comments:

Post a Comment