A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [RESOLVED] Referencing Stage within custom "Global" class

  1. #1
    Senior Member
    Join Date
    Jan 2006
    Posts
    133

    resolved [RESOLVED] Referencing Stage within custom "Global" class

    Hello All,

    I'm new to OOP and I still haven't gotten a handle on coding classes and the appropriate syntax with all the "public" "private" "static" keywords, etc.
    Anyway, one thing I miss in AS3.0 is having "out-of-the-box" global variables and functions.
    I've read a lot of forums and articles about making a simple "MyGlobal" class that could hold variables and functions which could be called from the timeline as "MyGlobal.doSomething()"
    However, I seem to be running into a problem trying to reference "stage" from within one of the global functions... I imagine this is because "stage" is an instance property.
    I'm basically trying to add/remove an event listener to the stage whenever the global function "MyFunction" is called.

    MyGlobal.as:
    PHP Code:
    package{
        
        
    import flash.display.*;
        
    import flash.events.KeyboardEvent;
        
        public class 
    MyGlobal{
            
            public static var 
    objVars:Object = new Object();

            public static function 
    MyFunction(Previous_mc:MovieClipTarget_mc:MovieClip):void{
                
    // Remove the previous keyboard listener
                
    stage.removeEventListener(KeyboardEvent.KEY_DOWNPrevious_mc.fOnKeyDownfalse);
                
    // Add a keyboard listener that targets the Target_mc's fOnKeyDown function
                
    stage.addEventListener(KeyboardEvent.KEY_DOWNTarget_mc.fOnKeyDownfalse0true);        
            }
            
        }
        

    MyApplication.fla Timeline:
    PHP Code:
    MyGlobal.objVars.myVar1 "Hello World";
    trace(MyGlobal.objVars.myVar1); // output: Hello World

    MyGlobal.MyFunction(myMC1_mcmyMC2_mc); 
    Compiler Error:
    Code:
    1120: Access of undefined property stage.
    Am I missing an import statment, or is there someway I need to declare stage as this.stage?
    Last edited by badaboom55; 01-25-2010 at 03:11 PM.

  2. #2
    Flactionscrish Baby Minion's Avatar
    Join Date
    Nov 2005
    Location
    Planet Earth
    Posts
    312
    http://help.adobe.com/en_US/AS3LCR/F...ect.html#stage

    The stage property is only accessible by DisplayObject that are in a display list, or the main class, or timeline.

    What I suggest, is making a variable in your MyGlobal class called stageRef. At the beginning of your application, set the stageRef = stage. Then all of your functions that require a reference to the stage will be able to access it through the stageRef class.


    PHP Code:
    package{
        
        
    import flash.display.*;
        
    import flash.events.KeyboardEvent;
        
        public class 
    MyGlobal{
            
            
    // added stage ref
            
    public static var stageRef:Stage;
            public static var 
    objVars:Object = new Object();

            public static function 
    MyFunction(Previous_mc:MovieClipTarget_mc:MovieClip):void{
                
    // Remove the previous keyboard listener
                
    stageRef.removeEventListener(KeyboardEvent.KEY_DOWNPrevious_mc.fOnKeyDownfalse);
                
    // Add a keyboard listener that targets the Target_mc's fOnKeyDown function
                
    stageRef.addEventListener(KeyboardEvent.KEY_DOWNTarget_mc.fOnKeyDownfalse0true);        
            }
        } 

    ktu[k-two]
    he who hesitates is lost; so i guess i'll wander intently

    Are you sure this is real?
    Life is Love, Love is Blind, Blind we go through Life.
    Life isn't hard, dealing with your self is.

    The concept of life in a human brain is weakening day after day. Live every day like its your last. Take the chances, and opportunities, and never let authority push you around for fun.


  3. #3
    Senior Member
    Join Date
    Jan 2006
    Posts
    133
    So, I would just have to add "MyGlobal.stageRef = this.stage;" to my timeline code, like so:

    PHP Code:
    MyGlobal.stageRef this.stage;

    MyGlobal.objVars.myVar1 "Hello World";
    trace(MyGlobal.objVars.myVar1); // output: Hello World

    MyGlobal.MyFunction(myMC1_mcmyMC2_mc); 
    Is there anyway to declare the stageRef within the MyGlobal class .as file itself? Instead of having to define stageRef at runtime via frame actions?

    I suppose I understand that I'm not adding an instance of the MyGlobal class to the display list, so maybe I've answered my own question... it's not a deal-breaker either way.
    Writing "MyGlobal.stageRef = stage" at the beginning of every application isn't much of an inconveinence... but it would be nice if MyGlobal could just do it automatically by grabbing the "this.stage" of wherever "MyGlobal.something()" is being called... since MyGlobal will only be called by either 1) other class instances (MovieClips, Buttons, etc.) already existing in the Display List, or 2) by the actions on the root timeline (as above).

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Baby Minion's solution will work, but why do you need that in a global scope anyway? It seems more like something that should go in the document class.

  5. #5
    Senior Member
    Join Date
    Jan 2006
    Posts
    133
    If you can explain briefly...
    Why/What are you suggesting should go in the document class? What are the benefits of the document class vs. what I'm trying to do?

    Reading a bit, I see that "the document class serves as a shortcut for creating an instance of a MovieClip or Sprite and adding it to the display list..."
    Does this mean if I use a document class, I don't have to declare stageRef = stage from the timeline?

    I'm not sure I need anything in the "global scope" as you put it... it's just what I was used to in AS2.0, because you could declare a function or variable as global by using "_global"... so usually I would just define my global stuff on Frame 1 of the Main Timeline
    So in attempting to transition over to AS3.0, I did a few searches on "AS3 global functions and variables". And the common solution was a custom class.

    To my thinking... I want variables within the global scope so that I don't have to write (using AS2.0 timeline coding logic):
    PHP Code:
    MovieClip(parent.parent.parent.parent.parent.parent).myVar1 "Hello"
    Similarly, if I have a function that isn't necessarily project specific... such as a function that converts us dollars to bhutanese money... I don't want to have to write:
    PHP Code:
    MovieClip(parent.parent.parent.parent.parent.parent).fConvertUSDtoBHU(263.00); 
    Now, if I had several common functions, I don't think I'd necessarily want a ".as" file for each... so why not bundle them into 1 ".as" file which I can just reference as needed from ANY .fla:
    PHP Code:
    myGlobal.fConvertUSDtoBHU(263.00); 
    If it's flawed logic, I'd love to hear why, or how anybody would handle a set of functions which may or MAY NOT be needed in any given project?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Definitely do not use parent.parent.parent etc. At the very least keep a reference to the instance with the var or function you need.

    The document class is the class which will be associated with the root. Properties and functions defined I'm the document class will be available in the main timeline, and by calling them with the root reference (after casting).

    What you have described with the convert function is a static utility class. There's nothing wrong with those, but try to keep them stateless.

  7. #7
    Senior Member
    Join Date
    Jan 2006
    Posts
    133

    Question

    Quote Originally Posted by 5TonsOfFlax View Post
    The document class is the class which will be associated with the root. Properties and functions defined I'm the document class will be available in the main timeline, and by calling them with the root reference (after casting).
    So do you mean by using a document class, I could call a function within the document class like this?

    PHP Code:
    MovieClip(root).myDocClassFunction(); 
    Why is that better than this:

    PHP Code:
    myGlobal.myGlobalClassFunction(); 
    Quote Originally Posted by 5TonsOfFlax View Post
    What you have described with the convert function is a static utility class. There's nothing wrong with those, but try to keep them stateless.
    What do you mean by stateless?

    Sorry, I know this is a forum, not a classroom.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Within the document class or the main timeline (which IS the root), you could just do:
    Code:
    myDocClassFunction();
    From other instances/classes you would still need to get a reference to the document instance to call that function.


    By stateless, I mean that it does not keep any info around in variables between function calls. It should not have any modes set or things like that. This is not required, it's just good practice. If you do need state, then the class should not be static, you should get an instance to store in whatever scope you need it.

    The reason to avoid storing state in a static class is that other stuff you might not plan on could alter that state. Say your convert utility was written in a stateful manner, where you'd have to set the to-unit and the from-unit, then call convert. Between calls to convert, another class might change the units, and mess up your values. If instead, you had function convert(from, to, value):Number, then each call would be completely independent of anything else that has happened to the class.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center