2012-04-21 53 views
0

什么,我基本上希望做的是改变输出实例的名称。AS3:函数和变量

但我的问题是如何发送传递思想的功能“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; 
    } 

这是有点事可能吗?

回答

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

0

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

一个例子是this post

基本上可以延长事件处理程序来改变它接受你传递给它的另一个理由。

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

     } 

} 
+0

要使用自定义事件,你需要编写自定义的URLLoader类:) – skozin 2012-04-21 01:57:14

+0

扩展URLLoader不是困难,有一个thousund例子在那里像http://www.mikechambers.com/blog/2008/09/ 12/urlloader-subclass-with-automatic-refresh-support /。我不说这是最好的解决方案,但工作和优雅。 – rcdmk 2012-04-21 02:01:41

+0

无论如何,该链接有4种解决方案供您选择。 – rcdmk 2012-04-21 02:03:10

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

是什么样的这种风格有趣的是,通过使用一个内联函数作为事件处理程序,然后调用真正的与任何你想要的人一起传递事件你想要的处理器。很简约。我从来没有尝试过,但它看起来应该对我有用。

+0

这是我发布的链接中的一个例子,该链接为这种情况提供了4种解决方案。 – rcdmk 2012-04-21 02:02:47

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