Flash, ActionScript 3: define a variable as a variable from its creator without constantly using creator.var
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?
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
}
精彩评论