Flash AS3 学习5 - 全代码拼图游戏

package {
 import flash.display.Sprite;
 import com.zjj.RectShape;
 import flash.events.MouseEvent;
 import flash.sensors.Accelerometer;
 
 public class Main extends Sprite{
  private var indexShape:Array = new Array(1,2,3,4,5,6,7,8);
  private var map:Array = new Array(new Array(3),new Array(3),new Array(3));
  private var arrangement:Array = new Array(new Array(0,0),new Array(40,0),new Array(80,0),new Array(0,40),new Array(40,40),new Array(80,40),new Array(0,80),new Array(40,80),new Array(80,80));
  public function Main(){
   init();
  }
  public function init():void{
   var kuang:Sprite = new Sprite();
   kuang.graphics.lineStyle(3,0xff0000);
   kuang.graphics.drawRect(0,0,120,120);
   kuang.graphics.endFill();
   kuang.x = 215;
   kuang.y = 140;
   addChild(kuang);
   
   for(var i:uint = 0;i<3;i++){
    for(var t:uint = 0;t<3;t++){
     map[i][t] = 1;
    }
   }
   for(var n:uint = 1;n<=8;n++){
    var no:uint = Math.floor(Math.random()*indexShape.length);
    var number:uint = indexShape[no];
    indexShape.splice(no,1);
    var rect:RectShape = new RectShape(number);
    rect.mouseChildren = false;
    var weizhi:uint = Math.floor(Math.random()*arrangement.length);
    var arr:Array = arrangement[weizhi];
    arrangement.splice(weizhi,1);
    rect.x = arr[0];
    rect.y = arr[1];
    rect.X = arr[0]/40;
    rect.Y = arr[1]/40;
    kuang.addChild(rect);
    map[arr[0]/40][arr[1]/40] = 0;
    //添加鼠标点击事件
    rect.addEventListener(MouseEvent.CLICK,dong);
   }
  }
  private function dong(evt:MouseEvent):void{
   var left:uint = evt.target.X-1;
   var right:uint = evt.target.X+1;
   var up:uint = evt.target.Y-1;
   var down:uint = evt.target.Y+1;
   if(left != 4294967295 && map[left][evt.target.Y] == 1){
    map[evt.target.X][evt.target.Y] = 1;
    map[left][evt.target.Y] = 0;
    evt.target.X = left;
    evt.target.x -= 40;
   }else if(right != 3 && map[right][evt.target.Y] == 1){
     map[evt.target.X][evt.target.Y] = 1;
     map[right][evt.target.Y] = 0;
     evt.target.X = right;
     evt.target.x += 40;
   }else if(up != 4294967295 && map[evt.target.X][up] == 1){
     map[evt.target.X][evt.target.Y] = 1;
     map[evt.target.X][up] = 0;
     evt.target.Y = up;
     evt.target.y -= 40;
   }else if(down != 3 && map[evt.target.X][down] == 1){
     map[evt.target.X][evt.target.Y] = 1;
     map[evt.target.X][down] = 0;
     evt.target.Y = down;
     evt.target.y += 40;
   }
  }
 }
}

 

 

package com.zjj{
 import flash.display.Sprite;
 import flash.text.TextField;
 import flash.text.TextFieldAutoSize;
 import flash.text.TextFormat;
 
 public class RectShape extends Sprite{
  private var x_zb:uint;
  private var y_zb:uint;
  
  public function RectShape(indexs:uint){
   init(indexs);
  }
  
  private function init(index:uint):void {
   this.graphics.beginFill(0x00ff00);
   this.graphics.lineStyle(1,0x000000,1);
   this.graphics.drawRect(0,0,40,40);
   this.graphics.endFill();
   var txt:TextFormat = new TextFormat();
   txt.size = 30;
   var num_txt:TextField = new TextField();
   num_txt.selectable = false; //属性是否能被选择
   num_txt.text = String(index);
   num_txt.width = 40;
   num_txt.height = 40;
   num_txt.x = 0;
   num_txt.y = 0;
   num_txt.setTextFormat(txt);
   num_txt.autoSize = TextFieldAutoSize.CENTER;
   this.addChild(num_txt);
  }
  public function get X():uint{
   return x_zb;
  }
  public function set X(n:uint):void{
   x_zb = n;
  }
  public function get Y():uint{
   return y_zb;
  }
  public function set Y(n:uint):void{
   y_zb = n;
  }
 }
}

 

 

更多详情请点击  http://blog.sina.com.cn/zhaojianjunzjj

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页