小游戏制作——打地鼠

                                                                                 打地鼠

首先做好UI界面,然后在元件内部做好地鼠动的不见动画(就是地鼠网上出来的动画),最后拔一下代码粘贴到类文件中,即可。

Main 类

package
{
 import flash.display.MovieClip;
 import flash.events.Event;
 import flash.events.EventDispatcher;
 public class MouseMove extends MovieClip
 {
  private var _mc:MovieClip;
  private var Y:Number;
  private var _distance:Number;
  private var FangXiang:Number = -1;
  public function MouseMove(target:MovieClip,distance):void
  {
   _mc = target;
   Y = _mc.y;
   _distance = Y - distance;
  }
  public function startMove():void
  {
   _mc.addEventListener(Event.ENTER_FRAME,moves);
  }
  private function moves(e:Event):void
  {
   var mc:MovieClip = e.target as MovieClip;
   var dis:Number = Math.abs(mc.y - _distance) + 0.2;
   mc.y +=  dis / 4 * FangXiang;
   if (mc.y <= _distance)
   {
    FangXiang = 1;
   }
   else if (FangXiang == 1 && mc.y >= Y)
   {
    mc.y = Y;
    FangXiang = -1;
    mc.removeEventListener(Event.ENTER_FRAME,moves);
    mc.dispatchEvent(new Event("moveover"));
   }
  }
 }
}

MouseMove 类

package

{

 import flash.display.MovieClip;

 import flash.events.Event;

 import flash.events.MouseEvent;

 import flash.events.TimerEvent;

 import flash.utils.Timer;

 import flash.text.TextField;

 public class Main extends MovieClip

 {  

 private var i:uint;  

 private var mc:MovieClip;

  private var score:uint;

  private var timer:Timer;

  private var time:uint;  

 private var totalTimes:uint = 60;

  private var clickState:Boolean;

  private var gameState:Boolean = true;

  private var button:AgainButton;

  public function Main():void

  {  

  stop();

   score_txt.mouseEnabled = false;  

  stage.frameRate = 60;  

  init();

  }  

 private function changes():void   

{   

 if (gameState)  

  {     

    clickState = false;   

     i = uint(Math.random() * 18) + 1;   

     mc = this["mouse" + i + "_mc"].mc;

    var mousemove:MouseMove = new MouseMove(mc,80);

    mousemove.startMove();  

    mc.addEventListener("moveover",OnMoveOver);

    mc.addEventListener(MouseEvent.CLICK,OnPlayHandler);  

    mc.buttonMode = true;

   }  

  else

   {    

    timer.stop();

    gameOver();

   }  

 }  

 private function OnMoveOver(e:Event):void  

 {   

  mc.removeEventListener("moveover",OnMoveOver);  

  mc.removeEventListener(MouseEvent.CLICK,OnPlayHandler);  

  mc.buttonMode = false;

   changes();  

 }

  private function OnPlayHandler(e:Event):void

  {  

  if (clickState == false)  

  {  

   clickState = true;  

   score++;   

  score_txt.text = "得分:" + String(score);  

   mc.play();   

 }  

 }

  private function init():void   

{  

  back.visible = false;  

  timer = new Timer(1000);  

  timer.start();

   timer.addEventListener(TimerEvent.TIMER,OnTimerHandler);  

  time = 0;  

  score = 0;

   score_txt.text =  "得分:" + String(score);  

  time_txt.text = "计时:" + String(time);

   button = new AgainButton();

   button.buttonMode = true;

   changes();

  }

  private function OnTimerHandler(e:TimerEvent):void  

 {   

   time++;

   time_txt.text = "计时:" + String(time);

   if (time >= 60)  

  {    

   gameState = false;  

   gotoAndStop(2);  

  }

  }

  private function gameOver():void  

 {   

 addChild(button);  

  back.visible = true;

   button.x = stage.stageWidth / 2;  

  button.y = stage.stageHeight / 2;  

  button.addEventListener(MouseEvent.CLICK,OnReStartHandler);  

 }

  private function OnReStartHandler(e:MouseEvent):void  

 {  

  gameState = true;   

 removeChild(button);  

  init();

  }

 }

}

 

posted @ 2012-11-09 22:00  泡沫小灰灰  阅读(464)  评论(0编辑  收藏  举报