ActionScript 3,只是学习AS3但知道一些其他语言。
什么会导致一个事件,即监听调用执行而不会抛出错误并成功调度,不被监听? 调度调用(运行):
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;
}
听取呼叫(此操作):
addEventListener( PlayerMoveFinishedEvent.MOVE_FINISHED, HandlePlayerMove );
处理程序(从未调用过):
public function HandlePlayerMove( e:PlayerMoveFinishedEvent ):void{trace("received!");}
消息:
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");
}
}
更新: 好的,这是一个全能的听众课程:
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 );
}
}
现在可以将其添加的地方类重新提供到必要的地方:
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;
......其他变量......
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;
}
}
“Squares”被childed到一个棋盘,这个棋盘被childed到我的主应用程序(扩展精灵)。棋盘显示并具有我想要的所有行为,但是当我单击一个正方形时,单击事件将触发并调度自定义事件,但永远不会收到自定义事件。
应该是所有需要的代码,其余的代码只是创建一个网格并在方块中放置随机数,这很好。
任何建议表示赞赏! :)
根据每个有“自定义事件教程”的网站,看来我拥有听取这个事件所需的一切,它只是没有发生:(
答案 0 :(得分:0)
为了回答你的问题,我们需要知道是谁派出了这个活动。您何时/何时添加事件监听器。
为什么你的HandlePlayerMove监听器是一个公共函数?
您可以尝试一种不同的方法,在Square类中使用调度程序来侦听棋盘类中的自定义事件。
// 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 } }