Monday 9 February 2015

COLOR MATCHING GAME AS2 AS3

COLOR MATCHING GAME AS3


----------------------------------------------------------------------------------------------------------------
READ MORE:
https://www.youtube.com/watch?v=5-Zh_HasUt0
http://mattmaxwellas3.blogspot.co.uk/

DOWNLOAD SOURCE FILE
http://bit.ly/1uvmco3
---------------------------------------------------------------------------
COPY AND PASTE BLOW CODE
/*HOW DOES COLOR MATCHING GAME WORKS? 1) WE MAKE SMILER COPIES OF ONE MOVIE CLIP WITH HELP OF FOR FOR LOOP METHOD 2) EACH MOVIE CLIP HAS SAME NUMBERS OF FRAME 3) EACH COPY GO AND STOP ON RANDOM FRAME 4) IF BOTH RANDOM FRAMES SAME THEN BOTH COPIES REMOVE ELSE RETURN TO FIRST FRAME*/
---------------------------------------------------------------------------

// MAKE MOVIE CLIP WITHOUT INSTANCE NAME WITH INSIDE DIFFRENT 9 COLORS FRAME
// GOES TO MOVIE CLIP PROPETIES AND CHECKED TWO BOXES AND SET CLASS " colors "
// BELOW LINES TELLS YOUR CLASS NAME "var tile:colors = new colors(); "
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
var first_tile:colors;
var second_tile:colors;
var pause_timer:Timer;
var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8);
for (x=1; x<=4; x++){
for (y=1; y<=4; y++){
var random_card = Math.floor(Math.random()*colordeck.length);
var tile:colors = new colors();
tile.col = colordeck[random_card];
colordeck.splice(random_card,1);
tile.gotoAndStop(9);
tile.x = (x-1)*82;
tile.y = (y-1)*82;
tile.addEventListener(MouseEvent.CLICK,tile_clicked);
addChild(tile)}}
function tile_clicked(event:MouseEvent) {
var clicked:colors = (event.currentTarget as colors);
if (first_tile == null) {
first_tile = clicked;
first_tile.gotoAndStop(clicked.col);
}
else if (second_tile == null && first_tile != clicked) {
second_tile = clicked;
second_tile.gotoAndStop(clicked.col);
if (first_tile.col == second_tile.col) {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
pause_timer.start();
}
else {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
pause_timer.start()}}}
function reset_tiles(event:TimerEvent) {
first_tile.gotoAndStop(9);
second_tile.gotoAndStop(9);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
}
function remove_tiles(event:TimerEvent) {
removeChild(first_tile);
removeChild(second_tile);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
}

----------------------------------------------------------------------------------------------------------------
COLOR MATCHING GAME AS2
http://flashcollege.blogspot.co.uk/2015/02/card-matching-game-as2.html
https://www.youtube.com/watch?v=DScn-sBJen0
------------------------------------------------------------------------------------------------------------------

// INSIDE MOVIE CLIP SCENE 1 FIRST FRAME  BUTTON CODE 

on (press) {
 // send click info to main timeline
 _root.clickCard(this);
}

===============================================================
MAIN TIME LINE FIRST FRAME CODE
===============================================================

initGame();
stop();
function initGame() {
cardsListOrdered = [];
//DIGIT 9 TELLS HOW MANY FRAMES NEED AFTER BUTTON FRAME
for (i=1; i<=9; i++) {
cardsListOrdered.push(i, i);
}
cardsListSorted = [];
while (cardsListOrdered.length>0) {
r = int(Math.random()*cardsListOrdered.length);
cardsListSorted.push(cardsListOrdered[r]);
cardsListOrdered.splice(r, 1)}
x = 0;
y = 0;
//DIGIT 18 MAKES ONE COPIES OF 9 FRAMES
for (i=0; i<18; i++) {
attachMovie("Card", "Card"+i, i);
_root["Card"+i].picture = cardsListSorted[i];
_root["Card"+i]._x = x*60+200;
_root["Card"+i]._y = y*60+50;
x++;
if (x>5) {
x = 0;
y++;
}}
firstclip = 0;
}
function clickCard(clip) {
if (secondclip != 0) {
firstclip.enabled = true;
secondclip.enabled = true;
firstclip.gotoAndStop(1);
secondclip.gotoAndStop(1);
firstclip = 0;
secondclip = 0;
}
if (firstclip == clip) {
firstclip.gotoAndStop(1);
firstclip = 0}
else if (firstclip == 0) {
clip.gotoAndStop(clip.picture+1);
firstclip = clip;
firstclip.enabled = false}
else {
clip.gotoAndStop(clip.picture+1);
secondclip = clip;
secondclip.enabled = false;
if (firstclip.picture == secondclip.picture) {
firstclip.enabled = false;
secondclip.enabled = false;
firstclip = 0;
secondclip = 0;
}}}
buttonInstance.onRelease = function() {
initGame();
}

------------------------------------------------------------------------------------------------------------------
COLOR MATCHING GAME AS3 WITHOUT CLASS


1) DOWNLOAD SOURCE FILE

2) REMOVE package { }
3) REMOVE public class color_match extends Sprite {}
4) public function color_match() {} BECAUSE IF YOU DON'T REMOVE THIS LINE ADD CHID FUNCTION NOT WORK
5) REMOVE public & private WORDS
READ MORE:

-----------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------
CODE:
-----------------------------------------------------------------------------------------------------------------


import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

var first_tile:colors;
var second_tile:colors;
var pause_timer:Timer;
var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8);

//I REMOVE HERE THIS LINE public function color_match() {} BECAUSE IF YOU DON'T REMOVE THIS LINE ADDCHILD FUNCTION NOT WORK

for (x=1; x<=4; x++) {
for (y=1; y<=4; y++) {
var random_card = Math.floor(Math.random()*colordeck.length);
var tile:colors = new colors();
tile.col = colordeck[random_card];
colordeck.splice(random_card,1);
tile.gotoAndStop(9);
tile.x = (x-1)*82;
tile.y = (y-1)*82;
tile.addEventListener(MouseEvent.CLICK,tile_clicked);
addChild(tile);
}
}

function tile_clicked(event:MouseEvent) {
var clicked:colors = (event.currentTarget as colors);
if (first_tile == null) {
first_tile = clicked;
first_tile.gotoAndStop(clicked.col);
}
else if (second_tile == null && first_tile != clicked) {
second_tile = clicked;
second_tile.gotoAndStop(clicked.col);
if (first_tile.col == second_tile.col) {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
pause_timer.start();
}
else {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
pause_timer.start();
}
}
}
function reset_tiles(event:TimerEvent) {
first_tile.gotoAndStop(9);
second_tile.gotoAndStop(9);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
}
function remove_tiles(event:TimerEvent) {
removeChild(first_tile);
removeChild(second_tile);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
}

-----------------------------------------------------------------------------------------------------------------
ADD TWO LINES  IN ABOVE CODE
COLOR MATCHING AS3 GAME WITH REST BUTTON
STEP NO:1
FIRST ADD RESTART FUNCTION LINES
//initGame(); 
//function initGame() {}

STEP NO:2
MAKE REST BUTTON AND GIVE INSTANCE NAME RESET
AND ADD CODE

//MAKE BUTTON GIVE INSTANCE NAME RESET

RESET.addEventListener(MouseEvent.CLICK, RESETO );
function RESETO (e:MouseEvent):void{

initGame();

}
----------------------------------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------------------------------
COLOR MATCHING AS3 GAME WITH REST BUTTON CODE:
----------------------------------------------------------------------------------------------------------------

// FOR RESET BUTTON  ADD TWO LINES initGame(); & function initGame() { LINES

initGame();

function initGame() {

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

var first_tile:colors;
var second_tile:colors;
var pause_timer:Timer;
var colordeck:Array = new Array(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8);

//I REMOVE HERE THIS LINE public function color_match() {} BECAUSE IF YOU DON'T REMOVE THIS LINE ADD CHID FUNCTION NOT WORK

for (x=1; x<=4; x++) {
for (y=1; y<=4; y++) {
var random_card = Math.floor(Math.random()*colordeck.length);
var tile:colors = new colors();
tile.col = colordeck[random_card];
colordeck.splice(random_card,1);
tile.gotoAndStop(9);
tile.x = (x-1)*82;
tile.y = (y-1)*82;
tile.addEventListener(MouseEvent.CLICK,tile_clicked);
addChild(tile);
}
}

function tile_clicked(event:MouseEvent) {
var clicked:colors = (event.currentTarget as colors);
if (first_tile == null) {
first_tile = clicked;
first_tile.gotoAndStop(clicked.col);
}
else if (second_tile == null && first_tile != clicked) {
second_tile = clicked;
second_tile.gotoAndStop(clicked.col);
if (first_tile.col == second_tile.col) {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
pause_timer.start();
}
else {
pause_timer = new Timer(1000,1);
pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
pause_timer.start();
}
}
}
function reset_tiles(event:TimerEvent) {
first_tile.gotoAndStop(9);
second_tile.gotoAndStop(9);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,reset_tiles);
}
function remove_tiles(event:TimerEvent) {
removeChild(first_tile);
removeChild(second_tile);
first_tile = null;
second_tile = null;
pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
}

}

//MAKE BUTTON GIVE INSTANCE NAME RESET
RESET.addEventListener(MouseEvent.CLICK, RESETO );
function RESETO (e:MouseEvent):void{
initGame();
}





EmoticonEmoticon