1

我正在创建 Flash“记忆”游戏,想发现 2 张相等的卡片。我需要在窗口顶部添加“计时器”,以计算所有卡片将被发现的秒数。

这是我的代码:

package
{
    import flash.display.MovieClip;
    import Card;
    import Boarder;
    import BlueBoard;
    import flash.events.MouseEvent;
    import RedBoard;
    import Snow;

    public class MemoryGame extends MovieClip
    {
        private var _card:Card;
        private var _boarder:Boarder;
        private var _blueBoard:BlueBoard;
        private var _cardX:Number;
        private var _cardY:Number;
        private var _firstCard:*;
        private var _totalMatches:Number;
        private var _currentMatches:Number;
        private var _redBoard:RedBoard;
        private var _snow:Snow;
        private var _cards:Array;
        public var  _message:String;

        public function MemoryGame()
        {
            _cards = new Array();
            _totalMatches = 4;
            _currentMatches = 0;
            createCards();
        }

        private function createCards():void
        {
            _cardX = 45;
            _cardY = 10;

            for(var i:Number = 0; i < 2; i++)
            {
                _card = new Card();
                addChild(_card);
                _boarder = new Boarder();
                _card.setType(_boarder);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            for(var j:Number = 0; j < 2; j++)
            {
                _card = new Card();
                addChild(_card);
                _blueBoard = new BlueBoard();
                _card.setType(_blueBoard);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            _cardX = 45;
            _cardY = _card.height + 30;

            for(var k:Number = 0; k < 2; k++)
            {
                _card = new Card();
                addChild(_card);
                _redBoard = new RedBoard();
                _card.setType(_redBoard);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            for(var l:Number = 0; l < 2; l++)
            {
                _card = new Card();
                addChild(_card);
                _snow = new Snow();
                _card.setType(_snow);
                _card.x = _cardX;
                _card.y = _cardY;
                _cardX += _card.width + 50;
                _card.addEventListener(MouseEvent.CLICK, checkCards);
                _cards.push(_card);
            }

            randomizeCards(_cards);
        }

        private function checkCards(event:MouseEvent):void
        {
            event.currentTarget.removeEventListener(MouseEvent.CLICK, checkCards);

            if(_firstCard == undefined)
            {
                _firstCard = event.currentTarget;
            }
            else if(String(_firstCard._type) == String(event.currentTarget._type))
            {
                trace("match");
                _message = "match";
                message_txt.text = _message;
                _firstCard = undefined;
                _currentMatches ++;
                if(_currentMatches >= _totalMatches)
                {
                    trace("YOU WIN !!!");
                    _message = "YOU WIN !!!";
                    message_txt.text = _message;
                }
            }
            else
            {
                trace("wrong");
                _message = "wrong";
                message_txt.text = _message;
                _firstCard.gotoAndPlay("flipBack");
                event.currentTarget.gotoAndPlay("flipBack");
                _firstCard.addEventListener(MouseEvent.CLICK, checkCards);
                event.currentTarget.addEventListener(MouseEvent.CLICK, checkCards);
                _firstCard = undefined;             
            }
        }

        private function randomizeCards(cards:Array):void
        {
            var randomCard1:Number;
            var randomCard2:Number;
            var card1X:Number;
            var card1Y:Number;

            for(var i:Number = 0; i < 10; i++)
            {
                randomCard1 = Math.floor(Math.random() * cards.length);
                randomCard2 = Math.floor(Math.random() * cards.length);

                card1X = cards[randomCard1].x;
                card1Y = cards[randomCard1].y;
                cards[randomCard1].x = cards[randomCard2].x;
                cards[randomCard1].y = cards[randomCard2].y
                cards[randomCard2].x = card1X;
                cards[randomCard2].y = card1Y;
            }
        }
    }
}

编辑: 我还有一个问题。当我将此游戏添加到 PHP 中时,如何将用户名和他的时间添加到数据库中?我需要在动作脚本(swf 文件)中编写代码,还是可以稍后在 php 中编写代码?我的意思是在 php 中我可以使用任何方法从 swf 文件中获取时间并将其写入数据库吗?

你可以帮帮我吗?非常感谢。

4

1 回答 1

0

这是一个如何使用Timer类 (flash.utils.Timer) 来完成您所要求的示例:

    var timer:Timer; //import flash.utils.Timer;
    var txtTime:TextField;
    var tmpTime:Number;  //this will store the time when the game is started    

//your constructor:
public function MemoryGame()
{
    timer = new Timer(1000); //create a new timer that ticks every second.
    timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick

    txtTime = new TextField();
    addChild(txtTime);

    tmpTime = flash.utils.getTimer();
    timer.start(); //start the timer
    //....the rest of your code
}

private function tick(e:Event):void {
    txtTime.text = showTimePassed(flash.utils.getTimer() - tmpTime);
}

//this function will format your time like a stopwatch
function showTimePassed(startTime:int):String {
  var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
  var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds

  var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
  var miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing, 


  if (miliseconds < 10) { //if less than two digits, add a leading 0
    leadingZeroMS = "0";
  }

  var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds

  if (seconds < 10) { //if seconds are less than two digits, add the leading zero
    leadingZeroS = "0";
  }

  var minutes = Math.floor( (time / (60 * 1000) ) ); //60 seconds times 1000 miliseocnds gets the minutes
  return minutes + ":" + leadingZeroS + seconds + "." + leadingZeroMS + miliseconds;
}


//in your you-win block of code:
var score = flash.utils.getTimer() - tmpTime; //this store how many milliseconds it took them to complete the game.

这将创建一个每秒计时的计时器,并使用当前经过的秒数更新文本字段


根据您请求的第二部分,这是您可以执行此操作的一种方法(数据库工作需要在 PHP 中完成,这向您展示了如何将数据发送到 php 页面)

        var urlLoader:URLLoader = new URLLoader();
        urlLoader.addEventListener(Event.COMPLETE, scoreSaveResponse,false,0,true);
        var request:URLRequest = new URLRequest("http://mysite.com/score.php");
        var urlVars:URLVariables = new URLVariables();
            urlVars.time = flash.utils.getTimer() - tmpTime;
            urlVars.userName = "yourUserName";
            //add any other parameters you want to pass to your PHP page
        request.method = URLRequestMethod.POST;
        urlLoader.data = urlVars;
        urlLoader.load(request);

function scoreSaveResponse(e:Event):void {
    //whatever you return from the php page is found in urlLoader.data
}
于 2013-04-30T21:32:38.927 回答