我有一个弹出屏幕,玩家输入他们的名字然后点击OK。单击确定后,我希望将播放器的名称传递给主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;
}
答案 0 :(得分:1)
等待玩家输入他们的名字是一个异步过程,因此您必须等待弹出窗口调度的事件。由于弹出窗口自动关闭(从舞台上删除)一旦单击确定按钮,您可以在Event.REMOVED_FROM_STAGE
事件的该弹出窗口上收听,然后才从弹出窗口中收集数据。不要从弹出窗口中删除事件监听器,这样你就不会泄漏实例。
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
}