8 Replies Latest reply on Oct 16, 2008 11:19 AM by Peter Lorent

    Adding objects to the stage & acceessing stage properties - I'm a bit confused..

    tomfoo
      Hi,

      I'm a little confused on two fronts regarding display programming:

      A. What the best way to add objects to the stage?
      B. How to access stage properties.

      I can do both of these when the project is an 'Action Script Project', but I'm having trouble when it is a 'Flex Project' (e.g. an MXML file). See the two attached files with questions included in the comment to see exactly what I mean.


      A. What the best way to add objects to the stage?
      =====================================

      In an action script project that extends Sprite I can just call addChild()

      In an MXML project I can add objects to the stage by first adding them to a UIComponent, then adding that to the stage.

      1. Is that the best way to do it?

      In this doc: http://livedocs.adobe.com/flex/3/html/help.html?content=05_Display_Programming_02.html it says that
      quote:

      Each SWF file has an associated ActionScript class, known as the main class of the SWF file. When Flash Player opens a SWF file in an HTML page, Flash Player calls the constructor function for that class and the instance that is created (which is always a type of display object) is added as a child of the Stage object. The main class of a SWF file always extends the Sprite class


      2. why cant I just add a sprite object direct to the stage without the UIComponent?
      3. What is the 'main class', and how can i get access to it?

      B. How to access stage properties.
      =========================

      In an action script project that extends Sprite I can just call trace(stage.framerate)

      But in an MXML file I cannot figure out how to access the stage properties.
      See the attached code for the things that I tried, but which didnt work.
      In the page: http://www.actionscript.org/forums/showthread.php3?t=140655
      It says
      quote:

      For stage access you have a couple of rules:

      * natively, only display objects have inherent access to stage
      * display objects only have a valid reference to stage if they're within the stage's display list (on the screen, after added to it with addChild)
      * The only display objects which immediately have access to the stage before the use of addChild (in their constructor) are the document/application class instance (root) and any instance from the library placed on the timeline (not created with AS).
      * Non-display object classes have to be passed or assigned a reference to the stage from another object which already has access to it - they cannot access it otherwise.



      4. How do I access the stage properties in the MXML project?
      5. I suspect this is related to the "main class", which I dont yet understand - is it?

      Sorry for the long post.

      cheers

      tom