0
votes

I'm trying to create a simple Air/Flex app which loads in user selected image and then resizes the window so that the selected image fits into it. If I have understood correctly, I need to set stage.scaleMode = StageScaleMode.NO_SCALE. However, when I call this I just get Error #1009 "Cannot access a property or method of a null object reference." Below is my default application mxml:

        <?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" 
                       initialize="init();"
                       applicationComplete="complete();"
                       xmlns:local="*">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->

    </fx:Declarations>


    <mx:MenuBar id="myMenubar" width="832" itemClick="menuHandler(event);" labelField="@label" depth="0">
        <fx:XMLList xmlns="">
            <item label="File">
                <item label="New" id="new"/>
                <item label="Open" id="open"/>
                <item label="Save" id="save"/>
                <item label="Save As" id="saveas"/>
                <item label="Quit" id="quit"/>
            </item>

            <item label="Edit">
                <item label="Undo" id="undo"/>
                <item label="Redo" id="redo"/>
                <item label="Preferences" id="preferences"/>
            </item>

            <item label="Level">
                <item label="New Room" id="newroom"/>
                <item label="Properties" id="properties"/>
            </item>

            <item label="Objects">
                <item label="Clickable" id="clickable"/>
                <item label="Character" id="character"/>
                <item label="Door" id="door"/>
                <item label="Treasure" id="treasure"/>
            </item>
        </fx:XMLList>


    </mx:MenuBar>

    <s:SpriteVisualElement id="flashpunk" depth="-2">
    </s:SpriteVisualElement>    

    <fx:Script>

        <![CDATA[

        import mx.collections.*;
        import mx.controls.Alert;
        import mx.core.IUIComponent;
        import mx.events.MenuEvent;
        import flash.display.StageScaleMode;

        public var Maini:Main = new Main; 

            private function init():void
            {
                flashpunk.addChild(Maini);

            }

            private function complete():void
            {

                stage.scaleMode = StageScaleMode.NO_SCALE;
            }

            private function menuHandler(evt:MenuEvent):void  
            {
                // Don't open the Alert for a menu bar item that 
                // opens a popup submenu.

                    Alert.show("Label: " + evt.item.@label + "\n" + 
                        "Data: " + evt.item.@data, "Clicked menu item");
                    Maini.foo();

            }


        ]]>

    </fx:Script>



</s:WindowedApplication>

What I'm doing wrong?

Also, after I have set scalemode, how exactly I can set the window size to a new size? My debug player doesn't seem to recognize even [SWF(width = "1200", height = "600")] or attempts to set stage width and height. I'm using Flash builder 4.6.

1
In a Flex component, you won't be able to access the stage unless the item is on the display list. For a Flex Component, this won't happen until after the ApplicationComplete event fires from the apps main application. Based on the code you've shared, I'm unsure if this is the issue. - JeffryHouser
I tried add ApplicationComplete event function, but with it I get same error. I updated my whole mxml code to the original post. - Tumetsu
Nevermind, I got it working. Apparently I had some older build files around which messed the build. - Tumetsu
As a followup to what @www.Flextras.com stated, ApplicationComplete will only be available on the TopLevelApplication. For all other components, you want to use the AddedToStage event instead before executing any code that requires the stage. - Josh

1 Answers

0
votes

According to http://killerspaz.wordpress.com/2010/10/24/access-to-the-stage-in-flex/ - During component initialization, you can access the stage through this.systemManager reference.

systemManager.stage.scaleMode = StageScaleMode.NO_SCALE;