Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access an event handler from one class to another class as3
    text
    copied!<p>Is it possible to access an event handler from one class to another and if so how? I want to remove an event handler from an object. I have two objects say objectA and objectB. Inside objectA how can I remove an event handler for objectB. This is what I tried but it does not work.</p> <pre><code>MovieClip(root).objectB.removeEventListener(MouseEvent.CLICK, ready); </code></pre> <p><strong>Details:</strong></p> <p>I have two classes called Guest and Guest2. I wanted to know is it possible to remove an eventlistener in the Guest class from Guest2 class. Below is the full code. Note: Both class have exactly the same code</p> <pre><code>package { import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.*; public class Guest extends MovieClip { var walkSpeed:Number = 5; var oldPosX; var oldPosY; var myGlow:GlowFilter = new GlowFilter(); public function Guest() { addEventListener(MouseEvent.MOUSE_OVER, addGlow); } function addGlow(event:MouseEvent):void { filters = [myGlow]; addEventListener(MouseEvent.MOUSE_OUT, removeGlow); addEventListener(MouseEvent.CLICK, ready); } function removeGlow(event:MouseEvent):void { filters = []; } function ready(event:MouseEvent):void { filters = [myGlow]; stage.addEventListener(MouseEvent.MOUSE_DOWN, walk); removeEventListener(MouseEvent.MOUSE_OUT, removeGlow); **MovieClip(root).Guest02.addEventListener(MouseEvent.CLICK, walkTo);** } function walk(event:MouseEvent):void { oldPosX = parent.mouseX; oldPosY = parent.mouseY; rotation = Math.atan2(oldPosY - y,oldPosX - x) / Math.PI * 180; filters = []; stage.removeEventListener(MouseEvent.MOUSE_DOWN, walk); stage.addEventListener(Event.ENTER_FRAME, loop); } function loop(event:Event):void { var dx:Number = oldPosX - x; var dy:Number = oldPosY - y; var distance:Number = Math.sqrt((dx*dx)+(dy*dy)); if (distance&lt;walkSpeed) { // if you are near the target, snap to it x = oldPosX; y = oldPosY; removeEventListener(Event.ENTER_FRAME, loop); } else { x = x+Math.cos(rotation/180*Math.PI)*walkSpeed; y = y+Math.sin(rotation/180*Math.PI)*walkSpeed; } } **function walkTo(event:MouseEvent):void { _Guest02.removeEventListener(MouseEvent.CLICK, ready); }** } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload