02-03-PY-RollingDice.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //CN Sensei Guide Solution
  2. //*******Scene - Initialize when Scene Starts*************//
  3. $this.dieClick=false;
  4. //totalText does not need to be called from $this.scene
  5. //We can access totalText just by totalText.text()
  6. //***********Scene - Update Every Frame********************//
  7. if($this.scene.dieClick){
  8. var roll1=Math.round(random(5));
  9. var roll2=Math.round(random(5));
  10. die1.frameIndex(roll1);
  11. die2.frameIndex(roll2);
  12. //2 is added to the total since each die roll is off by 1.
  13. totalText.text(roll1+roll2+2);
  14. $this.scene.dieClick=false;
  15. }
  16. //************die1 and die2 Objects: Mouse Click Events**********//
  17. if(!$this.scene.dieClick){
  18. $this.scene.dieClick=true;
  19. }
  20. /*
  21. Alternate Solution - how our senseis teach it
  22. We eliminate the use of the boolean to detect the mouse
  23. click which is a topic taught in the next section
  24. Decomposition of Rolling Dice:
  25. * When die is clicked, 2 numbers need to generated and
  26. stored into 2 variables
  27. * The variables will be use to show the correct image
  28. using frameIndex()
  29. * The two variables are added together and stored in
  30. a separate variable
  31. * This variable (sum) will be displayed in a label - which
  32. will be an incorrect sum
  33. * last part is figuring out how to make the sum correct
  34. */
  35. //*********************************die1 - Mouse Click Event**********************************************//
  36. //Use random() to generate numbers to simulate die roll.
  37. //Good place to teach ninjas about the possible numbers when running random(5) - 0,1,2,3,4,5
  38. //This happens to coincide with the frame indexes for the die values
  39. var roll1 = Math.round(random(5));
  40. var roll2 = Math.round(random(5));
  41. //Show ninjas how we will use the variables to make the numbers that we generated match
  42. die1.frameIndex(roll1);
  43. die2.frameIndex(roll2);
  44. var total = roll1+roll2+2; //good to show how to use another variable to store the total. Don't add 2 to start
  45. //Good place to start by showing how we can update the total by hard coding the number into the .text()
  46. //After we do the generation of values above and store the total into its own variable, we can use the
  47. //total instead of hardcoding the number
  48. totalText.text(total);
  49. //Ask what is wrong with the game at the moment without adding 2 to the total
  50. //We're using index values so it includes 0, how do we fix this?
  51. //We should +2 to the total