12-17-2010 10:16 AM
I have the following code to display a prompt dialog in which text can be entered after a user clicks a button. The prompt dialog shows 2 buttons: OK and Cancel. Whatever button I click, the program never gets into the event listener function 'onPromptButtonClicked' (I don't see the text "Hello world" in the debugging output).
What am I doing wrong here?
private function onBtnAddTextClicked (event:MouseEvent):void {
var prompt
romptDialog = new PromptDialog();
prompt.title = "Title";
prompt.message = "Please enter text:";
prompt.addButton("OK");
prompt.addButton("Cancel");
prompt.addEventListener(DialogEvent.DIALOG_BUTTO
prompt.show();
}
private function onPromptButtonClicked(event
ialogEvent):void {
trace("Hello world");
}
Solved! Go to Solution.
12-17-2010 10:19 AM
hey eugenevk,
yeah unfortunately this is a bug and hopefully gets fixed soon. it was discussed on the forums prior and a work around for it was the following:
Replace:
prompt.addEventListener(DialogEvent.DIALOG_BUTTON_CLICKED, onPromptButtonClicked);
with:
prompt.addEventListener(Event.SELECT, onPromptButtonClicked);
and it should do what you need it to do. here is a thread for reference if you need more information:
hope that helps. good luck!
12-17-2010 10:33 AM
Super, that works. Was breaking my head on it.
12-17-2010 10:39 AM
JRab, would you also know how to use/access the value of the text entered in the input field of the prompt dialog in de event handler function?
12-17-2010 10:52 AM
hey eugenvk,
sure thing! you would request the prompt object's text property by doing the following:
private var prompt:PromptDialog; // placed outside of your main application constructor
(...)
private function onBtnAddTextClicked (event:MouseEvent):void {
prompt = new PromptDialog();
prompt.title = "Title";
prompt.message = "Please enter text:";
prompt.addButton("OK");
prompt.addButton("Cancel");
prompt.addEventListener(Event.SELECT, onPromptButtonClicked);
prompt.show();
}
private function onPromptButtonClicked(event:Event):void {
trace(prompt.text);
}
the only thing to remember is to place your prompt dialog object declaration outside of the the constructor in order to have access to it by other functions such as the onPromptClicked(). hope that helps. good luck!
12-17-2010 11:30 AM
Thanks again!
Expected that, but thought there might be another way. I did btw the following, which works as well:
prompt.addEventListener(Event.SELECT, function onPromptButtonClicked(event:Event):void{
var btnOKClicked:Boolean = (event.target.selectedIndex == 0);
if (btnOKClicked && (prompt.text != "")) {
trace(prompt.text);
}
});
Rgrds,
Eugene
12-17-2010 11:41 AM
hey eugenevk,
you're absolutely right there is another method. if you go back to your old code, you can refer to the text as event.target.text. event.target being your dialog since thats where the event orginated from. sorry for the brain fart hah!
private function onPromptButtonClicked(event:Event):void
{
trace(event.target.text);
}