如何从flex中的服务结果处理程序返回值?

时间:2012-03-16 13:39:01

标签: actionscript-3 flex flex4

我只会调用“clickfun()”方法,成功服务返回值后需要urlPath值。但它会抛出堆栈溢出错误。请帮忙。

private function clickfun(eve:Event):String{
            languagecode = "assets/myFont_en.swf";
            Defaultlanguagecode = "assets/myFont_default.swf";
            var request:URLRequest = new URLRequest(languagecode);
            var xmlURLLoader:URLLoader = new URLLoader(request);
            xmlURLLoader.addEventListener(Event.COMPLETE,loadcompletefun);
            xmlURLLoader.addEventListener(IOErrorEvent.IO_ERROR,ioerrorFun);
            xmlURLLoader.load(request); 
            return getpath();

        }

        private function getpath():String{
            if(loadcomplete == true){
                Alert.show(urlpath);
                return urlpath;
            }else
                return getpath(); 


        }
        private function loadcompletefun(e:Event):void{
            loadcomplete = true;
            urlpath = languagecode;
        }
        private function ioerrorFun(e:IOErrorEvent):void{
            loadcomplete = true;
            urlpath = Defaultlanguagecode;
        }



<mx:Panel title="Embedded Fonts Using ActionScript" width="800" height="500">
            <mx:Button label="Click" id="btn" click="clickfun(event)"/>
</mx:Panel>

2 个答案:

答案 0 :(得分:1)

您的远程交互代码显而易见的是,加载器异步加载数据。这意味着当加载器在(虚拟地)不同的线程上加载数据时,程序的执行继续。

问题在于您在开始加载后立即致电getpath()。这使得loadcomplete为false,并且getpath函数不断递归并且堆栈溢出。

你应该是这样的:

让您的班级派遣一个活动。假设您只发送一个Event.COMPLETE事件。

将IDE告诉IDE:

在您的类声明附近,添加元数据并使您的类扩展EventDispatcher

//Imports and package declaration

[Event(name="complete", type="flash.events.Event")]
public class YourClassName extends EventDispatcher {

    //Remaining part of class here

然后,在loadcompletefun中添加此

dispatchEvent(new Event(Event.COMPLETE));

并且,在您致电clickfun的地方,请执行以下操作:

o=new YourClassName();
o.addEventListener(Event.COMPLETE, gp);

并将gp声明为

private function gp(e:Event):void {
    trace(getpath());
    //You now have the ability to call getpath()
}

答案 1 :(得分:0)

基于对代码的审核,我猜你收到了一个错误,因为没有定义loadcomplete和urlpath变量。所以,请确保在ActionScript块中定义它:

public var loadcomplete : Boolean
public var urlPath : String

还要确保使用相同的方法将languagecode和Defaultlanguagecode定义为变量。

这些遗漏中的任何一个都可能导致编译时或运行时错误。