文档类:
package
{
import com.ui.Card;
import flash.display. MovieClip;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.utils.getTimer;
public class Main extends Sprite
{
private static const boardWidth:uint = 6; // 卡片列数
private static const boardHeight:uint = 5; // 行数
private static const cardHorizontalSpacing: Number = 62; // 卡片水平间隔
private static const cardVerticalSpacing: Number = 62; // 垂直间隔
private static const boardOffsetX: Number = 10; // 卡片左上角横坐标
private static const boardOffsetY: Number = 85; // 卡片左上角纵坐标
private static const pointsForMatch:int = 100; // 游戏分数,匹配+100
private static const pointsForMiss:int = -5; // 游戏分数,不匹配-5
// 两个临时Card变量
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint; // 用于卡片计数,为0则结束游戏
private var gameScore:int; // 分数
private var gameStartTime:uint; // 初始化时间
private var gameTime:uint; // 获取游戏时间
// 声明两文本
private var gameScoreField:TextField;
private var gameTimeField:TextField;
// 卡片自动翻转时间变量
private var flipBackTimer:Timer;
// 声音变量
private var theFirstCardSound:FirstCardSound = new FirstCardSound();
private var theMissSound:MissSound = new MissSound();
private var theMatchSound:MatchSound = new MatchSound();
// 卡片数组
var cardlist:Array;
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.stageWidth = 550;
stage.stageHeight = 400;
init();
}
private function init():void
{
var ym:YingMu = new YingMu();
this.addChild(ym);
// 舞台上生成15对卡片列表的索引值,并存入到数组cardlist中
cardlist = new Array();
for( var i:uint=0; i<(boardWidth*boardHeight) / 2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
// 把30张卡片添加到舞台
cardsLeft = 0;
for( var x:uint=0; x<boardWidth; x++)
{
for( var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card(); // 卡片副本
c.x = x * cardHorizontalSpacing + boardOffsetX; // 卡片在屏幕上的左上角坐标
c.y = y * cardVerticalSpacing +boardOffsetY;
// 得到一个随机卡片索引值
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r]; // 把卡片索引值赋给card
cardlist.splice(r,1); // 从数组中移除卡片
c.buttonMode = true;
this.addChild(c); // 卡片添加到屏幕
cardsLeft++ // 生成卡片数自动+1
// 点击卡片侦听函数
c.addEventListener(MouseEvent.CLICK, clickCard);
}
}
// 设置文本格式
var format:TextFormat = new TextFormat();
format.size = 16;
format.font = "宋体";
format.bold = true;
// 设置游戏分数文本并复制
gameScoreField = new TextField();
this.addChild(gameScoreField);
gameScoreField.defaultTextFormat = format;
gameScoreField.selectable = false;
gameScoreField.width = 200;
gameScoreField.height = 50;
gameScoreField.x = 400;
gameScoreField.y = 110;
gameScore = 0;
// 调用计分函数
showGameScore();
// 设置时钟文本
gameTimeField = new TextField();
this.addChild(gameTimeField);
gameTimeField.defaultTextFormat = format;
gameTimeField.selectable = false;
gameTimeField.width = 200;
gameTimeField.height = 50;
gameTimeField.x = 400;
gameTimeField.y = 85;
gameTimeField.text = String(getTimer());
gameTime = 0;
// 调用计时函数
this.addEventListener(Event.ENTER_FRAME, showTime);
}
// 点击卡片函数
public function clickCard(event:MouseEvent):void
{
// 当第一张牌和第二张牌都翻开时,其余牌点击无效
if(firstCard != null && secondCard != null)
{
return;
}
// 把点击的卡片对象赋值给它的实例
var thisCard:Card = (event.currentTarget as Card);
// 翻开第一张牌
if(firstCard == null)
{
firstCard = thisCard;
// 翻转到对应的牌面上,thisCard.cardface + 2表示所在的哪一帧
// 因为cardlist保存着0-14,Card的第一帧是牌背面,所以0和1帧都无效,所以+2才是第一张牌的正面
thisCard.startFlip(thisCard.cardface + 2);
// 播放翻牌声音
playSound(theFirstCardSound);
}
// 重复翻开第一张牌
else if(firstCard == thisCard)
{
// 第二次点击后,牌翻转回去,参数1表示Card的第一帧
firstCard.startFlip(1);
firstCard = null;
playSound(theMissSound);
}
// 翻开第二张牌
else if(secondCard == null)
{
secondCard = thisCard;
thisCard.startFlip(thisCard.cardface + 2);
// 播放翻牌声音
playSound(theFirstCardSound);
// 开始比较两个卡片
if(firstCard.cardface == secondCard.cardface)
{
showScoreText(secondCard, 0);
// 如果匹配,都移除屏幕
this.removeChild(firstCard);
this.removeChild(secondCard);
// 两个变量值复位
firstCard = null;
secondCard = null;
// 计算分数,匹配+100
gameScore += pointsForMatch;
// 调用显示分数文本函数
showGameScore();
playSound(theMatchSound);
// 若匹配一次,总数量-2
cardsLeft -= 2;
// 通过cardsLeft检测游戏是否结束
if(cardsLeft == 0)
{
trace("恭喜!游戏成功通关!");
trace("--------------------------");
trace("游戏分数:" + gameScore);
trace("游戏时间:" + clockTime(gameTime));
trace("--------------------------");
}
}
// 两个卡片不同
else
{
showScoreText(secondCard, 1);
// 匹配错误,-5分
gameScore += pointsForMiss;
showGameScore();
// 两张不匹配,0.8秒后自动翻转回去
flipBackTimer = new Timer(800,1);
flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, returnCards);
flipBackTimer.start();
}
}
else
{
returnCards( null);
firstCard = thisCard;
firstCard.startFlip(thisCard.cardface + 2);
}
}
// 匹配或不匹配时飘出的得分文字.type =0匹配,1不匹配
public function showScoreText(secondCard:Card, type:uint):void
{
var sTxt: MovieClip;
if(type == 0)
{
sTxt = new FanOk();
}
else if(type == 1)
{
sTxt = new FanEroor();
}
sTxt.x = secondCard.x;
sTxt.y = secondCard.y;
this.addChild(sTxt);
}
// 卡片自动翻转回去函数
public function returnCards(event: String):void
{
firstCard.startFlip(1); // 第1张牌翻回到背面
secondCard.startFlip(1); // 第2张牌翻回到背面
firstCard = null;
secondCard = null;
flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
}
// 显示分数文本函数
public function showGameScore():void
{
gameScoreField.text = "游戏得分:" + String(gameScore);
}
// 显示游戏时间毫秒数函数
public function showTime(event:Event):void
{
gameTime = getTimer() - gameStartTime;
gameTimeField.text = "已用时间:" + clockTime(gameTime);
}
// 时间转换函数
public function clockTime(ms:int): String
{
var seconds:int = Math.floor(ms/1000);
var minutes:int = Math.floor(seconds/60);
seconds -= minutes * 60;
var timeString: String = minutes + ":"+ String(seconds+100).substr(1,2);
return timeString;
}
// 播放声音函数
public function playSound(soundObject:Sound):void
{
soundObject.play();
}
}
}
import com.ui.Card;
import flash.display. MovieClip;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.utils.getTimer;
public class Main extends Sprite
{
private static const boardWidth:uint = 6; // 卡片列数
private static const boardHeight:uint = 5; // 行数
private static const cardHorizontalSpacing: Number = 62; // 卡片水平间隔
private static const cardVerticalSpacing: Number = 62; // 垂直间隔
private static const boardOffsetX: Number = 10; // 卡片左上角横坐标
private static const boardOffsetY: Number = 85; // 卡片左上角纵坐标
private static const pointsForMatch:int = 100; // 游戏分数,匹配+100
private static const pointsForMiss:int = -5; // 游戏分数,不匹配-5
// 两个临时Card变量
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint; // 用于卡片计数,为0则结束游戏
private var gameScore:int; // 分数
private var gameStartTime:uint; // 初始化时间
private var gameTime:uint; // 获取游戏时间
// 声明两文本
private var gameScoreField:TextField;
private var gameTimeField:TextField;
// 卡片自动翻转时间变量
private var flipBackTimer:Timer;
// 声音变量
private var theFirstCardSound:FirstCardSound = new FirstCardSound();
private var theMissSound:MissSound = new MissSound();
private var theMatchSound:MatchSound = new MatchSound();
// 卡片数组
var cardlist:Array;
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.stageWidth = 550;
stage.stageHeight = 400;
init();
}
private function init():void
{
var ym:YingMu = new YingMu();
this.addChild(ym);
// 舞台上生成15对卡片列表的索引值,并存入到数组cardlist中
cardlist = new Array();
for( var i:uint=0; i<(boardWidth*boardHeight) / 2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
// 把30张卡片添加到舞台
cardsLeft = 0;
for( var x:uint=0; x<boardWidth; x++)
{
for( var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card(); // 卡片副本
c.x = x * cardHorizontalSpacing + boardOffsetX; // 卡片在屏幕上的左上角坐标
c.y = y * cardVerticalSpacing +boardOffsetY;
// 得到一个随机卡片索引值
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r]; // 把卡片索引值赋给card
cardlist.splice(r,1); // 从数组中移除卡片
c.buttonMode = true;
this.addChild(c); // 卡片添加到屏幕
cardsLeft++ // 生成卡片数自动+1
// 点击卡片侦听函数
c.addEventListener(MouseEvent.CLICK, clickCard);
}
}
// 设置文本格式
var format:TextFormat = new TextFormat();
format.size = 16;
format.font = "宋体";
format.bold = true;
// 设置游戏分数文本并复制
gameScoreField = new TextField();
this.addChild(gameScoreField);
gameScoreField.defaultTextFormat = format;
gameScoreField.selectable = false;
gameScoreField.width = 200;
gameScoreField.height = 50;
gameScoreField.x = 400;
gameScoreField.y = 110;
gameScore = 0;
// 调用计分函数
showGameScore();
// 设置时钟文本
gameTimeField = new TextField();
this.addChild(gameTimeField);
gameTimeField.defaultTextFormat = format;
gameTimeField.selectable = false;
gameTimeField.width = 200;
gameTimeField.height = 50;
gameTimeField.x = 400;
gameTimeField.y = 85;
gameTimeField.text = String(getTimer());
gameTime = 0;
// 调用计时函数
this.addEventListener(Event.ENTER_FRAME, showTime);
}
// 点击卡片函数
public function clickCard(event:MouseEvent):void
{
// 当第一张牌和第二张牌都翻开时,其余牌点击无效
if(firstCard != null && secondCard != null)
{
return;
}
// 把点击的卡片对象赋值给它的实例
var thisCard:Card = (event.currentTarget as Card);
// 翻开第一张牌
if(firstCard == null)
{
firstCard = thisCard;
// 翻转到对应的牌面上,thisCard.cardface + 2表示所在的哪一帧
// 因为cardlist保存着0-14,Card的第一帧是牌背面,所以0和1帧都无效,所以+2才是第一张牌的正面
thisCard.startFlip(thisCard.cardface + 2);
// 播放翻牌声音
playSound(theFirstCardSound);
}
// 重复翻开第一张牌
else if(firstCard == thisCard)
{
// 第二次点击后,牌翻转回去,参数1表示Card的第一帧
firstCard.startFlip(1);
firstCard = null;
playSound(theMissSound);
}
// 翻开第二张牌
else if(secondCard == null)
{
secondCard = thisCard;
thisCard.startFlip(thisCard.cardface + 2);
// 播放翻牌声音
playSound(theFirstCardSound);
// 开始比较两个卡片
if(firstCard.cardface == secondCard.cardface)
{
showScoreText(secondCard, 0);
// 如果匹配,都移除屏幕
this.removeChild(firstCard);
this.removeChild(secondCard);
// 两个变量值复位
firstCard = null;
secondCard = null;
// 计算分数,匹配+100
gameScore += pointsForMatch;
// 调用显示分数文本函数
showGameScore();
playSound(theMatchSound);
// 若匹配一次,总数量-2
cardsLeft -= 2;
// 通过cardsLeft检测游戏是否结束
if(cardsLeft == 0)
{
trace("恭喜!游戏成功通关!");
trace("--------------------------");
trace("游戏分数:" + gameScore);
trace("游戏时间:" + clockTime(gameTime));
trace("--------------------------");
}
}
// 两个卡片不同
else
{
showScoreText(secondCard, 1);
// 匹配错误,-5分
gameScore += pointsForMiss;
showGameScore();
// 两张不匹配,0.8秒后自动翻转回去
flipBackTimer = new Timer(800,1);
flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, returnCards);
flipBackTimer.start();
}
}
else
{
returnCards( null);
firstCard = thisCard;
firstCard.startFlip(thisCard.cardface + 2);
}
}
// 匹配或不匹配时飘出的得分文字.type =0匹配,1不匹配
public function showScoreText(secondCard:Card, type:uint):void
{
var sTxt: MovieClip;
if(type == 0)
{
sTxt = new FanOk();
}
else if(type == 1)
{
sTxt = new FanEroor();
}
sTxt.x = secondCard.x;
sTxt.y = secondCard.y;
this.addChild(sTxt);
}
// 卡片自动翻转回去函数
public function returnCards(event: String):void
{
firstCard.startFlip(1); // 第1张牌翻回到背面
secondCard.startFlip(1); // 第2张牌翻回到背面
firstCard = null;
secondCard = null;
flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
}
// 显示分数文本函数
public function showGameScore():void
{
gameScoreField.text = "游戏得分:" + String(gameScore);
}
// 显示游戏时间毫秒数函数
public function showTime(event:Event):void
{
gameTime = getTimer() - gameStartTime;
gameTimeField.text = "已用时间:" + clockTime(gameTime);
}
// 时间转换函数
public function clockTime(ms:int): String
{
var seconds:int = Math.floor(ms/1000);
var minutes:int = Math.floor(seconds/60);
seconds -= minutes * 60;
var timeString: String = minutes + ":"+ String(seconds+100).substr(1,2);
return timeString;
}
// 播放声音函数
public function playSound(soundObject:Sound):void
{
soundObject.play();
}
}
}
Card类:
package com.ui
{
import flash.display. MovieClip;
import flash.events.Event;
import flash.text.TextField;
public dynamic class Card extends MovieClip
{
private var flipStep:uint;
private var isFlipping: Boolean = false;
private var flipToFrame:uint;
private var c:Card_FF;
public function Card()
{
c = new Card_FF();
c.stop(); // 停在第一帧
this.addChild(c);
}
public function startFlip(flipToWhichFrame:uint):void
{
isFlipping = true;
flipStep = 10;
flipToFrame = flipToWhichFrame;
this.addEventListener(Event.ENTER_FRAME, flip);
}
// 需要10帧完成卡片翻转
public function flip(event:Event):void
{
flipStep--; // 帧数递减
// 前5帧翻转时放大状态
if(flipStep > 5)
{
this.scaleX = 0.2 * (flipStep - 6);
}
else
{
this.scaleX = 0.2 * (5 - flipStep);
}
c.gotoAndStop(flipToFrame);
// 反转完后移除侦听
if(flipStep == 0)
{
this.removeEventListener(Event.ENTER_FRAME,flip);
}
}
}
{
import flash.display. MovieClip;
import flash.events.Event;
import flash.text.TextField;
public dynamic class Card extends MovieClip
{
private var flipStep:uint;
private var isFlipping: Boolean = false;
private var flipToFrame:uint;
private var c:Card_FF;
public function Card()
{
c = new Card_FF();
c.stop(); // 停在第一帧
this.addChild(c);
}
public function startFlip(flipToWhichFrame:uint):void
{
isFlipping = true;
flipStep = 10;
flipToFrame = flipToWhichFrame;
this.addEventListener(Event.ENTER_FRAME, flip);
}
// 需要10帧完成卡片翻转
public function flip(event:Event):void
{
flipStep--; // 帧数递减
// 前5帧翻转时放大状态
if(flipStep > 5)
{
this.scaleX = 0.2 * (flipStep - 6);
}
else
{
this.scaleX = 0.2 * (5 - flipStep);
}
c.gotoAndStop(flipToFrame);
// 反转完后移除侦听
if(flipStep == 0)
{
this.removeEventListener(Event.ENTER_FRAME,flip);
}
}
}
}