AS3 - 我无法从函数返回一个字符串值

时间:2011-12-19 06:40:38

标签: actionscript-3 return tinyurl

AS3代码,从示例中,我希望字符串'location'中的值可用于主程序的其他部分。它在完成的处理程序中返回正常,但是如何使它可用于第一部分?

package  {

import flash.display.MovieClip;
import flash.display.MovieClip;
import flash.events.*
import flash.net.*;
import flash.net.URLVariables;  

public class turl extends MovieClip {

public var location:String = new String();

public function turl() {
    // constructor code 
var variables:URLVariables = new URLVariables();
variables.url = String("xxxxxxxxx");
sendAndLoad("xxxxxxxx", variables)
// THIS TRACE WILL NOT DISPLAY THE LOCATION _ A TINY URL
trace("TinyURL: " + location);
    }

function sendAndLoad(url:String, _vars:URLVariables ):void {

var request:URLRequest = new URLRequest(url);
var _urlloader:URLLoader = new URLLoader();
_urlloader.dataFormat = URLLoaderDataFormat.TEXT;
request.data = _vars;
request.method = URLRequestMethod.POST;
_urlloader.addEventListener(Event.COMPLETE, handleComplete);
_urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_urlloader.load(request);


}
function handleComplete(event:Event):void {
    var loader:URLLoader = URLLoader(event.target);
    location = loader.data;
    trace("TinyURL: " +  location);

}
function onIOError(event:IOErrorEvent):void {
    trace("Error loading URL.");
}


    }

}

3 个答案:

答案 0 :(得分:1)

package 
{
  import flash.display.MovieClip;
  import flash.display.MovieClip;
  import flash.events.*
  import flash.net.*;
  import flash.net.URLVariables;  

  public class turl extends MovieClip 
  {
    public static var Location:String;

    public function turl() {
     // constructor code 
     var variables:URLVariables = new URLVariables();
     variables.url = String("http://www.designscripting.com");
     sendAndLoad("http://tinyurl.com/api-create.php", variables)
     // THIS TRACE WILL NOT DISPLAY THE LOCATION _ A TINY URL
     trace("TinyURL: " + Location);
  }

  function sendAndLoad(url:String, _vars:URLVariables ):void
  {
     var request:URLRequest = new URLRequest(url);
     var _urlloader:URLLoader = new URLLoader();
     _urlloader.dataFormat = URLLoaderDataFormat.TEXT;
      request.data = _vars;
      request.method = URLRequestMethod.POST;
     _urlloader.addEventListener(Event.COMPLETE, handleComplete);
     _urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
     _urlloader.load(request);
   }

   function handleComplete(event:Event):void {
   var loader:URLLoader = URLLoader(event.target);
   Location= loader.data;
   trace("TinyURLss: " +  Location);
   }

   function onIOError(event:IOErrorEvent):void {
   trace("Error loading URL.");
   }
}
}

静态变量位置保存您的String值,您可以获取此String值    课堂上和课外的任何地方。

答案 1 :(得分:0)

构造函数中的trace语句不起作用,因为该跟踪在发出数据请求后立即发生,在下载数据并设置了 location 之前。构造函数用于设置对象的初始条件。使构造函数立即可用的数据请求结果的唯一方法是直接传递它,但我认为这会破坏类的重点。

public function TURL(value:String)
{
    location = value;

    // Now this will work like you think.
    trace("TinyURL: " + location);
}

我猜你还有其他对象依赖于这个 TURL 类具有合适的位置。如果是这种情况,请让 TURL 类在设置位置变量时调度一个事件,表明它已准备好使用。

function handleComplete(event:Event):void
{
    var loader:URLLoader = URLLoader(event.target);        
    location = loader.data;

    dispatchEvent(new Event(Event.COMPLETE));
}

答案 2 :(得分:0)

经过测试和工作!

var turl:Turl = new Turl(“http://www.designscripting.com”);

收到URL后,您可以通过trace(turl.loc);

访问它
   package  {

    import flash.display.MovieClip;
    import flash.events.*
    import flash.net.*;
    import flash.net.URLVariables;  

    public class Turl extends MovieClip {

    public var loc:String;

    public function Turl(urlToEncode:String):void {

    var variables:URLVariables = new URLVariables();
    variables.url = String(urlToEncode);
    sendAndLoad("http://tinyurl.com/api-create.php", variables);

    }


//2. send the request for the URL

    private function sendAndLoad(url:String, _vars:URLVariables ):void {

    var request:URLRequest = new URLRequest(url);
    request.data = _vars;
    request.method = URLRequestMethod.POST;

    var _urlloader:URLLoader = new URLLoader(request);
    _urlloader.dataFormat = URLLoaderDataFormat.TEXT;

    _urlloader.addEventListener(Event.COMPLETE, handleComplete, false, 0, true);
    _urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
    _urlloader.load(request);

    }


//3. handle the response. Only accessible once the response has been received.

    private function handleComplete(event:Event):void {

    event.target.removeEventListener(Event.COMPLETE, handleComplete);
    event.target.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);

    loc = event.target.data;
    trace("loc = "+event.target.data);

    }

    function onIOError(event:IOErrorEvent):void {
    event.target.removeEventListener(Event.COMPLETE, handleComplete);
    event.target.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
    trace("Error loading URL.");
    }
}
}