配对游戏的分析



package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.getTimer;
    
    public class MatchingGameObject9 extends MovieClip {
        // game constants
        private static const boardWidth:uint = 6;
        private static const boardHeight:uint = 6;
        private static const cardHorizontalSpacing:Number = 52;
        private static const cardVerticalSpacing:Number = 52;
        private static const boardOffsetX:Number = 120;
        private static const boardOffsetY:Number = 45;
        private static const pointsForMatch:int = 100;
        private static const pointsForMiss:int = -5;
        
        private var firstCard:Card;
        private var secondCard:Card;
        private var cardsLeft:uint;
        private var gameScore:int;
        private var gameStartTime:uint;
        private var gameTime:uint;
        
        private var gameScoreField:TextField;
        private var gameTimeField:TextField;
        
        public function MatchingGameObject9():void {
            // make a list of card numbers
            var cardlist:Array = new Array();
            for(var i:uint=0;i<boardWidth*boardHeight/2;i++) { 产生卡的数字:00 11 22 33. 1717 共18对
                cardlist.push(i);
                cardlist.push(i);
            }
            
            // create all the cards, position them, and assign a randomcard face to each
            cardsLeft = 0;
            for(var x:uint=0;x<boardWidth;x++) { // horizontal  在舞台放置6X6的影片剪辑
                for(var y:uint=0;y<boardHeight;y++) { // vertical
                    var c:Card = new Card(); // copy the movie clip
                    c.stop(); // stop on first frame
                    c.x = x*cardHorizontalSpacing+boardOffsetX; // set position  新的card x坐标120+52*i
                    c.y = y*cardVerticalSpacing+boardOffsetY;
                    var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face 产生该mc对应的数字
                    c.cardface = cardlist[r]; // assign face to card
                    cardlist.splice(r,1); // remove face from list
                    c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
                    addChild(c); // show the card
                    cardsLeft++;
                }
            }
            
            gameScoreField = new TextField();
            addChild(gameScoreField);
            gameScore = 0;
            showGameScore();
            
            gameTimeField = new TextField();
            gameTimeField.x = 450;
            addChild(gameTimeField);
            gameStartTime = getTimer();
            gameTime = 0;
            addEventListener(Event.ENTER_FRAME,showTime);
        }
        
        // player clicked on a card
        public function clickCard(event:MouseEvent) {
            var thisCard:Card = (event.target as Card); // what card?
            
            if (firstCard == null) { // first card in a pair
                firstCard = thisCard; // note it
                firstCard.gotoAndStop(thisCard.cardface+2); // turn it over
                
            } else if (firstCard == thisCard) { // clicked first card again
                firstCard.gotoAndStop(1); // turn back over
                firstCard = null;
                
            } else if (secondCard == null) { // second card in a pair
                secondCard = thisCard; // note it
                secondCard.gotoAndStop(thisCard.cardface+2); // turn it over
                
                // compare two cards
                if (firstCard.cardface == secondCard.cardface) {
                    // remove a match
                    removeChild(firstCard);
                    removeChild(secondCard);
                    // reset selection
                    firstCard = null;
                    secondCard = null;
                    // add points
                    gameScore += pointsForMatch;
                    showGameScore();
                    // check for game over
                    cardsLeft -= 2; // 2 less cards
                    if (cardsLeft == 0) {
                        MovieClip(root).gameScore = gameScore;
                        MovieClip(root).gameTime = clockTime(gameTime);
                        MovieClip(root).gotoAndStop("gameover");
                    }
                } else {
                    gameScore += pointsForMiss;
                    showGameScore();
                }
                
            } else { // starting to pick another pair
                // reset previous pair
                firstCard.gotoAndStop(1);
                secondCard.gotoAndStop(1);
                secondCard = null;
                // select first card in next pair
                firstCard = thisCard;
                firstCard.gotoAndStop(thisCard.cardface+2);
            }
        }
            
        public function showGameScore() {
            gameScoreField.text = "Score: "+String(gameScore);
        }
        
        public function showTime(event:Event) {
            gameTime = getTimer()-gameStartTime;
            gameTimeField.text = "Time: "+clockTime(gameTime);
        }
        
        public function clockTime(ms:int) {
            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;
        }
    }
}

package {
    import flash.display.*;
    import flash.events.*;
    翻牌效果,翻牌时,绕着y轴旋转
    public dynamic class Card10 extends MovieClip {
        private var flipStep:uint;
        private var isFlipping:Boolean = false;
        private var flipToFrame:uint;
        
        // begin the flip, remember which frame to jump to
        public function startFlip(flipToWhichFrame:uint) {
            isFlipping = true;
            flipStep = 10;
            flipToFrame = flipToWhichFrame;
            this.addEventListener(Event.ENTER_FRAME, flip);
        }
        
        // take 10 steps to flip
        public function flip(event:Event) {
            flipStep--; // next step
            
            if (flipStep > 5) { // first half of flip
                this.scaleX = .20*(flipStep-6);
            } else { // second half of flip
                this.scaleX = .20*(5-flipStep);
            }
            
            // when it is the middle of the flip, go to new frame
            if (flipStep == 5) {
                gotoAndStop(flipToFrame);
            }
            
            // at the end of the flip, stop the animation
            if (flipStep == 0) {
                this.removeEventListener(Event.ENTER_FRAME, flip);
            }
        }
    }
}
        
       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值