Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3-我能';t获取从函数返回的字符串值_Actionscript 3_Return_Tinyurl - Fatal编程技术网

Actionscript 3 AS3-我能';t获取从函数返回的字符串值

Actionscript 3 AS3-我能';t获取从函数返回的字符串值,actionscript-3,return,tinyurl,Actionscript 3,Return,Tinyurl,作为3代码,从一个示例中,我希望字符串“location”中的值可用于主程序的其他部分。它在已完成的处理程序中返回良好,但如何使其可用于第一部分 package { import flash.display.MovieClip; import flash.display.MovieClip; import flash.events.* import flash.net.*; import flash.net.URLVariables; public class turl extends

作为3代码,从一个示例中,我希望字符串“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.");
}


    }

}

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

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

    // Now this will work like you think.
    trace("TinyURL: " + location);
}
我猜还有其他对象依赖于这个TURL类,它们有一个合适的位置。如果是这种情况,请让TURL类在设置location变量时分派一个事件,指示它已准备好使用

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

    dispatchEvent(new Event(Event.COMPLETE));
}
静态变量Location保存字符串值,您可以获取该字符串值 课堂内外的任何地方。

经过测试,工作正常

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

    // Now this will work like you think.
    trace("TinyURL: " + location);
}
变量turl:turl=新turl(“http://www.designscripting.com");

一旦收到URL,您就可以通过跟踪(turl.loc)访问它


您的完整处理程序执行正确??我看不出引入静态位置如何解决问题。更糟糕的是,现在有两个变量用于相同的目的。Stefan,我想这是用来处理变量的。这就是OP想要得到的,尽管这是一种非常可怕的做法。
   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.");
    }
}
}