import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.events.MouseEvent;
var reset_mc:mcReset = new mcReset();
var cupAx:Number = new Number(cupA_mc.x);
var cupAy:Number = new Number(cupA_mc.y);
var cupBx:Number = new Number(cupB_mc.x);
var cupBy:Number = new Number(cupB_mc.y);
var cupCx:Number = new Number(cupC_mc.x);
var cupCy:Number = new Number(cupC_mc.y);
var randomFirst:Number;
var randomSecond:Number;
var cupsArray:Array = new Array(cupA_mc, cupB_mc, cupC_mc);
var level:Number = new Number(0);
var counter:Number = new Number(0);
var time:Number = new Number(.6);
var life:Number = new Number(3);
play_mc.addEventListener(MouseEvent.CLICK, startGame);
play_mc.buttonMode = true;
level_txt.text = "0";
life_txt.text = life.toString();
function startGame(e:MouseEvent):void
{
// Disabling the play button
play_mc.alpha = 0;
play_mc.buttonMode = false;
play_mc.removeEventListener(MouseEvent.CLICK, startGame);
//Fading out the coin before the cup start moving
var myAlphaTween:Tween = new Tween(coin_mc, "alpha", Regular.easeOut, 1, 0, 1, true);
myAlphaTween.addEventListener(TweenEvent.MOTION_FINISH, selectRandomCup);
counter = 0;
level_txt.text = level.toString();
}
function selectRandomCup(e:TweenEvent):void
{
randomFirst = Math.ceil(Math.random()* 3); //generating the first random number
randomSecond = Math.ceil(Math.random()* 3); // generating the second random number
while(randomFirst == randomSecond) //run this loop untill the random second no is different than random first no
{
randomSecond = Math.ceil(Math.random()* 3);
}
if(randomFirst != randomSecond) // once the random numbers are different start swaping
{
//Getting cups from cupsArray using its index number
// Since the random no is between 1-3 and array length is 2, -1 is used so that there is no error.
startSpin(cupsArray[randomFirst-1], cupsArray[randomSecond-1]);
}
}
function startSpin(firstCup:MovieClip, secondCup:MovieClip):void
{
disableCupClicks();// disabling the cups to be clicked
setChildIndex(coin_mc,0); //Sending the coin to back of the cup(add remove child doesn't work,bug)
//Moving the first coin
var cupATweenX:Tween = new Tween(firstCup, "x", Regular.easeOut, firstCup.x, secondCup.x, time, true);
var cupATweenY:Tween = new Tween(firstCup, "y", Regular.easeOut, firstCup.y, secondCup.y, time, true);
//Moving the second coin
var cupBTweenX:Tween = new Tween(secondCup, "x", Regular.easeOut, secondCup.x, firstCup.x, time, true);
var cupBTweenY:Tween = new Tween(secondCup, "y", Regular.easeOut, secondCup.y, firstCup.y, time, true);
//increasing the counter once the move is completed
counter++;
//once the animation is doing call swap again
// Use the MOTION_STOP. For some reason MOTION_FINISH keeps on freezing randomly
cupBTweenY.addEventListener(TweenEvent.MOTION_STOP, swapAgain);
}
function swapAgain(e:TweenEvent):void
{
if(level >= counter) //Checking if the move needs to occur again
{
selectRandomCup(e);
}
else
{
enableCupClicks();
}
}
function enableCupClicks():void
{
cupA_mc.buttonMode = true;
cupB_mc.buttonMode = true;
cupC_mc.buttonMode = true;
cupA_mc.addEventListener(MouseEvent.CLICK, rightAnswer);
cupB_mc.addEventListener(MouseEvent.CLICK, wrongAnswer);
cupC_mc.addEventListener(MouseEvent.CLICK, wrongAnswer);
}
function disableCupClicks():void
{
cupA_mc.buttonMode = false;
cupB_mc.buttonMode = false;
cupC_mc.buttonMode = false;
cupA_mc.removeEventListener(MouseEvent.CLICK, rightAnswer);
cupB_mc.removeEventListener(MouseEvent.CLICK, wrongAnswer);
cupC_mc.removeEventListener(MouseEvent.CLICK, wrongAnswer);
}
function rightAnswer(e:MouseEvent):void
{
setChildIndex(coin_mc,5); //Bringing the coing to front
coin_mc.x = cupA_mc.x + cupA_mc.width/2 - coin_mc.width/2; // matching its position with cup A
coin_mc.y = cupA_mc.y+ cupA_mc.height/2 - coin_mc.height/2;
var myAlphaTween:Tween = new Tween(coin_mc, "alpha", Regular.easeOut, 0, 1, .5, true); // Animating it to be visible
checkTime(); //reducing the time if needed
level++; //increasing the level
startGame(e); //starting the game again
}
function wrongAnswer(e:MouseEvent):void
{
if(life >= 1)
{
life --; // Reducing the life count
life_txt.text = life.toString(); //Dispaying the text
setChildIndex(coin_mc,5); // Making the coin visible
coin_mc.x = cupA_mc.x + cupA_mc.width/2 - coin_mc.width/2; // Matching its posistion with cup A
coin_mc.y = cupA_mc.y+ cupA_mc.height/2 - coin_mc.height/2;
var myAlphaTween:Tween = new Tween(coin_mc, "alpha", Regular.easeOut, 0, 1, .5, true);
startGame(e);
}
else // if the life is 0
{
disableCupClicks();
addChild(reset_mc); // show Reset gamebutton
reset_mc.x = -1;
reset_mc.y =stage.stageHeight - reset_mc.height;
reset_mc.buttonMode = true;
reset_mc.addEventListener(MouseEvent.CLICK, reload);
}
}
function reload(e:MouseEvent):void
{
// Reseting everything
removeChild(reset_mc);
play_mc.alpha = 1;
play_mc.buttonMode = true;
play_mc.addEventListener(MouseEvent.CLICK, startGame);
level = 0;
level_txt.text = level.toString();
counter = 0;
time = .6;
life = 3;
life_txt.text = life.toString();
setChildIndex(coin_mc,5);
coin_mc.x = cupA_mc.x + cupA_mc.width/2 - coin_mc.width/2;
coin_mc.y = cupA_mc.y+ cupA_mc.height/2 - coin_mc.height/2;
}
function checkTime():void
{
// if the time is less than 0.2 sec it is very hard to see the animation
if(time >= .2)
{
time -= .07;
}
}