我有一个带有AS2代码的Flash动画,它在一个单独的类文件中调用sendAndLoad,并且在线指南后我使用了一个Delegate来处理onLoad部分,如下所示:
ActionScript Code in class:
function myFunction():String{
...
var send_lv:LoadVars = new LoadVars();
var saveresult_lv:LoadVars = new LoadVars();
send_lv.dataString = rc4_hash; send_lv.sendAndLoad("<my url>",saveresult_lv,"POST");
saveresult_lv.onLoad = Delegate.create(this, onLoad);
}
function onLoad (success:Boolean) {
if (success) {
}else{
}
}
我有两个问题:
因为我从.fla文件中调用myFunction(),如何将onLoad()的结果作为myFunction()的返回值发回?
如何在onLoad中引用myFunction内创建的变量?
在我的fla文件中,我有这样的代码:
myVar的:字符串= myFunction的();
如果onLoad需要任何时间,那么我认为myFunction()没有返回任何内容,myVar最终未定义。如何让fla等待类文件中的onLoad完成,然后用结果填充myVar?
非常感谢任何提示
答案 0 :(得分:0)
您的课程可以重组以回答您的问题。
分离构造函数,加载函数和结果操作。当加载就绪时,类可以调度一个事件。您的FLA可以收听此活动。
// Class code should something like this: (my AS2 is a little rusty)
class Sender {
private var classSavedData:String;
// these three vars are needed to use EventDispatcher.
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
// Make your class ready to send events
public function Sender() {
EventDispatcher.initialize(this);
}
public function loadData(){
classSavedData = "Something the FLA can pick up after loading";
var send_lv:LoadVars = new LoadVars();
var saveresult_lv:LoadVars = new LoadVars();
send_lv.dataString = rc4_hash;
end_lv.sendAndLoad("<my url>",saveresult_lv,"POST");
saveresult_lv.onLoad = Delegate.create(this, onLoad);
}
private function onLoad():Void {
dispatchEvent({target:this, type:"eventFired", loadAnswer: saveresult_lv , extraData:classSavedData});
}
}
... FLA中的代码可能如下所示:
var myClassInstance:Sender = new Sender()
myClassInstance.addEventListener ("eventFired", Delegate.create (this, onDataLoaded))
myClassInstance.loadData()
function onDataLoaded(evt:Object){
trace ("loaded loadvars "+evt.loadAnswer)
}