views:

27

answers:

1

Hi,

ActionScript 3, just learning AS3 but know a handful of other languages.

What would cause an event that the listen call executes without throwing an error and was successfully dispatched, to not be listened to? dispatch call (this runs ):

private function HandleMouseClick( e:Event ):void{
if ( m_MouseState == MOUSE_STATE_SELECTED )
    m_MouseState = MOUSE_STATE_NONE;
else
{
    m_MouseState = MOUSE_STATE_SELECTED;
    var evt:PlayerMoveFinishedEvent =  new PlayerMoveFinishedEvent( m_uiValue, Operation.ADDITION );
    var result:Boolean = dispatchEvent( evt );
    trace(result);
}

m_bDirty = true;

}

Listen call (this runs ):

addEventListener( PlayerMoveFinishedEvent.MOVE_FINISHED, HandlePlayerMove );

Handler (never called ):

public function HandlePlayerMove( e:PlayerMoveFinishedEvent ):void{trace("received!");}

message:

public class PlayerMoveFinishedEvent extends Event 
{
    public static const MOVE_FINISHED:String = "MoveFinished111wefsdsdvs";
    private var m_uiValue:uint;
    public var m_Operation:uint;

    public function PlayerMoveFinishedEvent( _uiValue:uint, _Operation:uint ) 
    { 
        super( PlayerMoveFinishedEvent.MOVE_FINISHED);

        m_uiValue = _uiValue;
        m_Operation = _Operation;

    } 

    public function get Value():uint 
    {
        return m_uiValue;
    }

    public function get Operation():uint
    {
        return m_Operation;
    }

    public override function clone():Event 
    { 
        return new PlayerMoveFinishedEvent( m_uiValue, m_Operation );
    } 

    public override function toString():String 
    { 
        return formatToString("PlayerMoveFinishedEvent", "type", "bubbles", "cancelable", "eventPhase"); 
    }

}

(sorry about the formatting, i blame the preview )

update: ok, here's the fulll listener class:

public class MoveTracker extends Sprite
{
    private var m_kCurrentOperation:Operation = null;
    private var m_uiTotalMoves:uint = 0;

    public function MoveTracker() 
    {
        super();
        var s:String = PlayerMoveFinishedEvent.MOVE_FINISHED;
        addEventListener( s, HandlePlayerMove );
    }

    public function HandlePlayerMove( e:PlayerMoveFinishedEvent ):void
    {
        var runningTotal:uint = 0;
        if ( m_kCurrentOperation != null )
            runningTotal = m_kCurrentOperation.RunningTotal;

        m_kCurrentOperation = new Operation( runningTotal, e.Value, e.Operation, m_kCurrentOperation );
        // draw in the right spot.
        m_kCurrentOperation.y = m_uiTotalMoves * m_kCurrentOperation.y;
        m_uiTotalMoves++;
        addChild( m_kCurrentOperation );
    }


}

now the place class where its added can be reuced to the necessary:

public class Square extends Sprite{     
private static var MOUSE_STATE_NONE:uint = 0;
private static var MOUSE_STATE_HOVER:uint = 1;
private static var MOUSE_STATE_SELECTED:uint = 2;

... other variables here ...

public function Square(_x:Number, _y:Number, fWidth:Number, fHeight:Number, colour:uint, value:uint) 
{   
    super();
    x = _x;
    y = _y;


    m_kSize = new Point( fWidth, fHeight );
    m_uiColour = colour;
    m_uiSelectedColour = 0xFF80C0;
    m_uiHoverColour = 0x9BDB88;
    m_uiValue = value;


    var kTextDisplay:Bitmap;
    var kTextDisplayData:BitmapData;            
    kTextDisplayData = new BitmapData(m_kSize.x, m_kSize.y, true, 0x00FFFFFF);
    DText.draw( kTextDisplayData, m_uiValue.toString(), 0, 0, DText.LEFT );
    kTextDisplay = new Bitmap( kTextDisplayData );

    // position the number in the centre of the square.
    kTextDisplay.x = m_kSize.x * 0.5 - ( 8 * ( m_uiValue.toString().length * 0.5 ) ) ;
    kTextDisplay.y = m_kSize.y * 0.5 - 8;
    addChild( kTextDisplay );

    m_MouseState = MOUSE_STATE_NONE;

    addEventListener(MouseEvent.MOUSE_OVER, HandleMouseEnter);
    addEventListener(MouseEvent.MOUSE_OUT, HandleMouseExit);

    addEventListener(MouseEvent.CLICK, HandleMouseClick );

    addEventListener(Event.RENDER, Draw );

    m_bDirty = true;
}
private function HandleMouseClick( e:Event ):void 
{
    if ( m_MouseState == MOUSE_STATE_SELECTED )
        m_MouseState = MOUSE_STATE_NONE;
    else
    {
        m_MouseState = MOUSE_STATE_SELECTED;
        var evt:PlayerMoveFinishedEvent =  new PlayerMoveFinishedEvent( m_uiValue, Operation.ADDITION );
        var result:Boolean = dispatchEvent( evt );

    }

    m_bDirty = true;
}

}

the "Squares" are childed to a checkerboard that is childed to my main app ( which extends sprite ). the checkerboard displays and has all the behaviour i want, but when i click a square, the click event fires and dispatches the custom event, but the custom event is never received.

that should be all code needed, the rest of the code is just to create a grid and put random numbers in the squares, which works fine.

any advice is appreciated! :)

according to every website that has a "custom event tutorial" it would appear that i have everything needed to listen to the event, its just not happening :(

A: 

In order to answer your question, we need to know who's dispatching the event & where/when you add your event listener.

Why is your HandlePlayerMove listener a public function?

You could try a different approach where you would use a dispatcher in the Square class that would listen to the custom event in the checkerboard class.

// In your Main Class, you create a new instance of an EventDispatcher
// and pass it as a parameter to the children
public class Main extends Sprite
{
    private var dispatcher:EventDispatcher;

    public function Main()
    {
       dispatcher = new EventDispatcher();
       dispatcher.addEventListener( PlayerMoveFinishedEvent.MOVE_FINISHED, 
                HandlePlayerMove )
    }
    // later in your code when you create a Square
    var square = new Square(dispatcher );
}

public class Square extends Sprite
{
     private var _dispatcher:EventDispatcher;

     public function Square(dispatcher:EventDispatcher):void
     {
         this._dispatcher = dispatcher;
     }

     //etc....

     private function HandleMouseClick( e:Event ):void 
     {
       //.... your code here
        _dispatcher.dispatchEvent( evt );
       //.... your code here
     }
}

PatrickS
its public because before coming to this place, i tried every little thing i could think of that might be effecting what is otherwise code taken straight from the docs, but is not working.
Simon Dobie
thank you, that worked!
Simon Dobie