Question

AS3 code, from a sample, I want to have the value in the string 'location' available to other parts of the main program. It returns fine in the completed handler, but how do I make it available to the first part?

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.");
}


    }

}
Was it helpful?

Solution

The trace statement in the constructor doesn't work because that trace happens immediately after the data request is made, before the data has been downloaded and location has been set. The constructer is meant for setting the initial conditions of an object. The only way to make the result of the data request immediately available to the constructor is to pass it in directly, but I think this would defeat the point of the class.

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

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

I'm guessing you have other objects relying on this TURL class having a proper location. If that's the case, have the TURL class dispatch an event when it sets the location variable, indicating that it is ready to be used.

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

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

OTHER TIPS

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.");
   }
}
}

static variable Location holds your String value and you can get this String value anywhere in class and outside the class.

Tested and working!

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

Once the URL has been received you can access it by 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.");
    }
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top