03-05-PY-whats-inside.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //Core solution - simplified
  2. //Scene - Initialize When Scene Starts
  3. //Open doors need to be set to invisible
  4. openDoor1.visible(false);
  5. openDoor2.visible(false);
  6. openDoor3.visible(false);
  7. openDoor4.visible(false);
  8. //Door should open when clicked
  9. //We should talk the logic out with the ninja before we code
  10. //pseudo code is a great idea
  11. //open doors don't have underscores but closed doors have it
  12. //************door_1 - Mouse Click Event************************//
  13. //Check if it's closed, if it is, the sprite is visible
  14. if ($this.visible()) {
  15. $this.visible(false); //we'll hide it when it's clicked
  16. openDoor1.visible(true); //show the openDoor sprite
  17. //card image generation - should be consistent with how
  18. //Rolling Dice is taught even though it is possible to do
  19. //this in one line, Math.round() isn't needed
  20. var randomImage = random(1,8); //pick a number between 1 and 8
  21. card_1.frameIndex(randomImage); //update the card with the image
  22. } else { //don't need else
  23. // Otherwise the door is somehow already open
  24. // it would be showing the card so we can't click it.
  25. }
  26. //**************openDoor1 - Mous Click Event*********************//
  27. //Check if it's open, if it is, this sprite is visible
  28. if($this.visible()) {
  29. $this.visible(false); //hide openDoor when clicked
  30. door_1.visible(true); //show the door_1 sprite
  31. } else { //don't need else
  32. //Otherwise, the openDoor is alredy hidden,
  33. //so we wouldn't be able to click it anyways.
  34. }
  35. //Above should be the base code, after that we can tackle the objective
  36. //to only allow one door open at a time
  37. //CN Sensei Guide Solution
  38. //***************door_x - Mouse Click Events********************//
  39. if ($this.visible()) {
  40. //reset all the doors
  41. openDoor1.visible(false);
  42. openDoor2.visible(false);
  43. openDoor3.visible(false);
  44. openDoor4.visible(false);
  45. door_1.visible(true);
  46. door_2.visible(true);
  47. door_3.visible(true);
  48. door_4.visible(true);
  49. $this.visible(false); //this is the code from base code above
  50. //New part of this solution is the reset code
  51. //this part is unique for each door
  52. //openDoor1 and card_1 are changed to their corresponding
  53. //door number, so openDoor3 and card_3 are on door_3
  54. openDoor1.visible(true);
  55. card_1.frameIndex(random(1, 8));
  56. }