Always Get Better

Archive for the ‘Flex’ Category

Accessing the stage in Flex

Sunday, November 9th, 2008

When creating components in Flex, designers sometimes need to attach events to the main stage. Unless the application has reached creationComplete, the stage property of custom components will be null.

If you need to, for example, attach a MOUSE_MOVE event to the application stage from a component that doesn’t include a creationComplete override, you have two options.

Wait for Event.ADDED_TO_STAGE

addEventListener( Event.ADDED_TO_STAGE, function(e:Event):void
stage.addEventListener( MouseEvent.MOUSE_MOVE, myMouseMoveEvent );

Attach Through the System Manager Object

The system manager parents all displayable elements within the Flex application. It is an elegant way of accessing the stage through custom flex components which have not yet been added to it.

systemManager.stage.addEventListener( MouseEvent.MOUSE_MOVE, myMouseMoveEvent );