WILSON AS3 CARD MATCHING GAME WITHOUT COVER EFFECT TUTRIAL 5
WITHOUT TIMER
IT WORKS RIGHT
WHEN I REMOVE TIMER IT GIVES ERROR SUCH AS DUPILCATE
VARIABLE IN FOR LOOP I CHANGE j TO k & m
---------------------------------------------------------------------------------------------------------------------
//Add back the event listener
for(var k:int = 0; k < bCards.length; k++)
{
bCards[k].addEventListener(MouseEvent.CLICK, openCard);
}
---------------------------------------------------------------------------------------------------------------------
//Add back the event listener
for(var m:int = 0; m < bCards.length; m++)
{
bCards[m].addEventListener(MouseEvent.CLICK, openCard);
}
---------------------------------------------------------------------------------------------------------------------
FULL CODE:
----------------------------------------------------------------------------------------------------------------------
/*AS3
Script by Wilson Oh A.K.A Trust me, I'm a programmer
5 June 2012*/
//Store all the front view movie clip inside
var fCards:Array = new Array(mc1, mc2, mc3, mc4);
//Store all the back view movie clip inside
var bCards:Array = new Array(mc1, mc2, mc3, mc4);
//Needed for game
var indexNum:Number;
var indexNum2:Number;
var clicked1Time:Boolean = false;
//This loop will change their position between each other.
for(var i:int = 0; i < fCards.length; i++)
{
//Use to store data of the random object
var randomNum:Number;
var xPos:Number;
var yPos:Number;
//Get a random number
randomNum = Math.floor(Math.random()*fCards.length);
//Store the randomNum object position
xPos = fCards[randomNum].x;
yPos = fCards[randomNum].y;
//Change between their position
fCards[randomNum].x = fCards[i].x;
fCards[randomNum].y = fCards[i].y;
fCards[i].x = xPos;
fCards[i].y = yPos;
}
//A loop that add event listener to the back view
for(var j:int = 0; j < bCards.length; j++)
{
bCards[j].addEventListener(MouseEvent.CLICK, openCard);
}
//This function make the backview unable to been seen when is clicked.
function openCard(e:MouseEvent):void
{
//To let the viewers see the front view
e.target.visible = false;
//This forumla get a set of pair number for my cards
indexNum = Math.floor(bCards.indexOf(e.target)/2);
//Check for the 2nd click so that i can active the checking of pair.
if(clicked1Time == false)
{
//The first time you click it will turn true
clicked1Time = true;
//The indexNum2 will store the first data.
indexNum2 = indexNum;
}
else
{
//Temporary remove the event listener.
for(var j:int = 0; j < bCards.length; j++)
{
bCards[j].removeEventListener(MouseEvent.CLICK, openCard);
}
//This is the 2nd click and i will turn it back to normal so i can check again.
clicked1Time = false;
//Check if the both are paired up, it will make it disappear if not it will go back to the
//back view
if(indexNum == indexNum2)//
{
//This is a delay timer for the viewers to see the cards
//Start my timer
//Add back the event listener
for(var k:int = 0; k < bCards.length; k++)
{
bCards[k].addEventListener(MouseEvent.CLICK, openCard);
}
//This will take the indexNum that is paired then use it to get back
//their array index so i can hide it from stage.
fCards[Math.floor(indexNum*2)].visible = false;
fCards[Math.floor(indexNum*2) + 1].visible = false;
//Stop my timer
}
else
{
//A delay timer for my viewers to see the cards
//Add back the event listener
for(var m:int = 0; m < bCards.length; m++)
{
bCards[m].addEventListener(MouseEvent.CLICK, openCard);
}
//This will show the back view of the cards again.
bCards[Math.floor(indexNum*2)].visible = true;
bCards[Math.floor(indexNum*2) + 1].visible = true;
bCards[Math.floor(indexNum2*2)].visible = true;
bCards[Math.floor(indexNum2*2) + 1].visible = true;
//Stop my timer
}
}
}
EmoticonEmoticon