-1

我基本上想做的是更改输出实例的名称。

但我遇到的问题是如何将通过函数'find_name'传递的变量发送到'Event.Complete'函数loadNameInfo?

下面的代码:

    // 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;
    }

这种事情可能吗?

伊莱

4

6 回答 6

0

这个有可能。有很多关于这个的话题。尝试在此处搜索“as3 将变量传递给事件处理程序”。

一个例子是这篇文章

基本上,您可以扩展事件处理程序以将其更改为接受您传递给它的另一个参数。

于 2012-04-21T01:49:13.483 回答
0

您可以编写自定义事件:

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

   }

}
于 2012-04-21T01:52:17.767 回答
0

我刚刚遇到的另一种方法是使用内联函数。我在这个链接上找到了它。

基本上思路如下:

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

这种风格的有趣之处在于,您可以通过使用内联函数作为事件处理程序来传递事件以及您想要的任何其他内容,然后调用您想要的真实处理程序。漂亮整齐。我从未尝试过,但它看起来应该对我有用。

于 2012-04-21T01:53:46.413 回答
0

您不能将自定义值附加到 URLLoader 对象,但有几种解决方法。

如果您只有一个加载器,您可以简单地将所需的值存储在类字段中。如果您有许多同时加载内容的加载器,您可以创建 Dictionary 对象以将 URLLoader 实例与您的值相关联。像这样的东西:

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;
}
于 2012-04-21T01:56:25.387 回答
0

移除loadNameInfo并使用匿名函数作为事件监听器:

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) ;
});
于 2012-04-21T09:00:17.003 回答
0

是的,这很有可能而且非常简单。这是您的代码,其中包含 3 行以上的代码:

// 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;
  }
}

此解决方案基于此答案

于 2012-11-24T16:08:13.830 回答