Browse Source

Add Candy Sort- Yellow

Michael Tang 3 years ago
parent
commit
8614e96ff1
1 changed files with 24 additions and 2 deletions
  1. 24 2
      02-yellow/04-08-PY-candy-sort.js

+ 24 - 2
02-yellow/04-08-PY-candy-sort.js

@@ -9,7 +9,7 @@ reference the timer code from Rain Catcher and Whack a Ninja.
 Decomposition
 * Candy move down the chute
 * Candy changes direction to moves along belt
-* Arrow keys control chutes
+* Arrow keys control chutes (use boolean)
 * Timer is used to spawn new candies
 * Check if the correct color dropped in bins
 
@@ -18,7 +18,29 @@ Decomposition
 //*************Scene Object - Initialize When Scene Starts***********//
 //Create score variable
 $this.totalScore = 0;
-//Create variable to match binColor with candyColor
+//Create variable to compare binColor with candyColor
 $this.binColor = 0;
 //boolean used to check which arrow is pressed
 bins.keyDown = false;
+
+//**************candy Object - Update Every Frame********************//
+//Candy should move only until y = 278
+if ($this.y() < 278) {
+    $this.moveY(50); //move horizontally at speed 50
+} else if ($this.x() < 408) { //at the end of the chute
+    $this.moveX(50); //move down at speed 50
+    $this.y(278); 
+} else {
+    $this.moveY(50);
+}
+
+if ($this.isTouching(bins)) { //check if candy is touching bin
+    if($this.color == $this.scene.binColor) { //candy color = bin color
+        $this.scene.totalScore += 1;
+        score.text($this.scene.totalScore);
+    } else { //candy color != bin color
+        $this.scene.totalScore -= 1;
+        score.text($this.scene.totalScore);
+    }
+    $this.remove(); //candy disappears in bin whether the color matches or not
+}