Skip to main content
Inspiring
May 2, 2011
Question

How to wait for user input?

  • May 2, 2011
  • 2 replies
  • 3353 views

Is there a way in as3 to pause the execution and wait for user input (click on yes or no for example)? I am looking for something like this,

var user_input:Boolean=wait_for_user_input();

//program pauses here until user click on yes or no

if (user_input) {

trace("you chose yes");

}

else {

trace("you chose no");

}

Thanks.

This topic has been closed for replies.

2 replies

aneelbakshi
Inspiring
May 2, 2011

I am also agreed with ned.

Wait for user input? I understand this as u want to perform something after user input something. U need to add event listener to textfield or the button

Known Participant
June 8, 2012

I agree with omgallnamestaken.  It seems like a lot of extra work and the creation of functions that do nothing but handle user clicks. 

My code checks to see if the player has used up all of their movement points.  EndOfTurn is triggered by a button click.  It asks the question "are you sure you want to continue even though you have points left".  The code to get a confirmation has been "modularized" into function displayConfirm so I can reuse it.  I find it very frusturating that i have to turn these 3 lines of code into several unecessary functions with listeners.

This:

function EndOfTurn(e):void {

     if (points > 0) rc = displayConfirm("Are you sure you want to continue even though you have points left?")

     if (rc = no) return;

}

Becomes This:

function displayConfirm(e):void {

//draw the message box which would normally be part of displayConfirm (seveal dozen lines of code)

messagebox.addchild(question);

messagebox.addchild(yesbtn);

messagebox.addchild(nobtn);

addChild(mesagebox);

yesbtn.addEventListener(MouseEvent.CLICK,callEndOfTurn);

nobtn.addEventListener(MouseEvent.CLICK.closeConfirmationCheck);

function closeConfirmationCheck(e):void {

removeChild(messagebox);

}

function callEndOfTurn(e):void {

removeChild(messagebox);

EndOfTurn();

}

Fine, but now displayConfirm has become specific to the EndOfTurn function. I thought about passing parameters to displayConfirm but realized that I can't because it has to accept an event from the listener and that's all it can accept.  I could have it switch based on the button's name and then dynamiclly display the right message and then call the function that it's supposed to.  But I feel this is much more compliated than the original function in which you just feed it a message and it returns a yes or no.

Inspiring
June 9, 2012

I end up doing similar things. I assign some global variables with values needed for the action then pop up a confirmation dialog. Click on "no" just remove the dialog without doing anything. Click on "yes" dispatches an event and the listener of that event will read the global variables and perform accordingly.

Ned Murphy
Legend
May 2, 2011

There will most likely be a way to do what you want, and what you just described will do it as is.  Until the user clicks the buttons, that code can be kept from executing by building it into the buttons' event handler function.  So the question is more likley a matter of what other code are you executing that needs to wait?

Inspiring
May 2, 2011

There are way too many things that need to wait and I can't build those into the event handler. I just want an easy way to get user confirmation in a large application. If I just use event listener, then when the user clicks yes, I will have to somehow tell the application what it was waiting for since the program doesn't pause for event listener (I can pass large amount of data between the events but I just feel sick about that). It would be ok if there is a event listener, once added, will pause the program until that event happens. (even that, I'm still a bit confused about how to structure the return value and event handler).

Any simple solution out there?

Ned Murphy
Legend
May 2, 2011

If you need to hold off executing a whole bunch of code until an event happens, then you need to have that code isolated into some function that gets called after the event occurs.  There is no one-liner wait-here-until-something-happens function.