开发者

referencing variable in document class from another class

开发者 https://www.devze.com 2023-02-16 02:13 出处:网络
I need to increment an integer variable in a function within the document class upon transpiring event in another clas开发者_如何学Pythons. I can\'t seem to figure out how to call the functionand refe

I need to increment an integer variable in a function within the document class upon transpiring event in another clas开发者_如何学Pythons. I can't seem to figure out how to call the function and reference the variable.

as3 newbie, please help!


The proper scope needs to be in place and the proper packaging.

Declare a static variable to handle your access to the Main Document Class

private static var _instance:Main;
public static function get instance():Main { return _instance; }

public function Main() { // constructor
    _instance = this;
}

Declare some getters and setters in the Main Document Class

private var _foo:int = 0;

public function get foo():int{
    return _foo;
}

public function set foo(value:int):void {
    _foo= value;
}

And then in any class you need you can change to something as follows,

public class O {
    public function O() {
        Main.instance.set(Main.instance.get() + 1);
    }
}


simple example, defining a variable 'test' in the document class:

package {   
    public class DocumentClass extends Sprite {
        public static var test:Number = 3;      
        public function DocumentClass() {
            test = 4;
        }            
    }
}

now access the 'test' variable in another class:

package {   
    public class OtherClass extends Sprite {
        public function OtherClass() {          
            DocumentClass.test = 5;
        }
    }
}

does this apply to your code?

0

精彩评论

暂无评论...
验证码 换一张
取 消