如何在AS3中创建自定义MouseEvent.CLICK事件(传递参数给函数)? [英] How to create custom MouseEvent.CLICK event in AS3 (pass parameters to function)?

查看:1430
本文介绍了如何在AS3中创建自定义MouseEvent.CLICK事件(传递参数给函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题并不只涉及MouseEvent.CLICK事件类型,而是已经在AS3中存在的所有类型的事件。我读了很多关于自定义事件,但是到现在为止我也搞不清楚这怎么做我想做的事情。我将试图解释,我希望你明白:

This question doesn't relate only to MouseEvent.CLICK event type but to all event types that already exist in AS3. I read a lot about custom events but until now I couldn't figure it out how to do what I want to do. I'm going to try to explain, I hope you understand:

下面是我的情况说明:

for(var i:Number; i < 10; i++){
    var someVar = i;
     myClips[i].addEventListener(MouseEvent.CLICK, doSomething);
}

function doSomething(e:MouseEvent){ /* */ }

不过,我希望能够通过的 someVar 的作为参数的 DoSomething的的。所以,我想这样的:

But I want to be able to pass someVar as a parameter to doSomething. So I tried this:

for(var i:Number; i < 10; i++){

    var someVar = i;
    myClips[i].addEventListener(MouseEvent.CLICK, function(){
        doSomething(someVar);
    });
}

function doSomething(index){ trace(index); }

这类型的作品,但并不如我所料。由于功能关闭,当MouseEvent.CLICK事件实际上是打响了的的循环已经结束, someVar 的是抱着最后一个值,这个数字的 9 的在例子。因此,每次点击的每个影片剪辑将调用的 DoSomething的的合格的 9 的作为参数。而且这不是我想要的。

This kind of works but not as I expect. Due to the function closures, when the MouseEvent.CLICK events are actually fired the for loop is already over and someVar is holding the last value, the number 9 in the example. So every click in each movie clip will call doSomething passing 9 as the parameter. And it's not what I want.

我想创建一个自定义事件应该工作,但我无法找到一个方法来触发一个自定义事件时,MouseEvent.CLICK事件被触发,参数传递给它。现在,我不知道这是否是正确的答案。

I thought that creating a custom event should work, but then I couldn't find a way to fire a custom event when the MouseEvent.CLICK event is fired and pass the parameter to it. Now I don't know if it is the right answer.

我应该怎么做,怎么样?

What should I do and how?

推荐答案

不知道更多关于你的应用程序,它似乎更喜欢你应该使用目标来传递参数,或延长的MouseEvent。前者会更符合普遍的做法,虽然。因此,例如,如果你在你的剪辑对象公开一个整数的公共财产(不管它是什么):

Without knowing more about your application, it seems more like you should use the target to pass parameters, or extend MouseEvent. The former would be more in line with common practice, though. So for example, if you exposed an integer public property on your "clip" object (whatever it is):

public class MyClip
{
   public var myPublicProperty:int;

   public function MyClip() { //... }
}

for (var i:int = 0; i < 10; i++)
{
   myClips[i].myPublicProperty = i;
   myClips[i].addEventListener(MouseEvent.CLICK, doSomething);
}

......然后,在你的事件监听器,你可以检索使用任何目标或事件(可能currentTarget当前,你的情况)的currentTarget属性该属性:

... and then, in your event listener, you could retrieve that property using either the target or currentTarget property of the event (probably currentTarget, in your case):

function doSomething(event:MouseEvent):void
{
   trace(event.currentTarget.myPublicProperty.toString());
}

这是应该做的吧!祝你好运。

That ought to do it! Good luck.

这篇关于如何在AS3中创建自定义MouseEvent.CLICK事件(传递参数给函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆