我尝试制作一个将amfphp与as3动画连接起来的应用程序。我从amfphp收到一个数组,我按照一些变量排序,如果'声明。之后,我有2个阵列,里面有阵列:
var Array1:Array = [["http://url.to.swf", "http://url.to.link"], ["",""]...];
var Array2:Array = [["http://url.to.swf", "http://url.to.link"], ... ];
之后,我运行一个读取该数组并将每个swf加载到舞台的函数:
ladujBajkiGorne(Array1);
function ladujBajkiGorne(polkaGorna:Array):void {
for(var i=0; i<polkaGorna.length; i++) {
Aktual = polkaGorna[i];
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(String(Aktual[0]));
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myfunction = function(e:Event):void { onLoadGorna( e, String(Aktual[1]) ); } );
mLoader.load(mRequest);
}
}
然后
function onLoadGorna(e, arg1):void {
userSwf = e.currentTarget.content;
userSwf.buttonMode = true;
userSwf.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) { afterClick(e, arg1 ); });
booksMC.addChild(userSwf);
var childName:String = booksMC.getChildAt(childs).name;
booksMC.getChildByName(childName).x = posX;
booksMC.getChildByName(childName).y = 0;
posX += 218;
childs++;
}
并在最后
function afterClick(e, arg3) {
var url:URLRequest = new URLRequest(arg3);
navigateToURL(url, "_self");
}
问题是所有已加载的swf在链接中都有相同的url。它在addEventListener语句中的ladujBajkiGorne函数中没有变化。我尝试了许多选项来改变它,没有任何作用。有机会帮助我吗? 非常感谢提前
答案 0 :(得分:2)
问题在于加载器完整处理程序的异步执行。将适当的参数传递给loader并在处理程序上读取它会很好。我提出了一个简单的解决方案,纠正你的代码:
function ladujBajkiGorne(polkaGorna:Array):void {
for(var i=0; i<polkaGorna.length; i++) {
Aktual = polkaGorna[i];
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(Aktual[0]+"?link="+Aktual[1]);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myfunction = function(e:Event):void { onLoadGorna( e ); } );
mLoader.load(mRequest);
}
}
function onLoadGorna(e):void {
var link:String = e.currentTarget.parameters.link;
userSwf = e.currentTarget.content;
userSwf.buttonMode = true;
userSwf.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) { afterClick(e, link ); });
booksMC.addChild(userSwf);
var childName:String = booksMC.getChildAt(childs).name;
booksMC.getChildByName(childName).x = posX;
booksMC.getChildByName(childName).y = 0;
posX += 218;
childs++;
}
之后,每个加载器都知道它的链接。
答案 1 :(得分:1)
问题出在complete事件的事件处理程序中。调用一个函数,返回一个新函数作为事件处理程序。
首先,创建一个这样的函数:
function getCompleteHandler(url:String):Function {
return function (e:Event) {
onLoadGorna(e, url);
}
}
然后替换它:
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myfunction = function(e:Event):void { onLoadGorna( e, String(Aktual[1]) ); } );
由此:
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, getCompleteHandler(Aktual[1]) );
现在应该可以了!!