PDA

View Full Version : accessing stage variables from class



DanglingChap
March 26th, 2008, 08:05 AM
hi ..

I have written some action script (well most of the script) on main time line without using any CLASS - (bad practice) .. there are many variables that i have created there and the stage objects .. I am using those objects on main time line as ..



this.mVideo.someProperty ...


now i have written a class say .. "Test.as" which i have placed in the same directory. now while being in that class method, how can i access variables define on the timeline actionscript + on the stage ..

how can i access that "mVideo" object from that class ??

icio
March 26th, 2008, 12:09 PM
Try either `root` or `stage` followed by `.mVideo`. If that doesn't tell the Test object explicitly what the current object is (pseudo code):

Timeline:

var test:Test = new Test(this);

Test Class:

function Test(caller:*) {
trace(caller.mVideo);
}

Hope that helps :thumb:

dail
March 26th, 2008, 04:05 PM
You can also either pass a reference to the mainTimeline to your class, so that any subsequent calls to vars, methods etc on the mainTimeline will be in the correct scope and fire correctly.

Something like var myClassInstance:myClass = new myClass(referenceToMainTimeline)

in class

var referenceToMainTimeline = this.referenceToMainTimeline
referenceToMainTimeline.method()

Or, use casting, and cast calls to your main timeLine like so;

MovieClip(this.root).method()

Or, if your external class extends any kind of display Object and has been added to the display list, simply use parent etc.

DanglingChap
March 27th, 2008, 03:20 AM
I came up the solution before i read this post. but thats exactly what i did. Thanks a lot that you people helped me.

anyway, i am posting my code snippet here so that it may help others ..:)


ActionScript Code:

package
{
import flash.display.MovieClip;
public class testclass extends MovieClip
{
private var _root : Object;
public function testclass (myRoot : Object)
{
_root = myRoot;
}

public function changeString ():void
{
_root.testThis();
_root.testVar = "it has been changed - thanks God";
}

}
}



and that's the code @ root time line ..

ActionScript Code:

var testVar : String = "Not Changed";
trace (testVar);
var obj : testclass = new testclass(this);
obj.changeString();
trace (testVar);

DanglingChap
March 27th, 2008, 03:23 AM
btw, testThis was a method that i created on mainTimeLine.

icio
March 27th, 2008, 07:41 AM
Cool -- Glad you got it. If you wanted to be more precise, however, you could set the `_root` variable type to MovieClip, DisplayObject...

EthanG800cc
March 28th, 2008, 03:02 PM
DanglingChap, THANK YOU!

I've been trying to get this question answered for a few weeks now!!! Nothing I did worked. I took dail's method and I couldn't get it to work (I'm sure due to my own limitations in understanding the code, and not dail's code itself).

Thanks to all all who help out on these forums!