A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: How evil -IS- root?

  1. #1
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967

    How evil -IS- root?

    I have heard MANY times that it is bad practice to use references to root. But I know I used _root many times in classes in AS2 and MovieClip(root) now in AS3.

    As an example for how I use it, I have a GameUpdater class that talks to my server. Since I am polling, all it does it setup the URLLoader, URLRequest, URLVariables, and the listener events to hear when the server responds. But now my CLASS knows that the server has new data, but my maintimeline is still baffled. So my COMPLETE listener adds server updates to an array in my main timeline something like: MovieClip(mRoot).serverUpdates.push("login successful")

    And it also sets appropriate properties in an object based on what was returned. Like in the login case, it would set a playerID property that comes from mySQL. My main timeline keeps an eye on that serverUpdates array and acts on anything it finds based on the content of the strings.


    Is this a horrible use of root? I could use a set of functions instead of using a class and avoid root references, but I like getting that code away from my main.

    My login screen is all on a separate movieclip (made in the flash api), and I have the code on that movieclip's timeline that handles what the buttons do, and the validation. It's all self-contained except that I have to make calls to that GameUpdater class variable that is on the main timeline. So my login and register buttons both reference MovieClip(root).

    Is this a horriible thing to do? How can I avoid it other than putting ALL code on the main timeline? Sure I could do that, but it doesn't seem very object oriented... I could also just use parent, or parent.parent but that seems a lot more evil than root to me, since the program would break if that parent changed.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I used _root all the time with AS1(2). But even then I considered putting code on anything but main timeline a bad habbit because you usually forget where it was when you need to update something.

    In AS3 I have everything in one single class except some stop() commands in movie clips to stop them auto-play. Well, I dont use OOP much, not even so-called "document class" but simply 1 class file that handles everything I need. Thats just to stop me going insane with the many annoying ways AS3 has.

    I am pretty sure its suppose to work with events and you extend eventDispatcher or something. Events have targets and other objects interested on the status can listen on events to know when something happens. But I am not correct person to say how you should work, if you have created system that works for you then stick with it and just make games. Using root or not has no effect on how good your game will be.

  3. #3
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    I never use "root" but I pass references to the main timeline throughout my projects.

    So the document class will instantiate a class by passing in a reference to the root like this.

    PHP Code:
    myClass = new Class(this); // pass in root reference
    myClass.init(this); // or for whatever reason I've passed in a reference in an init function 
    and inside the Class constructor would be something like this...

    PHP Code:
    private var rootTarget:DocumentClass;

    public function Class( 
    _rootTarget:DocumentClass ) {

         
    rootTarget _rootTarget;


    on an init function like this...

    PHP Code:
    public function init_rootTarget:DocumentClass ) {

         
    rootTarget _rootTarget;


    The benefit is that this works for any quick and dirty timeline work you might need to do. And also you can still embed this .swf into another one and all the reference will still work. If you use root, everything get messed up.

  4. #4
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Yeah, that is basically what I am doing, but didn't think to pass it the main timeline itself. Instead I made a pointless sprite to send. I'll go and change that to pass it 'this' instead. The less objects the better.

    I'm trying to use OOP as much as I can because I oneday would like to do this as my 9-5, and I suspect OOP will be the norm.

    So root is bad, but passing a reference to root and referring to that is okay. I guess this is an embedding issue then? So your swf does not break when inside another swf? Doing a find/replace to kill all root references and update the constructors would be pretty easy so I don't think I am going to sweat it much.

    Tonypa: Surprised to hear that you do everything in the main timeline (not surprised you don't put code in library objects, but surprised you don't use external .as files). Nothing wrong with that, I am just surprised, as I have been trying really hard to compartmentalize my code and break it apart because I thought that was good practice for reusability.
    Last edited by Alluvian; 05-27-2009 at 03:57 PM.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I like to compartmentalize things too. Part of that is reducing the dependencies between things. Some alternatives to consider (passing the root is a good start).
    1. pass the actual array to manipulate. (removes requirement that the array be named something particular)

    2. have the root implement an interface with methods to call, have a setWhatever method in the child that takes an instance of that interface. (provides compile-time checking for method existance)

    3. throw an event with relevant data instead of pushing something into an array. Let the root listen for that event and handle it however it wants. (decouples parent and child entirely. Same child can go anywhere without loss of functionality).

  6. #6
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Actually, I use system like Son of Bryce described.

  7. #7
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    Other alternative to reduce dependencies could be to pass a reference to an external function and make your class fire that when something happen.I dont like flash event system.
    And, AFAIK the OOPisticaly problem with root.this root.that is that with time things get difficult to understand.Passing a reference to the main timeline dont solve that.Look:
    http://www.kirupa.com/forum/showthre...21#post2167721
    Last edited by Incrue; 05-28-2009 at 06:55 AM.

  8. #8
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    5TonsOfFlax:
    Great suggestions! Throwing my own event from the class is really the RIGHT way to handle this, but I have heard that custom events in AS3 is a pain, and frankly I barely have a grip on the regular events, too much time spent in AS2 I guess. Definately something to look into. What I am doing now is essentially a convoluted home made listener (a loop that waits for an Array to get filled).

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