AS3:函数和变量

时间:2012-04-21 00:56:40

标签: actionscript-3 flash

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

但我遇到的问题是如何发送传递的变量,将函数'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;
    }

这种事情有可能吗?

6 个答案:

答案 0 :(得分:1)

是的,这很可能而且非常简单。这是你的代码,有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;
  }
}

此解决方案基于this answer

答案 1 :(得分:0)

这是可能的。关于这一点有很多主题。尝试在这里搜索“as3将变量传递给事件处理程序”。

一个例子是this post

基本上你可以扩展事件处理程序来改变它以接受你传递给它的另一个参数。

答案 2 :(得分: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");
      }

   }

}

答案 3 :(得分:0)

我刚刚遇到的另一种方法是使用内联函数。我在this link找到了它。

基本上这个想法如下:

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

这种风格的有趣之处在于,通过使用内联函数作为事件处理程序然后调用所需的实际处理程序,将事件与您想要的任何其他内容一起传递。很简约。我从来没有尝试过,但它看起来应该对我有用。

答案 4 :(得分: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;
}

答案 5 :(得分: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) ;
});