我可以从一个Object在AS3中创建EventListener并将其从另一个Object中删除吗?

时间:2013-04-07 09:09:41

标签: actionscript-3 event-listener

我花了很多时间试图解决这个问题。所以,基本上我有一个类(addadd),它包含可以创建eventListener并删除eventListener的函数。我有另外两个对象(Symbol1,Symbol2)告诉函数该做什么 - 创建或删除。


    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类来从Symbol1创建和删除这个eventListener但是我无法使addadd类创建这个eventListener从Symbol1发送“create”参数并通过发送“destroy”参数从Symbol2中删除它! !

如何从不同的对象创建和删除相同的eventListener?我发现这种方法创建并杀死了更有条理的听众,但我不确定这是否是正确的方法。 当我在主时间轴中的帧之间移动时,我遇到了侦听器的问题(错误:1009),因此我想在跳转到另一帧之前将它们全部删除。

由于

1 个答案:

答案 0 :(得分:1)

您可以将public function说,removeListeners()addListeners()放在该类中,然后从其他类调用这些函数,就像这样,

 class A ()
 {

      //constructor code


      public function removeListeners():void
      {

            //remove listeners here
      }
  }

然后从您创建此“A”类实例的类(例如removeListeners())中调用此public function Main.as

相关问题