irnik
irnik

Reputation: 139

Can I create EventListener in AS3 from one Object and remove it from another Object?

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");
    }
} }     

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!!!

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.

Thanks

Upvotes: 2

Views: 479

Answers (1)

Rajneesh Gaikwad
Rajneesh Gaikwad

Reputation: 1193

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
      }
  }

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

Upvotes: 1

Related Questions