06-scramble.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. This activity uses arays and loops to store and spawn tile objects. The Player must click the tiles and find the matching pairs.
  3. */
  4. //*************Scene Object - Initialize When Scene Starts Event********************//
  5. //************************FUNCTIONS************************//
  6. // this function takes an array
  7. // shuffles it randomly and returns it
  8. $this.shuffle = function(arr){
  9. arr.sort(() => random(1, -1));
  10. return arr;
  11. };
  12. $this.checkMatch = function() {
  13. // if there is a match
  14. if ($this.firstCard.picture === $this.secondCard.picture) {
  15. // remove the cards
  16. $this.firstCard.remove();
  17. $this.secondCard.remove();
  18. } else {
  19. // if the cards do not match
  20. // flip them over
  21. $this.firstCard.frameIndex(0);
  22. $this.secondCard.frameIndex(0);
  23. }
  24. // no matter what, clear out the firstCard and secondCard variables
  25. $this.firstCard = null;
  26. $this.secondCard = null;
  27. $this.isChecking = false;
  28. };
  29. // if the scene is in PLAY state
  30. if ($this.scene.state() == "PLAY") {
  31. // initialize the array of cards
  32. $this.orderedCardList = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8];
  33. // array stores the data and each number represents a card
  34. // randomize the array of cards
  35. $this.randomCardList = [];
  36. $this.randomCardList = $this.shuffle($this.orderedCardList);
  37. // set up helper variables for the loops
  38. var cardListIndex = 0;
  39. var cardSpacing = 100;
  40. var xOffset = 200;
  41. var yOffset = 70;
  42. // place the 16 cards
  43. // 4 cards in the x dirction
  44. for (var cx = 0; cx < 4; cx++) {
  45. // 4 cards in the y direction
  46. for (var cy = 0; cy < 4; cy++) {
  47. var cardClone = card.clone();
  48. // place cards in 4x4 grid using spacing
  49. cardClone.x(cx * cardSpacing + xOffset);
  50. cardClone.y(cy * cardSpacing + yOffset);
  51. // give the card a picture based on random array
  52. cardClone.picture = $this.randomCardList[cardListIndex];
  53. cardListIndex++;
  54. }
  55. }
  56. // hide the original card
  57. card.visible(false);
  58. // store references to the selected cards
  59. $this.firstCard = null;
  60. $this.secondCard = null;
  61. // track if the scene is checking for a match
  62. $this.isChecking = false;
  63. }
  64. //***********************card Object - Mouse Click Event************************//
  65. // if the scene is not checking for a match
  66. if ($this.scene.isChecking === false) {
  67. // when the player clicks the card
  68. // show the picture of the card
  69. $this.frameIndex($this.picture);
  70. // if the user has not selected a first card
  71. if (!$this.scene.firstCard) {
  72. // save a reference to the card the player clicked
  73. $this.scene.firstCard = $this;
  74. } else if ($this != $this.scene.firstCard){
  75. // save a reference to the second card
  76. $this.scene.secondCard = $this;
  77. // set isChecking to true
  78. $this.scene.isChecking = true;
  79. // tell the scene to check for a match after a delay
  80. createTimer(1000,$this.scene.checkMatch, false);
  81. }
  82. }