tags:

views:

740

answers:

4

I am trying to set the scaleMode on my AIR app. which is set via stage.scaleMode. However when I try to reference stage it returns as null. I do not know why?

Here is the entire app:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import flash.display.StageDisplayState;
            private function init():void
            {
                var s:Stage = this.stage; //<<-- this.stage == null...why??
                s.scaleMode = StageScaleMode.EXACT_FIT;
            }
        ]]>
    </mx:Script>
</mx:WindowedApplication>
+1  A: 

Try adding an event listener for Event.ADDED_TO_STAGE, or the addedToStage mxml attribute. You should be able to reference the stage then.

Alex Jillard
+3  A: 

Try windowComplete instead of creationComplete. Also:

[If] you are trying to add eventListeners to the stage right from the start of your application execution, this will only work with updateComplete() and applicationComplete().

Source: http://www.wietseveenstra.nl/blog/2007/02/understanding-the-flex-application-startup-event-order/

Typeoneerror
A: 

To be safe, in both Web and AIR apps, I generally use the applicationComplete event to mark completion of the startup cycle, before I go looking for the stage object. From the docs:

After all components are created and drawn, the Application object dispatches an applicationComplete event. This is the last event dispatched during an application startup.

If you wait for applicationComplete, you can be sure everything's ready for ya.

Christian Nunciato
+1  A: 

Hell,

You could try with

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication 
       xmlns:mx="http://www.adobe.com/2006/mxml" 
       creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import flash.display.StageDisplayState;
            private function init():void
            {
                this.systemManager.stage.scaleMode = StageScaleMode.EXACT_FIT;
            }
        ]]>
    </mx:Script>
</mx:WindowedApplication>

That should do the trick :)

Adrian


My Flex Blog

Adrian Pirvulescu