View Full Version : Access Root Variables
chris12295
August 7th, 2007, 02:55 AM
I want to do something like this:
class blah {
public function handleClickEvent ( event:MouseEvent ) : void {
this.setIndex ( _root.index );
}
}
I know _root.index doesn't work in ActionScript 3.0, so how do I access root variables from within a class or how do I accomplish what I'm trying to do?
glidealong
August 7th, 2007, 03:13 AM
I know _root.index doesn't work in ActionScript 3.0, so how do I access root variables from within a class or how do I accomplish what I'm trying to do?
use root instead of _root and make sure you have added the instance of the class to the display list before using root.
chris12295
August 7th, 2007, 03:25 AM
i tried this:
private function mouseUpHandler(event:MouseEvent) : void {
trace(this.root.myIndex);
}
and got this:
1119: Access of possibly undefined property myIndex through a reference with static type flash.display:DisplayObject.
fluid_tw0
August 7th, 2007, 05:06 AM
MovieClip(this.root)
glidealong
August 7th, 2007, 07:00 AM
i tried this:
private function mouseUpHandler(event:MouseEvent) : void {
trace(this.root.myIndex);
}
no need for 'this.root.myIndex' Try 'root.myIndex'
chris12295
August 7th, 2007, 01:50 PM
private function mouseUpHandler(event:MouseEvent) : void {
trace(MovieClip(this.root.myIndex));
}
1180: Call to a possibly undefined method MovieClip.
1119: Access of possibly undefined property myIndex through a reference with static type flash.display:DisplayObject.
private function mouseUpHandler(event:MouseEvent) : void {
trace(root.myIndex);
}
1119: Access of possibly undefined property myIndex through a reference with static type flash.display:DisplayObject.
myIndex is declared on Frame 1 of the main timeline as:
myIndex:Number = 1;
just for testing. Later it will be dynamic. Also, I import the class which is in a separate file if that changes anything.
Aquilonian
August 7th, 2007, 04:00 PM
MovieClip(root).myIndex
chris12295
August 7th, 2007, 05:09 PM
Thanks. That last one worked but I didn't have import flash.display.MovieClip. So it worked after I imported.
Pier25
August 9th, 2007, 04:32 AM
MovieClip(root).myIndex
I super new into as3, and I'm a designer... so my question is... what does that piece of code mean?
Ok I know myIndex is the name of the var. And I understand also that root is the lowest level of something.... but what does MovieClip(root) means? The lowest level in the movieclip class?... in the display object?...
As you can see I'm super lost...
Thanks ;)
mathew.er
August 9th, 2007, 05:20 AM
It casts to MovieClip data type.
E.g. if you have a document class named MyClass that has has some public property named 'property' and you don't want to disable strict mode or use stage['property'], you would cast to the type of your document class like MyClass ( stage ).property to avoid compiler errors.
Pier25
August 9th, 2007, 08:45 AM
Hi thanks mathew but you answer makes news questions... I know this must be super obvious to you OOP programmers but...
what is strict mode?
why do you use stage?
Aquilonian
August 9th, 2007, 11:49 AM
There is a tip of the day about the strict mode, now i dont remember each one
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.