reference variable AS3

女生的网名这么多〃 提交于 2019-12-23 22:13:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!