Lesson 18

Single Winner

Preventing multiple snaps

You might have noticed in your testing that it is possible to have more than one winner. This is not ideal...

1. Create a 'snapMade' variable

Below your player objects, create a new variable. Again, we could call this new variable anything, but 'snapMade' is short and clear. This will allow us to control if a snap has been made, and stop any further points being dished out.

2. Set snapMade to "YES" in snapCheck

'snapMade' will now be set to "YES" if a player correctly gets a snap.

3. Reset snapMade in play function

At the top of our play function, now that images are reset, we should reset snapMade to "NO" so the game can continue.

4. Add snapMade logic to playTouch1 & 2

Finally, in our playTouch functions, before we run snapCheck we use some logic to see if a snap has already been made. If snapMade is equal to "NO", the program will go on to check if the images match. If snapMade is equal to "YES", snapCheck will not be run and the player cannot gain any points.

Congratulations!

It should only be possible for one player to win a point per image snap now. Have a quick play to check it is working properly!

Go to the next lesson

index.html

script-1.js

the web page

New Window
Next Lesson