问题
I am trying to create a simple AS3 code in Flash Professional CS6 which references a variable.
Example:
var1:int = 1;
varref = "var1"; (this is the "reference" variable, but ofcourse this is not how it's done in as3)
if (var1 == 1)
{
varref = 50
}
If this is run, it would try to make the string from the variable varref which is currently "var1" into an int of "1'. I want it to reference the variable, not be a variable of it's own.
A simple example of how to do this would be great. (From what I know, an object may be needed, so a simple object example of this situation would be great.)
回答1:
I'm using this class for creating references : https://github.com/turbosqel/as3SupportLib/blob/master/as3SupportLib/src/turbosqel/data/LVar.as
Its very simple to use , like :
public var item:String = "some str";
{...}
var ref:LVar = new LVar (this,"item");
trace(ref.value);// return : "some str"
trace("my ref is " + ref ); // return : my ref is some str
// You can always change this on runtime :
item = "new value";
trace(ref.value); // return : new value
Same thing You can do with any other data type .
回答2:
In AS3 you can reference basic types (only classes and objects).
You could achieve what you want by doing something similar to:
var var1:Object = { value:1 };
var varRef:Object = var1;
if(var1.value == 1) {
varRef.value = 50;
}
trace(varRef.value); // outputs 50;
trace(var1.value); // outputs 50;
来源:https://stackoverflow.com/questions/11373291/reference-variable-as3