-1

What i am basically looking to do is change the name of the outputted instance.

But the problem i have is how to send variables passed thought the function 'find_name' to the 'Event.Complete' function loadNameInfo?

Code Below:

    // Finds a persons name using the ID
    private function find_name(page_Name='search.php',instance_name='name',get1='g1=1',get2='g2=2',get3='g3=3'):void
    {
        var randomParam:String = "?p=" + Math.floor(Math.random() * (10000000));
        var create_URL = (URL + page_Name + randomParam + '&'+ get1 + '&' + get2 + '&' + get3);
        _loader = new URLLoader();
        _request = new URLRequest(create_URL);
        _request.method = URLRequestMethod.POST;
        _loader.addEventListener(Event.COMPLETE, loadNameInfo);
        _loader.load(_request);
    }
    // Loads the name into the correct place
    public function loadNameInfo(e:Event)
    {
        instance_name.text = e.target.data;
    }

Is this kinda thing possible?

Eli

Eli Stone
  • 1,515
  • 4
  • 33
  • 57

6 Answers6

0

This is possible. There are many topics on this out there. Try searching here for "as3 passing variables to event handlers".

One example is the this post

Basically you can extend the event handler to change it to accept another argument that you pass to it.

Community
  • 1
  • 1
M. Laing
  • 1,607
  • 11
  • 25
0

You can write a custom event:

http://www.learningactionscript3.com/2008/11/11/passing-arguments-with-events/

package {   
   import flash.events.Event; 
   public class CustomEvent extends Event {     
      public static const CUSTOM:String = "custom";     
      public var arg:*;     
      public function CustomEvent(type:String, customArg:*=null,
                              bubbles:Boolean=false,
                              cancelable:Boolean=false) {
     
         super(type, bubbles, cancelable);
     
         this.arg = customArg;
     
      }
       
      public override function clone():Event {
         return new CustomEvent(type, arg, bubbles, cancelable);
      }
 
      public override function toString():String {
         return formatToString("CustomEvent", "type", "arg",
                           "bubbles", "cancelable", "eventPhase");
      }

   }

}
Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
0

Another way to go about doing it that I just came across is to use an inline function. I found it at this link.

Basically the idea is the following:

button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){handleClickEvent(e,"Home")});
function handleClickEvent(e:MouseEvent,str:String) {
    trace("Argument :"+str,"- Event target :"+e.target.name);
}

What is interesting about this style is that you pass the event along with whatever else you want by using an inline function as the event handler that then calls the real handler you want. Pretty neat. I've never tried it but it looks like it should work to me.

M. Laing
  • 1,607
  • 11
  • 25
0

You cannot attach custom values to a URLLoader object, but there are several workarounds.

If you have only one loader, you can simply store required values in class fields. If you have many loaders that load content simultaneously, you may create the Dictionary object to associate URLLoader instances with your values. Something like this:

private var _loaderValues:Dictionary = new Dictionary();

private function find_name(page_Name='search.php',instance_name='name',get1='g1=1',get2='g2=2',get3='g3=3'):void
{
    var randomParam:String = "?p=" + Math.floor(Math.random() * (10000000));
    var create_URL = (URL + page_Name + randomParam + '&'+ get1 + '&' + get2 + '&' + get3);
    var loader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest(create_URL);
    request.method = URLRequestMethod.POST;
    loader.addEventListener(Event.COMPLETE, loadNameInfo);
    loader.load(_request);
    _loaderValues[_loader] = {"pageName": page_Name, "instanceName": instance_name};
}

public function loadNameInfo(e:Event)
{
    var loader:URLLoader = e.target as URLLoader;
    instance_name.text = loader.data;
    var values:Object = _loaderValues[loader];
    trace(values["pageName"], values["instanceName"]);
    loader.removeEventListener(Event.COMPLETE, loadNameInfo);
    _loaderValues[loader] = null;
}
skozin
  • 3,789
  • 2
  • 21
  • 24
0

Remove loadNameInfo and use anonymous function as event listener:

var that:* = this;   
_loader.addEventListener(Event.COMPLETE, function(e:Event):void{
     //you can access instance,which has 'instance_name' name, in 2 ways
     // 1. that[instance_name]
     // 2. that.getChildByName(instance_name)

     that[instance_name].text = e.target.data;
     //Removing anonymous listener from '_loader'
     e.target.removeEventListener(e.type, arguments.callee) ;
});
Engineer
  • 47,849
  • 12
  • 88
  • 91
0

Yes, it's pretty possible and surprisingly simple. Here's your code with 3+ lines that do that:

// Finds a persons name using the ID
private function find_name(page_Name = "search.php", instance_name = "name", get1 = "g1=1", get2 = "g2=2", get3 = "g3=3"):void {
  var randomParam:String = "?p=" + Math.floor(Math.random() * (10000000));
  var create_URL = (URL + page_Name + randomParam + "&" + get1 + "&" + get2 + "&" + get3);
  _loader = new URLLoader();
  _request = new URLRequest(create_URL);
  _request.method = URLRequestMethod.POST;
  var functionLoadNameInfo:Function = loadNameInfo(page_Name, instance_name, get1, get2, get3);
  _loader.addEventListener(Event.COMPLETE, functionLoadNameInfo);
  // To remove you do: _loader.removeEventListener(Event.COMPLETE, functionLoadNameInfo);
  _loader.load(_request);
}

// Loads the name into the correct place
public function loadNameInfo(pageName:String, instance_name:String, get1:String, get2:String, get3:String):Function {
  return function(e:Event):void {
    instance.text = e.target.data;
    instance.name = instance_name;
  }
}

This solution is based on this answer.

Community
  • 1
  • 1