我可以在AS3从一个对象创建事件监听器,并从其他对象中删除吗? [英] Can I create EventListener in AS3 from one Object and remove it from another Object?

查看:220
本文介绍了我可以在AS3从一个对象创建事件监听器,并从其他对象中删除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间试图解决这个问题。所以,基本上我有一个类(ADDADD)包含功能,可以创建事件监听和删除事件监听。而且我还有一个两个对象(符号1,Symbol2)告诉函数做什么 - 创建或删除

I spent lots of time trying to resolve this issue. So, basically I have an class (addadd) that contain function which can create eventListener and remove eventListener. And I have another two objects (Symbol1, Symbol2) that tell the function what to do - create or remove.

    package {

import flash.display.*;
import flash.events.Event;
import fl.motion.MotionEvent;
import flash.events.MouseEvent;

public class addadd
{

    var stanishev:stanishev_line = new stanishev_line;

    public function addadd()
    {
        // constructor code
    }

    public function stanishevF(par1)
    {
        if (par1 == "create")
        {
        Main.display.addChild(stanishev);
        stanishev.name = "stanishev_child";
        stanishev.x = -200;
        stanishev.y = 500;
        stanishev.gotoAndPlay("start");
        stanishev.addEventListener(Event.ENTER_FRAME, frameDOstanishev);
        }
        else 
        {
            trace ("asasasas");
        stanishev.removeEventListener(Event.ENTER_FRAME, frameDOstanishev);
        }
    }

    public function frameDOstanishev(e:Event)
    {
        trace (stanishev.currentFrame);
    }
} }    

package {

import flash.display.SimpleButton;
import flash.events.MouseEvent;


public class Symbol1 extends SimpleButton
{

    var call_creator:addadd = new addadd;


    public function Symbol1()
    {
        // constructor code
        addEventListener(MouseEvent.MOUSE_OVER, eventResponse);
        addEventListener(MouseEvent.MOUSE_DOWN, eventResponse2);

    }

    function eventResponse(e:MouseEvent)
    {
        call_creator.stanishevF("create");
    }

    function eventResponse2(e:MouseEvent)
    {
        call_creator.stanishevF("destroy");
    }
} }    

package {


import flash.display.SimpleButton;
import flash.events.MouseEvent;


public class Symbol2 extends SimpleButton
{
    var call_creator:addadd = new addadd;

    public function Symbol2()
    {
        // constructor code
        addEventListener(MouseEvent.MOUSE_DOWN, eventResponse2);
    }

    function eventResponse2(e:MouseEvent)
    {
        call_creator.stanishevF("destroy");
    }
} }     

所以,我可以ADDADD类来创建和删除该事件监听从符号1,但我不能让ADDADD类来创建此事件监听发送的符号1创造的参数,并通过发送消灭参数从Symbol2删除! !

So I can make addadd class to create and remove this eventListener from Symbol1 but I am not able to make the addadd class to create this eventListener sending "create" parameter from Symbol1 and remove it from Symbol2 by sending "destroy" parameter!!!

如何创建并从不同的对象中删除相同的事件监听?我发现这种方式创建和查杀听众更有组织性,但我不知道这是正确的方式。 我有问题与听众(错误:1009)当我移动到主时间轴帧之间,所以我想跳到另一个帧之前杀了他们所有的

How can I create and remove the same eventListener from different objects? I found this approach creating and killing the listeners for more organized but I am not sure if this is the right way. I am having problems with the listeners (error: 1009) when I move between frames in the main timeline, so I want to kill them all before jump to another frame.

感谢

推荐答案

您可以把公共职能表示, removeListeners() addListeners()在类中调用其他类的功能,像这样,

You can put public function say, removeListeners() and addListeners() in that class and call those function from other class, like so,

 class A ()
 {

      //constructor code


      public function removeListeners():void
      {

            //remove listeners here
      }
  }

然后把这个 removeListeners() 公共职能从类(如为主。为),您已经创建了这个A类的实例。

Then call this removeListeners() public function from the class(e.g. Main.as) where you have created instance of this "A" class.

这篇关于我可以在AS3从一个对象创建事件监听器,并从其他对象中删除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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