Need help with my simon game

Need help with my simon game
0

#1

Hi, can anyone look at my simon game project? i don’t understand why it works ok in first few steps, but than gets crazy. here is my code, http://codepen.io/Tcar/pen/GNZWGg


#2

Hi tcar,

Its because of the non-blocking nature of JavaScript.

Have a look at this, it describes the problem quite well…

Event-Based Programming: What Async Has Over Sync


#3

ok, i start it all over again… and it works good. but i want to disable user from clicking when computer is playing. i tray to do with putting unbind in ponovi function and bind in randomSeries function. but it dosn’t work. i dont know why:S http://codepen.io/Tcar/pen/GNZWGg?editors=1010


#4

There are lots of ways to achieve that - I used something called interlocks.

Create a var playersTurn or something like that, set to true when player can click, false when computer is playing.

if(playersTurn) {
    flash red();
    play redTone()
}

Here is what I did - your code will be quite different, but you should be able to make something similar work for you…

function chooseColorAndPlay(color, whoClicked) {
        if (whoClicked === 'computer') {
            switch (color) {
                case 0:
                    flashRed();
                    break;
                case 1:
                    flashGreen();
                    break;
                case 2:
                    flashYellow();
                    break;
                case 3:
                    flashBlue();
                    break;
            }
        }
        else if (whoClicked === 'player' && clickEnabled && !maskClickEvents) {
            maskClickEvents = true;
            timeout = 0;
            switch (color) {
                case 0:
                    flashRed();
                    playersMoves.unshift(0);
                    break;
                case 1:
                    flashGreen();
                    playersMoves.unshift(1);
                    break;
                case 2:
                    flashYellow();
                    playersMoves.unshift(2);
                    break;
                case 3:
                    flashBlue();
                    playersMoves.unshift(3);
                    break;
            }
        }
    }