全屏观看地址:
视频1地址
视频2地址
视频3地址
源文件及 PPT 下载:
http://www.fs2you.com/files/76b3bea8-41d3-11dd-ba6f-001143e7b41c/
代码公布(数字版):
Card.as:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.filters.*;
import flash.events.MouseEvent;
public class Card extends Sprite {
public var id:int;
public var W:Number = 30;
public var H:Number = 40;
public var i:int;
public var j:int;
public var TargetCard:Card;
private var color:uint = 0xcccccc;
public var Path:Array = new Array();
private var pressed:Boolean = false;
public var line:Sprite = new Sprite();
public function Card(id:int) {
this.id = id;
init();
}
function init():void {
graphics.beginFill(color);
graphics.drawRect(0,0,W,H);
graphics.endFill();
var Text:TextField = new TextField();
Text.text = String(id);
Text.selectable = false;
Text.autoSize = "left";
var tf:TextFormat = new TextFormat(null,25);
// tf.size = 25;
Text.setTextFormat(tf);
addChild(Text);
addEventListener(MouseEvent.MOUSE_OVER, MouseOver);
addEventListener(MouseEvent.MOUSE_OUT, MouseOut);
}
function setIndex(a:int, b:int):void {
i = a;
j = b;
}
function Pressed():void {
pressed = true;
}
function UnPressed():void {
pressed = false;
this.filters = null;
}
function MouseOver(evt:MouseEvent):void {
var blur:BevelFilter = new BevelFilter();
var shadow:DropShadowFilter = new DropShadowFilter(10);
var f:Array = new Array();
f.push(blur);
f.push(shadow);
this.filters = f;
}
function MouseOut(evt:MouseEvent):void {
if (pressed == false) {
this.filters = null;
}
}
}
}
============================================
Main.as
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
public class Main extends Sprite {
var Row:uint = 4;
var Colum:uint = 5;
var MarginLeft:Number = 180;
var MarginTop:Number = 100;
var oldCard:Card;
var map:Array;
public function Main() {
init();
}
function init():void {
map = new Array(Row + 2);
var numArray:Array = new Array();
for (var i:uint = 0; i < Row; i++) {
numArray[i] = new Array();
var num:uint = 0;
for (var j:uint = 0; j < Colum; j++) {
numArray[i][j] = ++num;
}
}
for (i = 0; i < Row; i++) {
for (j = 0; j < Colum; j++) {
var Rani:uint = Math.floor(Math.random() * Row);
var Ranj:uint = Math.floor(Math.random() * Colum);
var temp:uint = numArray[i][j];
numArray[i][j] = numArray[Rani][Ranj];
numArray[Rani][Ranj] = temp;
}
}
for (i = 0; i < Row + 2; i++) {
map[i] = new Array(Colum + 2);
for (j = 0; j < Colum + 2; j++) {
if (i ==0 || j ==0 || i == Row + 1 || j == Colum + 1) {
map[i][j] = 0;
} else {
map[i][j] = 1;
}
}
}
for (i = 0; i < Row; i++) {
for (j = 0; j < Colum; j++) {
var card:Card = new Card(numArray[i][j]);
addChild(card);
card.x = MarginLeft + j * (card.W + 2);
card.y = MarginTop + i * (card.H + 2);
card.setIndex(i + 1, j + 1);
card.addEventListener(MouseEvent.CLICK, onClick);
}
}
}
function onClick(evt:MouseEvent):void {
try {
var currentCard:Card = Card(evt.target.parent);
} catch (error:Error) {
currentCard = Card(evt.target);
}
if (oldCard == null) {
oldCard = currentCard;
oldCard.Pressed();
} else {
oldCard.TargetCard = currentCard;
// successfully matched!
if (isMatched()) {
addChild(oldCard.line)
oldCard.line.graphics.clear();
oldCard.line.graphics.lineStyle(2);
var node:Object = oldCard.Path.shift();
oldCard.line.graphics.moveTo(MarginLeft + node.y * oldCard.W - oldCard.W / 2, MarginTop + node.x * oldCard.H - oldCard.H / 2);
oldCard.addEventListener(Event.ENTER_FRAME, ToLink);
oldCard = null;
} else {
// Can not Match!
oldCard.UnPressed();
oldCard = null;
}
}
}
function ToLink(evt:Event):void {
var card:Card = Card(evt.target);
if (card.Path.length > 0) {
var node:Object = card.Path.shift();
card.line.graphics.lineTo(MarginLeft + node.y * card.W - card.W / 2, MarginTop + node.x * card.H - card.H / 2);
} else {
map[card.i][card.j] = 0;
map[card.TargetCard.i][card.TargetCard.j] = 0;
card.removeEventListener(Event.ENTER_FRAME, ToLink);
removeChild(card);
removeChild(card.line);
removeChild(card.TargetCard);
}
}
function isMatched():Boolean {
if (oldCard == oldCard.TargetCard || oldCard.id != oldCard.TargetCard.id) {
return false;
}
var x1:uint = oldCard.i;
var y1:uint = oldCard.j;
var x2:uint = oldCard.TargetCard.i;
var y2:uint = oldCard.TargetCard.j;
var node:Object = new Object();
var tempPath:Array = new Array();
for (var i:uint = 0; i < Colum + 2; i++) {
var count:uint = 0;
tempPath.splice(0);
var step:int = (y1 > i) ? -1 : 1;
for (var j = y1; j != i; j += step) {
count += map[x1][j];
node = {x:x1,y:j};
tempPath.push(node);
}
step = (x1 > x2) ? -1 : 1;
for (j = x1; j != x2; j += step) {
count += map[j][i];
node = {x:j,y:i};
tempPath.push(node);
}
step = (i < y2) ? 1 : -1;
for (j = i; j != y2; j+= step) {
count += map[x2][j];
node = {x:x2,y:j};
tempPath.push(node);
}
if (count == 1) {
if (oldCard.Path.length == 0 || tempPath.length < oldCard.Path.length) {
oldCard.Path = tempPath.slice();
}
}
}
for (i = 0; i < Row + 2; i++) {
count = 0;
tempPath.splice(0);
step = (i < x1) ? -1 : 1;
for (j = x1; j != i; j += step) {
count += map[j][y1];
node = {x:j,y:y1};
tempPath.push(node);
}
step = (y2 > y1) ? 1 : -1;
for (j = y1; j != y2; j += step) {
count += map[i][j];
node = {x:i,y:j};
tempPath.push(node);
}
step = (x2 > i) ? 1 : -1;
for (j = i; j != x2; j += step) {
count += map[j][y2];
node = {x:j,y:y2};
tempPath.push(node);
}
if (count == 1) {
if (oldCard.Path.length == 0 || tempPath.length < oldCard.Path.length) {
oldCard.Path = tempPath.slice();
}
}
}
if (oldCard.Path.length > 0) {
node = {x:x1, y:y1};
oldCard.Path.unshift(node);
node = {x:x2, y:y2};
oldCard.Path.push(node);
return true;
}
return false;
}
}
}