PY-RollingDice.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. //Alternate Solution - how our senseis teach it
  21. //We eliminate the use of the boolean to detect the mouse
  22. //click which is a topic taught in the next section
  23. //die1 - Mouse Click Event
  24. //Use random() to generate numbers to simulate die roll.
  25. //Good place to teach ninjas about the possible numbers when running random(5) - 0,1,2,3,4,5
  26. //This happens to coincide with the frame indexes for the die values
  27. var roll1 = Math.round(random(5));
  28. var roll2 = Math.round(random(5));
  29. //Show ninjas how we will use the variables to make the numbers that we generated match
  30. die1.frameIndex(roll1);
  31. die2.frameIndex(roll2);
  32. var total = roll1+roll2+2; //good to show how to use another variable to store the total. Don't add 2 to start
  33. //Good place to start by showing how we can update the total by hard coding the number into the .text()
  34. //After we do the generation of values above and store the total into its own variable, we can use the
  35. //total instead of hardcoding the number
  36. totalText.text(total);
  37. //Ask what is wrong with the game at the moment without adding 2 to the total
  38. //We're using index values so it includes 0, how do we fix this?
  39. //We should +2 to the total