Actionscript 3 AS3:函数和变量
我基本上希望做的是更改输出实例的名称 但我的问题是如何将函数“find_name”传递的变量发送到“Event.Complete”函数loadNameInfo 代码如下:Actionscript 3 AS3:函数和变量,actionscript-3,flash,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 {
// 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;
}
这可能吗
Eli这是可能的。关于这一点有很多话题。尝试在此处搜索“as3将变量传递给事件处理程序” 一个例子是
基本上,您可以扩展事件处理程序,将其更改为接受传递给它的另一个参数。您可以编写自定义事件:
我刚才遇到的另一种方法是使用内联函数。我在家里找到的 基本思路如下:
button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){handleClickEvent(e,"Home")});
function handleClickEvent(e:MouseEvent,str:String) {
trace("Argument :"+str,"- Event target :"+e.target.name);
}
这种样式的有趣之处在于,通过使用内联函数作为事件处理程序传递事件,然后调用所需的实际处理程序。相当整洁。我从未尝试过,但它看起来应该对我有用。您不能将自定义值附加到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;
}
删除
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) ;
});
是的,这很有可能,而且非常简单。以下是您的代码,其中包含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;
}
}
此解决方案基于。要使用自定义事件,您需要编写自定义URLLoader类:)扩展URLLoader并不困难,还有成千上万的示例,如。我不是说这是最好的解决方案,但工作,是优雅的。无论如何,链接有4个解决方案供你选择。我同意这并不难。这是我发布的链接中的一个例子,它为这个案例提供了4种解决方案。
// 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;
}
}