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

查看:18
本文介绍了如何在 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 事件被实际触发时,for 循环已经结束并且 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);
}

... 然后,在您的事件侦听器中,您可以使用事件的 target 或 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天全站免登陆