Reference variable in a document class from another class

I need to increment an integer variable in a function inside a document class when an event occurs in another class. I cannot figure out how to call a function and refer to a variable.

as3 newbie please help!

+4
source share
2 answers

There must be a proper area and 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 something as follows:

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

simple example defining the variable 'test' in a 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?

+1
source

Source: https://habr.com/ru/post/1343345/


All Articles