θυοιβ

ActionScript3
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Matching

Posted on 2011-05-27 00:46  ichow  阅读(303)  评论(0)    收藏  举报
Program: Flash Language: AS3 Software Used: Flash Professional CS5, FlashDevelop 3.3.4

Demo!

Source

package
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.media.SoundChannel;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author iChow
	 */
	public class Matching extends MovieClip
	{
		//名字
		private static const GAMETITLE:String = ".Matching";
		//宽
		private static const boardWidth:uint = 6;
		//高
		private static const boardHeight:uint = 6;
		//横距离
		private static const cardHorizontalSpacing:Number = 47;
		//竖距离
		private static const cardVerticalSpacing:Number = 57;
		//开始坐标
		private static const boardOffsetX:Number = 42;
		private static const boardOffsetY:Number = 120;
		//得分
		private static const pointsForMatch:int = 100;
		//扣分
		private static const pointsForMiss:int = -5;
		//开始菜单
		private var menu:Sprite;
		private var _startButton:SimpleButton;
		//结束菜单
		private var _pauseMc:PauseFace;
		//分数
		private var currScore:int;
		//Tile个数
		private var tileLeft:int;
		//记录第一二次Tile
		private var firstTile:Tile;
		private var secondTile:Tile;
		//返回计时2000
		private var flipBackTimer:Timer;
		//文本 得分&时间
		private var scoreText:TextField;
		private var timerText:TextField;
		//记录开始时间
		private var startTime:int;
		//当前
		private var gameTime:int;
		//声音
		private var theFirstCardSound:FirstCardSound = new FirstCardSound();
		private var theMissSound:MissSound = new MissSound();
		private var theMatchSound:MatchSound = new MatchSound();

		public function Matching()
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//菜单
			menu = new Sprite();
			var _title:GameTitle = new GameTitle(GAMETITLE);
			_startButton = new StartButton();
			menu.x = stage.stageWidth / 2;
			menu.y = stage.stageHeight / 2;
			_title.y = -150;
			_startButton.y = 20;
			menu.addChild(_title);
			menu.addChild(_startButton);
			addChild(menu);
			//结束
			_pauseMc = new PauseFace();
			//文本
			var _iformat:TextFormat=new TextFormat(GameTitle.TextFont, 14, GameTitle.TextColor);
			scoreText = new TextField();
			timerText = new TextField();
			scoreText.defaultTextFormat = _iformat;
			scoreText.embedFonts = true;
			scoreText.selectable = false;
			timerText.defaultTextFormat = _iformat;
			timerText.embedFonts = true;
			timerText.selectable = false;
			timerText.x = 240;
			//事件
			_startButton.addEventListener(MouseEvent.CLICK, startGame);
		}
		/**
		 * 开始游戏
		 * @param	e
		 */
		private function startGame(e:MouseEvent):void
		{
			trace("start!");
			menu.visible = false;
			currScore = 0;
			showScore();
			startTime = getTimer();
			gameTime = 0;
			addChild(timerText);
			addChild(scoreText);
			showScore();
			addEventListener(Event.ENTER_FRAME, showTime);
			createTitles();
			addEventListener("reStart", reStartGame);
		}
		/**
		 * 更新分数
		 */
		private function showScore():void {
			scoreText.text = "Score: " + String(currScore);
		}
		/**
		 * 更新时间
		 * @param	e
		 */
		private function showTime(e:Event):void
		{
			gameTime = getTimer() - startTime;
			timerText.text = "Time: " + clockTime(gameTime);
		}
		/**
		 * 时间处理
		 * @param	ms
		 * @return
		 */
		private function clockTime(ms:int):String
		{
			var seconds:int = Math.floor(ms / 1000);
			var minutes:int = Math.floor(seconds / 60);
			seconds -= minutes * 60;
			var _time:String = minutes + ":" + String(seconds + 100).substr(1, 2);
			return _time;
		}
		/**
		 * 创建Tile
		 */
		private function createTitles():void
		{
			var tileList:Array = new Array();
			for (var i:uint = 0; i <boardWidth * boardHeight / 2; i++) {
				tileList.push(i);
				tileList.push(i);
			}
			tileLeft = 0;
			for(var x:uint=0;x<boardWidth;x++) {
				for(var y:uint=0;y<boardHeight;y++) {
					var _tile:Tile = new Tile();
					_tile.stop();
					_tile.x = x * cardHorizontalSpacing + boardOffsetX;
					_tile.y = y * cardVerticalSpacing + boardOffsetY;
					var _index:uint = Math.floor(Math.random() * tileList.length);
					_tile.id = tileList[_index];
					tileList.splice(_index, 1);
					_tile.addEventListener(MouseEvent.CLICK, tileClick);
					_tile.buttonMode = true;
					addChild(_tile);
					tileLeft++;
				}
			}

		}
		/**
		 * 点击处理
		 * @param	e
		 */
		private function tileClick(e:MouseEvent):void
		{
			var _tile:Tile = e.target as Tile;
			//第一为空
			if (firstTile == null) {
				//记录&显示
				firstTile = _tile;
				firstTile.startFilp(_tile.id + 2);
				playSound(theFirstCardSound);
				//点同一张
			}else if (firstTile == _tile) {
				//返回
				firstTile.startFilp(1);
				firstTile = null;
				playSound(theMissSound);
				//第二 检测
			}else if (secondTile == null) {
				secondTile = _tile;
				secondTile.startFilp(secondTile.id + 2);
				//答对
				if (firstTile.id == secondTile.id) {
					removeChild(firstTile);
					removeChild(secondTile);
					firstTile = null;
					secondTile = null;
					currScore += pointsForMatch;
					showScore();
					playSound(theMatchSound);
					tileLeft -= 2;
					if (tileLeft == 0) {
						winGame();
					}
				}else {
					//错误
					currScore += pointsForMiss;
					showScore();
					playSound(theMissSound);
					//2s后返回
					flipBackTimer = new Timer(2000, 1);
					flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, returnTiles);
					flipBackTimer.start();
				}
			}else {
				//立即点 先返回之前的
				returnTiles(null);
				playSound(theFirstCardSound);
				//现实当前点击Tile
				firstTile = _tile;
				firstTile.startFilp(firstTile.id + 2);
			}
		}
		/**
		 * 返回处理
		 * @param	e
		 */
		private function returnTiles(e:TimerEvent):void
		{
			if (firstTile != null) firstTile.startFilp(1);
			if (secondTile != null) secondTile.startFilp(1);
			firstTile = null;
			secondTile = null;
			flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, returnTiles);
		}
		/**
		 * 游戏结束
		 */
		private function winGame():void {
			removeEventListener(Event.ENTER_FRAME, showTime);
			_pauseMc.setTipText("GAME OVER", "PLAY ANGIN");
			addChild(_pauseMc);
		}
		/**
		 * 重新开始
		 * @param	e
		 */
		private function reStartGame(e:Event):void {
			removeEventListener("reStart", reStartGame);
			removeChild(_pauseMc);
			menu.visible = true;
		}
		/**
		 * 播放声音
		 * @param	soundObject
		 */
		public function playSound(soundObject:Object) {
			var channel:SoundChannel = soundObject.play();
		}
	}

}
胆大心细脸皮厚