Tutorial :Flash, ActionScript 3: define a variable as a variable from its creator without constantly using creator.var



Question:

I am trying to split up my code in classes. but there is a issue what really bothers me. when i create a class for functions i am giving its own stage. like this

dragf:Dragfunctions = new Dragfunctions(this)  

and in the class i use this

var stage:Object;        public function Dragfunctions(stage:Object)       {              this.stage = stage;      }  

as you can see i can now call a variable of the stage using stage.var1 = "hi" but when i need to ajust that varable many times it gets a quite messy...

there is a way to tell that when i call var1 he knows i mean stage.var1 without need to call stage. its:

var var1 = stage.var1   

and then using

stage.var1 = var1   

but that is quite unhandy too is there a better way?


Solution:1

Yes, use getters and setters (which provide field-like semantics, but allow you to execute custom logic when assignment is used):

function set var1(val:SomeType):void  {      stage.var1=val;  }  function get var1():SomeType  {      return stage.var1;  }  function doStuff():void  {      var1=new SomeType();  //this results in call to "set" method      var st:SomeType=var1; //this results in call to "get" method  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »