• 第一个flash游戏配对游戏


    看了《AS3.0游戏编程大学》用里面的素材模仿做了第一个flash游戏。。。

    game

     
    游戏影片剪辑代码,按照AS3.0 JIT编译的说明,把游戏代码放在构造函数里面貌似不是好的做法,效率低,因为JIT编译对构造函数里面的代码不进行动态编译。
    MyGame.as
      1 package 
      2 {
      3     import flash.display.MovieClip;
      4     import flash.events.MouseEvent;
      5     import flash.utils.Timer;
      6     import flash.events.TimerEvent;
      7     import flash.text.TextField;
      8     import flash.events.Event;
      9     import flash.utils.getTimer;
     10     import flash.display.IBitmapDrawable;
     11     import flash.media.SoundChannel;
     12     import flash.net.SharedObject;
     13 
     14 
     15     public class MyGame extends MovieClip
     16     {
     17 
     18         private const boardWidth:uint = 6;
     19         private const boardHeight:uint = 6;
     20         private const cardWidth:Number = 52;
     21         private const cardWHeight:Number = 52;
     22         private const offsetX:Number = 120;
     23         private const offsetY:Number = 45;
     24         private const pointForMatch:int = 100;
     25         private const pointForMiss:int = -5;
     26 
     27 
     28         private var cardArr:Array;
     29         private var firstCard:Card10;
     30         private var secondCard:Card10;
     31         private var cardNumbers:uint = 0;
     32         private var flipBackTimer:Timer;
     33 
     34         private var startTime:uint;
     35         private var gameTime:uint;
     36         private var timeText:TextField;
     37         private var score:int;
     38         private var scoreText:TextField;
     39 
     40         private var firstSound:FirstCardSound=new FirstCardSound();
     41         private var matchSound:MatchSound=new MatchSound();
     42         private var missSound:MissSound=new MissSound();
     43 
     44 
     45 
     46         public function MyGame():void
     47         {
     48             //var highScore:SharedObject = SharedObject.getLocal("userHighScore");
     49             //trace(highScore.data.HS);
     50             //highScore.data.HS = 0;
     51             //highScore.flush();
     52             //so.data.highScore = new Number(1234567890);
     53             //so.flush();
     54 
     55             startTime = getTimer();
     56             gameTime = 0;
     57 
     58             timeText=new TextField();
     59             timeText.x = 400;
     60             addChild(timeText);
     61 
     62             scoreText=new TextField();
     63             scoreText.x = 50;
     64             score = 0;
     65             addChild(scoreText);
     66 
     67 
     68             addEventListener(Event.ENTER_FRAME,showTimeAndScore);
     69 
     70 
     71 
     72             var Arr:Array=new Array();
     73             cardArr=new Array();
     74             for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
     75             {
     76                 Arr.push(i);
     77                 Arr.push(i);
     78             }
     79 
     80             for (i=0; i<boardHeight; i++)
     81             {
     82                 for (var j:uint=0; j<boardWidth; j++)
     83                 {
     84                     var thiscard:Card10=new Card10();
     85                     //thiscard.stop();
     86                     cardArr.push(thiscard);
     87                     thiscard.x = i * cardWidth + offsetX;
     88                     thiscard.y = j * cardWHeight + offsetY;
     89                     var no:uint = Math.floor(Math.random() * Arr.length);
     90                     thiscard.cardFace = Arr[no];
     91                     Arr.splice(no,1);
     92                     thiscard.gotoAndStop(thiscard.cardFace+2);
     93                     //thiscard.gotoAndStop(1);
     94                     thiscard.addEventListener(MouseEvent.CLICK,cardClik);
     95                     addChild(thiscard);
     96                     cardNumbers++;
     97                 }
     98             }
     99 
    100             flipBackTimer = new Timer(2000,1);
    101             flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,allFlipBack);
    102             flipBackTimer.start();
    103 
    104         }
    105 
    106         private function showTimeAndScore(event:Event)
    107         {
    108             gameTime = getTimer() - startTime;
    109             timeText.text = "time:" + clockTime(gameTime);
    110             scoreText.text = "score:" + String(score);
    111 
    112         }
    113 
    114         private function clockTime(time:uint):String
    115         {
    116             var seconds:int = Math.floor(time / 1000);
    117             var minutes:int = Math.floor(seconds / 60);
    118             seconds -=  minutes * 60;
    119             var timeString:String = minutes + ":" + String(seconds + 100).substr(1,2);
    120             return timeString;
    121         }
    122 
    123         private function allFlipBack(event:TimerEvent)
    124         {
    125             for (var i:uint=0; i<cardArr.length; i++)
    126             {
    127                 //if(!this.getChildAt(i) is MovieClip)
    128                 //trace("yes");
    129                 //(Card10)(this.getChildAt(i)).startFlip(1);
    130                 cardArr[i].startFlip(1);
    131 
    132             }
    133 
    134             //trace("t");
    135             flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,allFlipBack);
    136 
    137         }
    138 
    139         private function cardClik(event:MouseEvent)
    140         {
    141             //var highScore:SharedObject = SharedObject.getLocal("userHighScore");
    142             //trace(so.data.highScore);
    143             //var file:FileReference=new FileReference();
    144             //file.save(clockTime(gameTime),"t.txt");
    145             //MovieClip(root).gotoAndStop("GameOver");
    146             var thiscard:Card10=(event.currentTarget as Card10);
    147             //trace(thiscard.cardFace);
    148             if (firstCard == null)
    149             {
    150                 firstCard = thiscard;
    151                 //thiscard.gotoAndStop(thiscard.cardFace+2);
    152                 thiscard.startFlip(thiscard.cardFace+2);
    153                 score +=  pointForMiss;
    154                 PlaySound(firstSound);
    155             }
    156             else if (firstCard==thiscard)
    157             {
    158                 thiscard.startFlip(1);
    159                 firstCard = null;
    160                 PlaySound(firstSound);
    161 
    162 
    163             }
    164             else if (secondCard==null)
    165             {
    166                 secondCard = thiscard;
    167                 //thiscard.gotoAndStop(thiscard.cardFace+2);
    168                 thiscard.startFlip(thiscard.cardFace+2);
    169                 if (firstCard.cardFace == secondCard.cardFace)
    170                 {
    171                     PlaySound(matchSound);
    172                     removeChild(firstCard);
    173                     removeChild(secondCard);
    174                     firstCard = null;
    175                     secondCard = null;
    176                     cardNumbers -=  2;
    177                     if (cardNumbers == 0)
    178                     {
    179                         //trace(cardNumbers);
    180 
    181                         MovieClip(root).Time = clockTime(gameTime);
    182                         MovieClip(root).Score = score;
    183 
    184                         var highScore:SharedObject = SharedObject.getLocal("userHighScore");
    185 
    186                         if (score > highScore.data.HS)
    187                         {
    188                                 highScore.data.HS=score;
    189                                 highScore.flush();
    190                         }
    191                         MovieClip(root).HighScore=highScore.data.HS;
    192                         MovieClip(root).gotoAndStop("GameOver");
    193                     }
    194                     //trace(cardNumbers);
    195                     score +=  pointForMatch;
    196 
    197 
    198                 }
    199                 else
    200                 {
    201                     PlaySound(missSound);
    202                     //flipBackTimer=new Timer(2000,1);
    203                     flipBackTimer.reset();
    204                     flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,FlipBack);
    205                     flipBackTimer.start();
    206                     score +=  pointForMiss;
    207 
    208                 }
    209 
    210             }
    211             else
    212             {
    213                 PlaySound(firstSound);
    214                 //firstCard.gotoAndStop(1);
    215                 //secondCard.gotoAndStop(1);
    216                 firstCard.startFlip(1);
    217                 secondCard.startFlip(1);
    218                 firstCard = thiscard;
    219                 //thiscard.gotoAndStop(thiscard.cardFace+2);
    220                 thiscard.startFlip(thiscard.cardFace+2);
    221                 secondCard = null;
    222                 flipBackTimer.stop();
    223                 score +=  pointForMiss;
    224             }
    225 
    226         }
    227         private function FlipBack(event:TimerEvent)
    228         {
    229             if (firstCard != null && secondCard != null)
    230             {
    231 
    232                 firstCard.startFlip(1);
    233                 secondCard.startFlip(1);
    234                 firstCard = null;
    235                 secondCard = null;
    236 
    237             }
    238             flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,FlipBack);
    239         }
    240 
    241         private function PlaySound(soundObj:Object)
    242         {
    243             var channel:SoundChannel = soundObj.play();
    244         }
    245 
    246 
    247     }
    248 
    249 }

    卡片类代码:

    Card10.as
     1 package  
     2 {
     3     import flash.events.Event;
     4     
     5     public class Card10 extends Card
     6     {
     7 
     8         private var cardface:uint;
     9         private var flipToFrame:uint;
    10         private var Step:uint;
    11         public function Card10():void
    12         {
    13             cardface=0;
    14         }
    15         
    16         public function get cardFace():uint
    17         {
    18             return cardface;
    19         }
    20         public function set cardFace(cf:uint):void
    21         {
    22             cardface=cf;
    23         }
    24         
    25         public function startFlip(toWhichFrame:uint)
    26         {
    27             flipToFrame=toWhichFrame;
    28             Step=10;
    29             this.addEventListener(Event.ENTER_FRAME,flip);
    30         }
    31         
    32         public function flip(event:Event)
    33         {
    34             Step--;
    35             if (Step > 5) 
    36             { // first half of flip
    37                 this.scaleX = .20*(Step-6);
    38             } 
    39             else 
    40             { // second half of flip
    41                 this.scaleX = .20*(5-Step);
    42             }
    43             
    44             if(Step==5)
    45             {
    46                 gotoAndStop(flipToFrame);
    47             }
    48             
    49             if(Step==0)
    50             this.removeEventListener(Event.ENTER_FRAME,flip);
    51         }
    52 
    53     }
    54     
    55 }

    把flash嵌入html代码:

    <title>DEMO</title>
    <head>
    <script type="text/javascript" src="https://files.cnblogs.com/wonderKK/swfobject.js"></script>
    </head>
    <body>
       <form id="Form1">
          <div id="flashcontent">
             <a href="http://www.adobe.com/go/getflashplayer">
                <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /> 
             </a>      
          </div>
       </form>
       <script type="text/javascript">    
           var so = new SWFObject("https://files.cnblogs.com/wonderKK/game.swf", "mymovie", "800", "600", "7", "#FFFFFF");
           so.write("flashcontent");
       </script>
    </body>
  • 相关阅读:
    12
    Kafka设计解析(二)- Kafka High Availability (上)
    Apache kafka 工作原理介绍
    Kafka设计解析(一)- Kafka背景及架构介绍
    【转载】MySQL之权限管理
    【转载】漫谈HADOOP HDFS BALANCER
    【转载】HDFS 上传文件不均衡和Balancer太慢的问题
    【转载】mysql binlog日志自动清理及手动删除
    【转】Typora极简教程
    更新Linux服务器时间
  • 原文地址:https://www.cnblogs.com/wonderKK/p/2524555.html
Copyright © 2020-2023  润新知