PY-RollingDice 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //Scene - Initialize when Scene Starts
  2. $this.dieClick=false;
  3. //totalText does not need to be called from $this.scene
  4. //We can access totalText just by totalText.text()
  5. //Scene - Update Every Frame
  6. if($this.scene.dieClick){
  7. var roll1=Math.round(random(5));
  8. var roll2=Math.round(random(5));
  9. die1.frameIndex(roll1);
  10. die2.frameIndex(roll2);
  11. //2 is added to the total since each die roll is off by 1.
  12. totalText.text(roll1+roll2+2);
  13. $this.scene.dieClick=false;
  14. }
  15. //die1 and die2 Objects: Mouse Click Events
  16. if(!$this.scene.dieClick){
  17. $this.scene.dieClick=true;
  18. }
  19. //alternate solution
  20. //die1 - Mouse Click Event
  21. //Use random() to generate numbers to simulate die roll.
  22. //Good place to teach ninjas about the possible numbers when running random(5) - 0,1,2,3,4,5
  23. //This happens to coincide with the frame indexes for the die values
  24. var roll1 = Math.round(random(5));
  25. var roll2 = Math.round(random(5));
  26. //Show ninjas how we will use the variables to make the numbers that we generated match
  27. die1.frameIndex(roll1);
  28. die2.frameIndex(roll2);
  29. var total = roll1+roll2+2; //good to show how to use another variable to store the total. Don't add 2 to start
  30. //Good place to start by showing how we can update the total by hard coding the number into the .text()
  31. //After we do the generation of values above and store the total into its own variable, we can use the
  32. //total instead of hardcoding the number
  33. totalText.text(total);
  34. //Ask what is wrong with the game at the moment without adding 2 to the total
  35. //We're using index values so it includes 0, how do we fix this?
  36. //We should +2 to the total