ActionScript:如何监听来自不同 XML 的事件
ActionScript: How to listen for an event from a different XML
我有一个弹出屏幕,玩家可以在其中输入他们的名字并单击“确定”。单击“确定”后,我希望将玩家的名字传递给主 XML。我该怎么做?
这是处理弹出窗口的主要 XML 函数:
private function NewHighScore():void{
highScorePopup = PopUpManager.createPopUp(this, Popup, true) as Popup;
highScorePopup.SetScore(playerscore);
PopUpManager.centerPopUp(highScorePopup);
playerName = highScorePopup.getName();
trace(playerName);
}
这是弹出 XML 脚本:
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
import spark.events.TextOperationEvent;
public var playerName:String;
public function SetScore (playerScore:int):void{
scoreDisplay.text = "You achieved a new high score of " + playerScore.toString();
}
protected function button1_clickHandler(event:MouseEvent):void{ remove(); }
private function remove():void{ PopUpManager.removePopUp(this);}
protected function titlewindow1_closeHandler(event:CloseEvent):void
{ remove();}
protected function nameBox_changeHandler(event:TextOperationEvent):void
{playerName = nameBox.text;}
public function getName():String{
return playerName;
}
等待玩家输入名字是一个异步过程,因此您必须等待弹窗派发的事件。由于单击“确定”按钮后弹出窗口会自行关闭(从舞台上删除),因此您可以在该弹出窗口中侦听 Event.REMOVED_FROM_STAGE
事件,然后才从弹出窗口中收集数据。不要 for get 从弹出窗口中删除事件侦听器,以免泄漏实例。
private function NewHighScore():void{
highScorePopup = PopUpManager.createPopUp(this, Popup, true) as Popup;
highScorePopup.SetScore(playerscore);
PopUpManager.centerPopUp(highScorePopup);
highScorePopup.addEventListener(Event.REMOVED_FROM_STAGE,getPlayerName);
}
function getPlayerName(event:Event):void {
event.target.removeEventListener(Event.REMOVED_FROM_STAGE,getPlayerName);
var popup:Popup=event.target as Popup;
if (!popup) return;
var playerName:String=popup.getName(); // now the name will be populated
// do stuff with the name
}
我有一个弹出屏幕,玩家可以在其中输入他们的名字并单击“确定”。单击“确定”后,我希望将玩家的名字传递给主 XML。我该怎么做?
这是处理弹出窗口的主要 XML 函数:
private function NewHighScore():void{
highScorePopup = PopUpManager.createPopUp(this, Popup, true) as Popup;
highScorePopup.SetScore(playerscore);
PopUpManager.centerPopUp(highScorePopup);
playerName = highScorePopup.getName();
trace(playerName);
}
这是弹出 XML 脚本:
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
import spark.events.TextOperationEvent;
public var playerName:String;
public function SetScore (playerScore:int):void{
scoreDisplay.text = "You achieved a new high score of " + playerScore.toString();
}
protected function button1_clickHandler(event:MouseEvent):void{ remove(); }
private function remove():void{ PopUpManager.removePopUp(this);}
protected function titlewindow1_closeHandler(event:CloseEvent):void
{ remove();}
protected function nameBox_changeHandler(event:TextOperationEvent):void
{playerName = nameBox.text;}
public function getName():String{
return playerName;
}
等待玩家输入名字是一个异步过程,因此您必须等待弹窗派发的事件。由于单击“确定”按钮后弹出窗口会自行关闭(从舞台上删除),因此您可以在该弹出窗口中侦听 Event.REMOVED_FROM_STAGE
事件,然后才从弹出窗口中收集数据。不要 for get 从弹出窗口中删除事件侦听器,以免泄漏实例。
private function NewHighScore():void{
highScorePopup = PopUpManager.createPopUp(this, Popup, true) as Popup;
highScorePopup.SetScore(playerscore);
PopUpManager.centerPopUp(highScorePopup);
highScorePopup.addEventListener(Event.REMOVED_FROM_STAGE,getPlayerName);
}
function getPlayerName(event:Event):void {
event.target.removeEventListener(Event.REMOVED_FROM_STAGE,getPlayerName);
var popup:Popup=event.target as Popup;
if (!popup) return;
var playerName:String=popup.getName(); // now the name will be populated
// do stuff with the name
}