5 Replies Latest reply on Dec 18, 2008 6:19 AM by Newsgroup_User

    Referencing Stage in Flex

    ikas333
      Hi,

      Flash designer struggling to learn Flex. Any pointers on what I am doing wrong appreciated.

      How can I get the dimensions of the stage in a Flex App? I get null object reference error message for stage using the following type of approach. Works ok as AS3 project but not in Flex App.


      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="appInit()">
      <mx:Script>
      <![CDATA[
      import flash.display.*;

      private var ball:Image;

      private function appInit():void {
      ball = new Image();
      addChild(ball);
      ball.y = ball.stage.stageHeight / 2;
      ................etc

        • 1. Re: Referencing Stage in Flex
          Level 7

          "ikas333" <webforumsuser@macromedia.com> wrote in message
          news:gialdj$674$1@forums.macromedia.com...
          > Hi,
          >
          > Flash designer struggling to learn Flex. Any pointers on what I am doing
          > wrong
          > appreciated.
          >
          > How can I get the dimensions of the stage in a Flex App? I get null object
          > reference error message for stage using the following type of approach.
          > Works
          > ok as AS3 project but not in Flex App.
          >
          >
          > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          > layout="absolute"
          > creationComplete="appInit()">
          > <mx:Script>
          > <![CDATA[
          > import flash.display.*;
          >
          > private var ball:Image;
          >
          > private function appInit():void {
          > ball = new Image();
          > addChild(ball);
          > ball.y = ball.stage.stageHeight / 2;
          > ................etc
          >

          Looks like all you need is

          ball.y=ball.getExplicitOrMeasuredHeight()/2

          However, you should probably wait for the complete event on the image before
          you try to measure it.


          • 2. Re: Referencing Stage in Flex
            ikas333 Level 1
            Hi Amy,

            Would that not give me the height of the image?It is the size of the stage (browser viewport) that I am trying to get.

            I cannot understand why the stage is always a null object in Flex Apps when it works ok as an Actionscript project

            Thanks
            • 3. Re: Referencing Stage in Flex
              Level 7

              "ikas333" <webforumsuser@macromedia.com> wrote in message
              news:gib6il$q40$1@forums.macromedia.com...
              > Hi Amy,
              >
              > Would that not give me the height of the image?

              Yes. Why would you be referencing a subcomponent to try to get the project
              stage?

              > It is the size of the stage
              > (browser viewport) that I am trying to get.
              >
              > I cannot understand why the stage is always a null object in Flex Apps
              > when it
              > works ok as an Actionscript project

              I think you need to get the stage from the Application object, but, I think
              it will also have the same size as the Application.


              • 4. Re: Referencing Stage in Flex
                ikas333 Level 1
                Hi Amy

                I was trying to reference the stage from a subcomponent because my earlier attempts to reference the stage directly and via the application object gave a run time error (TypeError: Error #1009: Cannot access a property or method of a null object reference.). I had read that you could reference the stage from any visual component that had been added to the display list however I got the same error message. It appears that the stage is null in all my Flex apps so I don't know if I am doing something wrong (I am new to Flex). All works ok in Actionscript projects but not in my Flex Apps.

                After your reply I checked and application.height and application.width are ok for my purposes so I shall use these instead of stage in future. Guess using stage is more Flash than Flex. Still be interested if I am doing something wrong as the documentation I have read suggests application.stage should be ok in Flex apps.



                • 5. Re: Referencing Stage in Flex
                  Level 7

                  "ikas333" <webforumsuser@macromedia.com> wrote in message
                  news:giddal$nbg$1@forums.macromedia.com...
                  > Hi Amy
                  >
                  > I was trying to reference the stage from a subcomponent because my earlier
                  > attempts to reference the stage directly and via the application object
                  > gave a
                  > run time error (TypeError: Error #1009: Cannot access a property or method
                  > of a
                  > null object reference.). I had read that you could reference the stage
                  > from any
                  > visual component that had been added to the display list however I got the
                  > same
                  > error message. It appears that the stage is null in all my Flex apps so I
                  > don't
                  > know if I am doing something wrong (I am new to Flex). All works ok in
                  > Actionscript projects but not in my Flex Apps.
                  >
                  > After your reply I checked and application.height and application.width
                  > are ok
                  > for my purposes so I shall use these instead of stage in future. Guess
                  > using
                  > stage is more Flash than Flex. Still be interested if I am doing something
                  > wrong as the documentation I have read suggests application.stage should
                  > be ok
                  > in Flex apps.

                  I'd put in a break point and crawl through the objects that are in scope and
                  see what's going on...