AS3: How to send an event from the actionscript class

I have a small chess application consisting of cells and boards. When a user moves an item to the board, I want the board cell to send an event so that the Board can listen to it and call the listener.

public class BoardCell extends Canvas { public function Sample():void { ....Some code var e:Event = new Event("newMove") dispatchEvent(e); } } 

However, I can’t catch the event in the parent class of the chessboard (not sure if I am listening to it correctly)

  public class FrontEndBoard extends ChessBoard { private var initialPoition:String; public function FrontEndBoard() { //TODO: implement function this.addEventListener(Event.ADDED_TO_STAGE, addedToStage); this.addEventListener("newMove", moveEvent); super(); } 
+6
flex actionscript-3
source share
3 answers

You have 2 options:

1) instead of this.addEventListener ("newMove", moveEvent); do BoardCell.addEventListener ("newMove", moveEvent);

2) has a buble event to the parent (if BoardCell is a child of the FrontEndBoard display, you set it as a parameter in the event constructor)

var e: Event = new event ("newMove", true).

+5
source share

I'm not sure exactly how FrontEndBoard and BoardCell are hierarchically used in your application, but you may need to tell the "newMove" event that it can bubble .

 var e:Event = new Event("newMove", true); 
+4
source share

The event that you dispatch from the BoardCell class must bubble, so it falls into any parent classes. Check the constructor arguments for the Event class, where you can set the "bubbles" flag to true.

+1
source share

All Articles