A Flash Developer Resource Site

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140

    can't get the instance extending the root

    is there a problem with accessing the instance on the stage. Cause i can't seem to.

    in the fla, a movieclip exists on the stage (no document class), this movieclip belongs to the class Main, inside this movieclip are three other movieclips, correctly referenced in Main. Now i have another class which is trying to access one of these movieclips, and i want to access the instance of Main to get them, but it wont work.

    in Main: trace(parent == root) //true

    trace(root.numChildren) //compiler error, undefined property

    trace(stage.numChildren) // 1

    trace(stage.getChildAt(0)) // root1

    trace(stage["root1"]) //compiler error, undefined property

    can someone please explain whats going on here.

    stage says it has a child, its name: root1

    root1 is the parent of the movieclip placed on the stage, class: Main

    root1 says it has no children

    how do i solve this?
    lather yourself up with soap - soap arcade

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,426
    I have attached a little example, which may reflect your situation. You seem to call Main, when the object from which you are calling is not yet on stage. Then the stage itself is not recognized. Usually that happnes when you for example call the another object like Main from within the constructor . If you have this situation and you call Main from Child2 it won't work.

    var m:Main = new Main ();
    m.name = "m";
    m.x = 100;
    m.y = 100;
    addChild(m);
    var c2:Child2 = new Child2();
    c2.x = 200;
    c2.y = 200;
    addChild(c2);


    If you now add a function in which you call Main

    c2.getChild();

    You will be successful, since now the stage is established.
    Attached Files
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    ok. But its returning null. Why?

    addChild isn't a problem because when the object exists on stage its added to the root's or stage's display list. Then the class is initiated. Otherwise i wouldn't be able to access the stage property's which i am doing.

    here's my test. I'm stil stumped, why can't the class access itself from an alternate path than "this", i feel like i'm going insane here
    lather yourself up with soap - soap arcade

  4. #4
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,426
    I didn't know your movie. I mainly write scripts without placing anything on stage.

    In your case it is very simple to reference objects within main. You reference them using their name:

    ob1.x = 200;

    trace(root.numChildren); //error gives an error, since root is a DisplayObject and this property is not a property of the DisplayObject. In case of stage.numChildren there is no error.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    but the parent of the main instance is root.

    so how would an outside class access the main instance if the main instances parent is not a displayObjectContainer.

    i know the main instance can access any property's within itself by directly referencing them, such as ob1.x = 200

    in AS2, if i had declared an instance on the _root like: var main:Main = new Main(), i could get that variable by simply saying _root.main.

    in AS3 it don't work, since the name of the instance is instance1, one would think this would work (accessing from main)

    in Main.as:

    var me1 = this // [object Main]
    var me2 = root.instance1 //error
    var me3 = stage.instance1 //error

    so now i'm not even trying to access a child but simply a variable and it still doesn't work. It has to exist and have a path, i just don't know how to get that path.
    lather yourself up with soap - soap arcade

  6. #6
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,426
    The MovieClip is located on the timeline created by the flash player. You can call it using this syntax by casting the Displayobject "this.root" to a MovieClip:

    trace("this.root: "+MovieClip(this.root).getChildByName("instance1") );

    However, when you want to call other objects on the timeline you cannot use call them from a reference in the constructor, but you need to use a new function. A second object named ob4 for example when placed on the timeline would result in null if called from the constructor of the Main class. Only when you make a separate call using a new function you would catch the object. That is why you should leave the constructor empty except for methods and properties relating to the object itself.

    If you have a function, which should manipulate other objects you add as a parameter the timeline. Let's say you have a class Helper, then in the movie you would write:

    var a:Helper = new Helper();
    a.getChild(this);

    "this" refers to the timeline.

    public function Helper():void {
    }
    public function getChild(myStage):void
    {
    trace(myStage);
    trace(myStage.numChildren);
    trace(myStage.getChildAt(0).name);
    trace(myStage.getChildByName("instance1"));
    trace(myStage.getChildByName("ob4"));
    }

    You can now access all the MovieClips on the timeline. The stage is not the timeline. Also it is not good to add objects directly on the stage.When this movie is loaded into another movie, the stage will be the parent movie. To maintain the original movie use a timeline.
    Last edited by cancerinform; 02-10-2007 at 04:17 PM.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    ok, thanks for the help, much appreciated, i think i understand, i guess using AS2 for so long makes it hard to understand the new time line system.
    lather yourself up with soap - soap arcade

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