用代码做五子棋小游戏

今天做了一个五子棋的棋盘,给大家分享一下。

首先连接一个主类Main,再建一个As3.0类文件起名Tools该文件放置在和Main一个文件夹内的com文件夹内的tools文件夹内在Main类中设置如下;

package  {
 
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import com.tools.Tools;
 
 public class Main extends MovieClip
 {
  private var tools:Tools;
  private var box:Array = new Array();
  private var dd:Array = new Array();
  
  private var step:Number = 0;
  private var str:String = 'white';
  
  public function Main()
  {
   tools = new Tools();
   xunHuan();
   addFun();
  }
  private function addFun():void
  {
   //悔棋。
   btn_hui.addEventListener(MouseEvent.CLICK,hui);
   //重新开始
   restart.addEventListener(MouseEvent.CLICK,res);
  }
  private function xunHuan():void
  {
   for (var i:uint = 0; i<=18; i++)
   {
    box[i] = new Array();

在Tools中设置如下:

package  com.tools{
 import flash.display.MovieClip;
 import flash.text.TextField;
 import flash.display.Sprite;
 
 public class Tools extends Sprite
 {

  public function Tools()
  {
   // constructor code
  }
  public function setGoToAndStop(mc:MovieClip,biaoQian:String):void
  {
   mc.gotoAndStop(biaoQian);
  }
  public function setStop(mc:MovieClip):void
  {
   mc.stop();
  }
  public function setButtonMode(mc:MovieClip,boo:Boolean):void
  {
   mc.buttonMode = boo;
  }
  public function setMcXY(mc:MovieClip,NumX:Number,NumY:Number):void
  {
   mc.x = NumX;
   mc.y = NumY;
  }
  public function setText(test:TextField,str:String):void
  {
   test.text = str;
  }
  public function setArrayPush(arr:Array,mc:*):void
  {
   arr.push(mc);
  }

 }
 
}


    for (var j:uint = 0; j<=18; j++)
    {
     box[i][j] = new mc();
     stage.addChild(box[i][j]);
     //box[i][j].stop();
     tools.setStop(box[i][j]);
     //box[i][j].buttonMode = true;
     tools.setButtonMode(box[i][j],true);
     //box[i][j].x = 30 * i;
//     box[i][j].y = 30 * j;
     
     tools.setMcXY(box[i][j], 30 * i,30 * j);
     
     box[i][j].addEventListener(MouseEvent.CLICK,cq);
    }

   }
  }
   //出棋
  private function cq(e:MouseEvent):void
  {
   step++;
   //num.text = String("第"+step+"步");
   
   tools.setText(num ,"第"+step+"步");
   
   
   
   var t = e.currentTarget;
   //t.gotoAndStop(str);
   tools.setGoToAndStop(t,str);
   
   
   str = (str == 'white') ? 'black':'white';
   t.removeEventListener(MouseEvent.CLICK,cq);
   
   //t.buttonMode = false;
   tools.setButtonMode(t,false);
   
   //dd.push(t);
   tools.setArrayPush(dd,t);
   //trace(typeof(dd.push(t)));
   //trace(dd.push(t));
  }
  
  
  private function hui(e:MouseEvent):void
  {
   if (dd.length != 0)
   {
    var tmp = dd.pop();//删除数组最后 一个 元素 ,并返回这个元素。
    tmp.buttonMode = true;
    tmp.gotoAndStop('first');
    tmp.addEventListener(MouseEvent.CLICK,cq);
    step--;
    num.text = '第' + step.toString() + '步';
 
   }
   else
   {
    num.text = "悔到头了";
   }
  }
  
  private function res(e:MouseEvent):void
  {
   for (var i:uint = 0; i<=18; i++)
   {
    for (var j:uint = 0; j<=18; j++)
    {
     //把dd数组清除
     dd = null;
     box[i][j].gotoAndStop("first");
     //转到第一帧。;
     box[i][j].buttonMode = true;//去掉手
     step = 0;//步骤清零
     num.text = "第" + step + "步";//显示第几步
     box[i][j].addEventListener(MouseEvent.CLICK,cq);
     }
   }


  }
 }
 
}

这样一个简单的五子棋棋盘就设计出来了。 欢迎大家多多指导

 

 

 


 

转载于:https://www.cnblogs.com/sunmmbk/archive/2012/09/23/2699256.html

你可能感兴趣的:(用代码做五子棋小游戏)