问题很简单: 我需要将实际变量传递给函数。
private var test:String = "KKK";
trace (" Before --->>> " + test);
testFunction(test);
trace (" Next --->>> " + test);
private function testFunction(d:String):void{
d = "MMM";
}
结果:
Before --->>> KKK
Next --->>> KKK
结果是正确的,但是,我想要的是,将实际的test
变量发送到我的函数并更改它。所以我希望得到这样的输出:
Before --->>> KKK
Next --->>> MMM
任何解决方案?
感谢您的回答,但如果我有这样的代码,我需要将实际变量传递给我的函数:
if ( lastPos == -1 ){// if this is first item
flagLEFT = "mid";
tempImageLEFT = new Bitmap(Bitmap(dataBANK[0]["lineimage" + 10]).bitmapData);
}else if (nextPos == -1){// if this is the last position
flagRIGHT = "mid";
tempImageRGHT = new Bitmap(Bitmap(dataBANK[0]["lineimage" + 13]).bitmapData);
}
如您所见,更改位于flagLEFT
和tempImageRGHT
。此外,我对数字(10和13)进行了更改,可以正常方式处理。我需要这样的东西:
private function itemFirstLast(flag:String, bmp:Bitmap, pos:int):void{
flag = "mid";
bmp = new Bitmap(Bitmap(dataBANK[0]["lineimage" + pos]).bitmapData);
}
任何解决方案?
答案 0 :(得分:2)
一种方法是返回新字符串并将其分配给test:
private var test:String = "KKK";
trace (" Before --->>> " + test);
test = testFunction(test);
trace (" Next --->>> " + test);
private function testFunction(d:String):String{
d = "MMM";
return d;
}
这仍然没有传递实际的字符串对象,但测试字符串会改变。字符串在AS3中按值传递,如果你不想实际传递它,你可以将它包装在一个对象中:
var object:Object {
"test":"KKK"
};
trace (" Before --->>> " + object["test"]);
testFunction(object);
trace (" Next --->>> " + object["test"]);
private function testFunction(o:Object):void{
o["test"] = "MMM";
}
答案 1 :(得分:2)
您需要将其包装在类实例中:
class StringValue{
function StringValue( value : String ) : void{
this.value = value;
}
public var value : String;
public function toString() : String{
return value;
}
}
private var test:StringValue = new StringValue( "KKK" );
trace (" Before --->>> " + test);//traces 'KKK'
testFunction(test);
trace (" Next --->>> " + test);//traces 'MMM'
private function testFunction(d:StringValue):void{
d.value = "MMM";
}
答案 2 :(得分:0)