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.
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.
- The right of the People to create Flash movies shall not be infringed. -
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
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. -
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.
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:
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 05:17 PM.
- The right of the People to create Flash movies shall not be infringed. -