I’ve been working on my Simon game trying to get it functional before making it look good. I made pretty good progress and it works fine (although if you hit “reset” it then breaks the game and adds two clicks whenever you click a box, will try and figure that out later though!).
My huge problem at the moment is “locking” the system when Simon is doing his moves. At the moment you can just click the colours when they are flashing, which is obviously not the point of the game. I’ve tried a system lock function that locks the player out from clicking, that works fine, but I can’t get it to unlock again after Simon has finished.
I used a setTimeout(lockOff) approach which according to my test text does unlock the game at the right time, but then I still can’t click on anything afterwards.
If anyone could have a look and offer some advice that would be hugely appreciated. I’m not really happy with how I’ve done the coding, feel like it is a mess and might be why things aren’t happening how I want them to, but I’m really at a loose end at the moment and don’t know how to solve it.
This is my pen, thank you for reading.
That’s because your timeout runs after the code in
simonTurn is executed so this condition here
if (currentTurn === "Player" && systemLock === "off")
is false because systemLock is still
You have a function that checks if it’s the players turn (in line 140). Don’t you think that’s a good place to lockOff? You don’t have to use a timeout.
I’ve got a couple of tips for you.
First of all, you use on and off values. Those variables should be booleans instead. It will be much easier to work with them in your code than using strings.
Lastly, you need to tidy up your functions. Your playerTurn code is inside the simonTurn function. You should move that event binding outside of
simonTurn and do the
currentTurn check inside the clickhandler.
Thank you for taking the time to have a look, I see now why it is still locked as the function has already executed before the lock is switched off.
Haha yes I did try putting the lockOff in the function where I check if it is the player’s turn (line 140), however that did not seem to work either, hence why I was trying the timeout approach. I think it is due to that function not working as intended, it is meant to execute once Simon has finished flashing all his colours, but it seems to do it before it is meant to, probably due to me misunderstanding the way the i variable was behaving.
I will change the on/off variables to true/false; I had forgotten that was an option! I knew my functions were a bit mixed up but as it was initially working I just muddled on - I will go back and move the playerTurn and add the check to the click event and hopefully everything will fall into place.
Thank you very much for your advice! It is much appreciated.