如何在 addEventListener Clicked Event 中设置变量?
我想制作一个可识别被单击项目的单击函数,这可能吗?你能教我吗?^^
这是我的尝试,但没有成功:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class main extends MovieClip {
private var _s1:s1Mc;
private var _s2:s2Mc;
public function main() {
_s1=new s1Mc();
this.addChild(_s1);
_s2=new s2Mc();
this.addChild(_s2);
_s1.addEventListener(MouseEvent.CLICK,ClickedF(_s1));
_s2.addEventListener(MouseEvent.CLICK,ClickedF(_s2));
}
public function ClickedF(e:MouseEvent,$varMc:MovieClip)
{
if($varMc==_s1)
trace("_s1");
if($varMc==_s2)
trace("_s2");
}
}
}
这是错误:
F:\test\click test\main.as, Line 18 1067: Implicit coercion of a value of type s1Mc to an unrelated type flash.events:MouseEvent.
F:\test\click test\main.as, Line 18 1136: Incorrect number of arguments. Expected 2.
F:\test\click test\main.as, Line 19 1067: Implicit coercion of a value of type s2Mc to an unrelated type flash.events:MouseEvent.
F:\test\click test\main.as, Line 19 1136: Incorrect number of arguments. Expected 2.
我明白我应该在 addEventListener 的 ClickedF 中设置 2 件事,但我真的不知道该怎么做 T_T
I want to make a clicked function that recognized the item being click, is that possible? Could you teach me ?^^
Here is my try, but It didn't work:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class main extends MovieClip {
private var _s1:s1Mc;
private var _s2:s2Mc;
public function main() {
_s1=new s1Mc();
this.addChild(_s1);
_s2=new s2Mc();
this.addChild(_s2);
_s1.addEventListener(MouseEvent.CLICK,ClickedF(_s1));
_s2.addEventListener(MouseEvent.CLICK,ClickedF(_s2));
}
public function ClickedF(e:MouseEvent,$varMc:MovieClip)
{
if($varMc==_s1)
trace("_s1");
if($varMc==_s2)
trace("_s2");
}
}
}
Here is the Errors:
F:\test\click test\main.as, Line 18 1067: Implicit coercion of a value of type s1Mc to an unrelated type flash.events:MouseEvent.
F:\test\click test\main.as, Line 18 1136: Incorrect number of arguments. Expected 2.
F:\test\click test\main.as, Line 19 1067: Implicit coercion of a value of type s2Mc to an unrelated type flash.events:MouseEvent.
F:\test\click test\main.as, Line 19 1136: Incorrect number of arguments. Expected 2.
I understand that I should set 2 things inside the ClickedF of addEventListener but I really don't know how to do it T_T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
事件处理程序中只能有一个参数,但传入的 Event 对象有一个属性
currentTarget
,它是调度该事件的对象。另外,按照惯例,函数名称应该以小写字符开头。只有类名称应以大写字符开头。这使得您的代码更容易被其他人理解,因为按照惯例
ClickedF
看起来您正在传递对类的引用,而不是函数。在开发团队中工作时,遵守这些标准对于提高生产力至关重要,并且通常是工作场所的要求,因此最好现在就养成这个习惯。You can only have one argument in an event handler, but the Event object that comes in has a property
currentTarget
, which is that object that dispatched the event.Also, by convention you should start your function names with a lowercase character. Only Class names should start with an uppercase character. This makes your code easier for others to understand, because by convention
ClickedF
looks like you are passing a reference to a class, not a function. When working in a team of developers sticking to these standards is essential for productivity and is usually a workplace requirement, so it's best to get into the habit now.试试这个:
Try this: