0

I've searched all over the internet but couldn't solve the problem.

I am coding a game. Before the game starts, I prompt the user for difficulty, and user have to select a difficulty to start the game by clicking one of the buttons.

function GameManager(){
 this.current = 0;
 this.difficulty = 0;
 this.toolbox = new Toolbox();
 this.promptDifficulty(); // Here it should wait for user input
 this.grid = new Grid(this.difficulty);
 this.start();
}
GameManager.prototype.promptDifficulty = function() {
 $("#difficulty").show();
 this.toolbox.center();
 var selected = false;
 $('.selection').click(function(){
 var $diff = $(this).attr('id');
 if($diff === 'easy')
 this.difficulty = 8;
 else if($diff === 'medium')
 this.difficulty = 9;
 else if($diff === 'hard')
 this.difficulty = 10;
 $('#difficulty').hide();
 });
}; 

However, it creates the Grid before the user selects the difficulty, and this messes up whole design. How can I make it wait until user clicks one of the difficulty settings?

asked Mar 27, 2014 at 3:05

1 Answer 1

3

The problem is the difficulty prompt is an async method, when you call the promptDifficulty() method, it shows the difficulty selector, but the rest of the script execution will continue before the user selects an item. So you need to use a callback to continue, which will be called once the user selects an item.

function GameManager() {
 this.current = 0;
 this.difficulty = 0;
 this.toolbox = new Toolbox();
 this.promptDifficulty($.proxy(function (difficulty) {
 this.grid = new Grid(this.difficulty);
 this.start();
 }, this));
}
GameManager.prototype.promptDifficulty = function (callback) {
 $("#difficulty").show();
 this.toolbox.center();
 var selected = false;
 $('.selection').click($.proxy(function (e) {
 var $diff = e.currentTarget.id;
 if ($diff === 'easy') this.difficulty = 8;
 else if ($diff === 'medium') this.difficulty = 9;
 else if ($diff === 'hard') this.difficulty = 10;
 $('#difficulty').hide();
 callback(this.difficulty);
 }, this));
};

Also Note the use of $.proxy(), because in the click handler this by default refers to the clicked element, so when you set this.difficulty it is not setting to the GameManager instance, you can solve by passing a custom execution context for the callback method.

Demo: Fiddle

answered Mar 27, 2014 at 3:09
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain the code so I can fix it? Because I've never got the concept of callbacks, and the code doesn't seem to work properly again. this.difficulty seems to assigned to a div element instead of an integer(8,9 or 10).
$.proxy() doesn't seem to help it. Still assigns the wrong value to the difficulty.
@BurakÖzmen missed to pass a this to $.proxy() - see the update

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.