/**
One of 3 random candies drops down the chute onto the conveyor belt and 
into one of the bins. The player uses the arrow keys to shift the bins so 
that the candy drops into the matching colored bin. There is already a Boolean 
key check (bins.keyDown) to detect if either of the arrows has been pressed. 
The code for moving the bins is already attached to the group. Ninjas should 
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 (use boolean)
* Timer is used to spawn new candies
* Check if the correct color dropped in bins

*/

//*************Scene Object - Initialize When Scene Starts***********//
//Create score variable
$this.totalScore = 0;
//Create variable to compare binColor with candyColor
$this.binColor = 0;
if ($this.scene.state() = "PLAY") {
    createTimer(2000, function() {
        var candyColor = random(2);
        var nCandy = candy.clone();
        nCandy.color = candyColor;
        nCandy.frameIndex(candyColor);
        nCandy.x(58);
        nCandy.y(154);
 
    });
}
//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
}