如何在 addEventListener Clicked Event 中设置变量?

发布于 2024-11-28 00:37:22 字数 1240 浏览 8 评论 0原文

我想制作一个可识别被单击项目的单击函数,这可能吗?你能教我吗?^^

这是我的尝试,但没有成功:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2024-12-05 00:37:22

事件处理程序中只能有一个参数,但传入的 Event 对象有一个属性 currentTarget,它是调度该事件的对象。

public function main() {
    _s1=new s1Mc();
    this.addChild(_s1);
    _s2=new s2Mc();
    this.addChild(_s2);

    _s1.addEventListener(MouseEvent.CLICK,ClickedF);
    _s2.addEventListener(MouseEvent.CLICK,ClickedF);
}
public function ClickedF(e:MouseEvent)
{ 
    if(e.currentTarget==_s1)
    trace("_s1");
    if(e.currentTarget==_s2)
    trace("_s2");
}

另外,按照惯例,函数名称应该以小写字符开头。只有类名称应以大写字符开头。这使得您的代码更容易被其他人理解,因为按照惯例 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.

public function main() {
    _s1=new s1Mc();
    this.addChild(_s1);
    _s2=new s2Mc();
    this.addChild(_s2);

    _s1.addEventListener(MouseEvent.CLICK,ClickedF);
    _s2.addEventListener(MouseEvent.CLICK,ClickedF);
}
public function ClickedF(e:MouseEvent)
{ 
    if(e.currentTarget==_s1)
    trace("_s1");
    if(e.currentTarget==_s2)
    trace("_s2");
}

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.

洋洋洒洒 2024-12-05 00:37:22

试试这个:

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);
        _s2.addEventListener(MouseEvent.CLICK,ClickedF);
    }
    public function ClickedF(e:MouseEvent):void
    {
        var $varMc:MovieClip = MovieClip(e.currentTarget);
        if($varMc==_s1)
        trace("_s1");
        if($varMc==_s2)
        trace("_s2");
    }
}
}

Try this:

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);
        _s2.addEventListener(MouseEvent.CLICK,ClickedF);
    }
    public function ClickedF(e:MouseEvent):void
    {
        var $varMc:MovieClip = MovieClip(e.currentTarget);
        if($varMc==_s1)
        trace("_s1");
        if($varMc==_s2)
        trace("_s2");
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文