您查询的关键词是:flash拼图游戏源码 
下面是原始网址 https://www.csdn.net/gather_27/OtDaAgysMjE5LWJsb2cO0O0O.html 在 2020-06-03 09:10:49 的快照。

如果想更新或删除快照,请点击快照删除了解更多。360搜索与该网页作者无关,不对其内容负责。

【flash拼图游戏源码as3】-博文推荐-CSDN博客

flash拼图游戏源码as3

2013-06-29 21:33:00 weixin_34175509 阅读数 90

话说这种效果在课件里面经常用到,鼠标拖动事件,一个运用很频繁的事件,一起学习学习吧
首先SWF贡献给大家看看效果


感觉咋样,原理其实还蛮简单的,做做试试吧

下面来看看源码吧

  1. package

  2. {

  3. import flash.display.MovieClip;

  4. import flash.display.Sprite;

  5. import flash.events.Event;

  6. import flash.events.MouseEvent;


  7. public class Main extends MovieClip

  8. {

  9. //声明一个mc变量当临时的容器

  10. private var mc;

  11. //声明一个数组用来存储拖动元件的初始坐标

  12. private var arr:Array = new Array();

  13. //声明一个num数字,用来判断所有图形是否都已经对好了

  14. private var num:uint = 0;

  15. public function Main()

  16. {

  17. //for为所有拖动的对象添加鼠标手型和鼠标按下事件

  18. for (var i:int = 0; i < 4; i++) 
  19. {

  20. //为所有对象添加鼠标手型

  21. this["mc" + i].buttonMode = true;

  22. //为所有对象添加按下事件

  23. this["mc" + i].addEventListener(MouseEvent.MOUSE_DOWN, downHandler);

  24. //http://www.shengshiyouxi.com把所有对象的初始坐标存储进数组里面

  25. arr.push(this["mc" + i].x, this["mc" + i].y);

  26. }

  27. //归位按钮在开始时候隐藏

  28. gw_btn.visible = false;

  29. }

  30. //鼠标按下的方法

  31. private function downHandler(e:MouseEvent):void 

  32. //临时容器mc赋值为当前按下的对象

  33. mc = e.currentTarget;

  34. //开始拖动mc

  35. mc.startDrag();

  36. //把mc对象移动到最上层,这样就不会出现拖动的对象处于未拖动对象的下方了

  37. addChild(mc);

  38. //用stage监听鼠标抬起事件 
  39. stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);

  40. }

  41. //stage鼠标抬起的方法

  42. private function upHandler(e:MouseEvent):void 
  43. {

  44. //mc对象停止拖动

  45. mc.stopDrag(); 
  46. //移除stage的鼠标抬起监听事件

  47. stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);

  48. //声明uint类型的变量n并赋值为mc对象的名字的第2位往后面的数值,这一步是为了取值被对图的对象的编号

  49. var n:uint = uint(String(mc.name.substr(2))); 
  50. //if语句判断,如果mc碰撞到了对图对象mc_i时执行

  51. if (mc.hitTestObject(this["mc_" + n])) 
  52. {

  53. //mc隐藏

  54. mc.visible = false;

  55. //被碰撞到的mc_i对象停止播放在第二帧上(第二帧就是对图对象的完整界面)

  56. this["mc_" + n].gotoAndStop(2);

  57. //判断的值num加加,用来判断拼图拼对了几次

  58. num++;

  59. }

  60. //如果没有碰撞到mc_i对象

  61. else 
  62. {

  63. //mc的x、y坐标等于数组里面存储的初始坐标,为啥会乘以2呢,你可以自己研究一下

  64. mc.x = arr[n*2];

  65. mc.y = arr[n*2+1]; 
  66. }

  67. //清空mc对象

  68. mc = null; 
  69. //if语句判断,如果num等于4(有四个被拼图的对象,所以这里的判断值为4)

  70. if (num == 4)

  71. {

  72. //归位按钮gw_btn显示

  73. gw_btn.visible = true;

  74. //为归位按钮添加按钮点击事件

  75. gw_btn.addEventListener(MouseEvent.CLICK, clickHandler);


  76. }

  77. //归位按钮的鼠标点击事件

  78. private function clickHandler(e:MouseEvent):void 
  79. {

  80. //当前点击的对象隐藏,也就是归位按钮隐藏

  81. e.currentTarget.visible = false;

  82. //num判断的数值归0,以便于再次判断拼图拼对了几个

  83. num = 0;

  84. //for为mci对象添加隐藏属性为true,x、y坐标为初始坐标,被拼图的对象停止播放在第一帧上面

  85. for (var i:int = 0; i < 4; i++) 
  86. {

  87. this["mc" + i].visible = true;

  88. this["mc" + i].x = arr[i*2];

  89. this["mc" + i].y = arr[i * 2 + 1];

  90. this["mc_" + i].gotoAndStop(1);

  91. }

  92. }

  93. }

  94. }
复制代码

最后送上我的祝福:祝大家

早日成为大神

2013-04-18 20:44:12 wang3436989 阅读数 2131

刚想学as制作游戏,所以打算先写个贪食蛇小游戏练习下。。大笑大笑


说明:使用的开发工具为 flash builder4.6
首先创建一个as项目。


然后我们先来添加一个config as类将游戏的所有设置都存放在该类下方便更改设置。



以下config类里面的一些基本设置:

好了,接下来我们就来画网 地图。


接下来就是画一个一个圆来代表蛇或者食物,用不同颜色来区分食物以及蛇头,蛇身。

我们先来创建一个Snake类代码如下:



首先要使Snake类继承Sprite,然后构造函数添加color,x,y 3个参数来描述颜色以及初始坐标。

接下来就是画出圆圈:

完整的snake类代码如下:


为什么要把画圆圈的方法提取出来呢?原因就是假如你中途要改变颜色,那么就需要重新画一次,那么这个时候就需要提供一个重新画图的方法了。

好了,我们在地图上添加蛇头看看效果,在snakeGame的构造函数中添加以下代码如图所示


首先前2句代码是把舞台也就是我们可以画画的画板设置为全屏的,

然后就创建地图,创建蛇头,最后把它添加到舞台上。

我们看一下运行效果。


接下来我们来创建食物,首先添加个全局变量

privatevarfood:Snake;

再添加一个方法,如下


这样便可以在网 地图内随机添加食物。效果图如下:(绿色的是食物)



那接下来要做什么呢?当然就是让蛇动了,我们加个定时器让蛇动起来,

我们先在SnakeGane类中定义多一个定时器全局变量timer

privatevartimer:Timer;

然后import flash.events.*;这个包

添加timer使其周期性地引发事件

 

添加代码后如下


然后再添加3个全局变量

privatevargoX:Number=Config.MAP_GRID_WIDTH;

privatevargoY:Number=0;

privatevarsnakeList:Array    = [];

前2个变量用来确认蛇的运动方向,默认刚开始是是往右动。

当goX为负的时候是往左运动,goY为负时是往上。

最后一个是用来存放蛇的节点数组。

SnakeMove方法添加代码如下


好了运行后,发现蛇头不会动,什么原因呢,原因就在我们刚在构造函数里面没有把蛇头添加到存放蛇的数组里面,我们在构造函数里面添加多一句snakeList.push(snakeHead);并且启动计时器

添加后构造函数整体代码如下:



现在运行一下是不是发现会动了呢。哈哈大笑

突然发现写这种图片多的文章还真麻烦,CSDN的编辑器图片大小竟然没办法直接拖动大小的太不给力了,唉,下班了,明天再继续。



2017-06-08 11:31:41 u011623030 阅读数 971

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

2008-07-15 18:02:52 wwj256 阅读数 47

在上篇说到,拼图游戏做到一半不知道怎么做也不想再做了,但前几天看到一些方法,知道了下一步该怎么做,而最近AS3又不知怎么学了,于是就想把它完成,当作练习。

这个程序写得该是我最简洁的一个(我没说很简洁。。),以前我写的都是乱七八糟的一堆东西,有效果就行了,这次我很注意代码的简洁性了,还加了注释。。前所未有。

源代码:

together.as:

package bang{
 import flash.display.Bitmap
 import flash.display.BitmapData
 import flash.display.Sprite
 import flash.display.Loader
 import flash.display.LoaderInfo
 import flash.net.URLRequest
 import flash.events.MouseEvent
 import flash.events.Event
 import flash.display.Stage
 import flash.geom.Matrix
 import flash.text.TextField
 import flash.geom.Rectangle
 import flash.geom.Point 
 public class together extends Sprite{  
  var loader:Loader
  var ul:String    //URL地址
  var picURL:URLRequest
  var n:uint     //块数=n*n
  var picBD:BitmapData  //整张图的BitmapData
  var picWidth:Number   //图宽
  var picHeight:Number  //图高
  var mySprite:Sprite   //放置块及图的容器
  var massWidth:Number  //块宽
  var massHeight:Number  //块高
  var leaveNum:uint   //剩余块数 用于判断完成
  var picx:Number    //图X坐标
  var picy:Number    //图Y坐标
  public function together(){
   }
  public function loaderPic(){
   picURL=new URLRequest(ul)
   loader=new Loader()
   loader.load(picURL)
   loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete)
  }
  public function onComplete(e:Event){
   mySprite=new Sprite()
   //获得比例<
   var xScale=loader.width/(stage.stageWidth-150)
   var yScale=loader.height/stage.stageHeight
   var picScale:Number   //缩放比例
   if(xScale<yScale){
    picScale=1/yScale
   }else{
    picScale=1/xScale
   }
   if(xScale<1&&yScale<1){
    picScale=1
   }
   //获得比例>   
   picWidth=loader.width*picScale   //缩放后的宽度
   picHeight=loader.height*picScale  //缩放后的高度
   var matrix:Matrix=new Matrix()   //缩放Matrix
   picBD=new BitmapData(picWidth,picHeight)  //整张图的BitmapData
   var bitmap:Bitmap=new Bitmap(picBD)    //添加到场景的整张图
   matrix.scale(picScale,picScale)
   picBD.draw(loader.content,matrix,null,null,null,true)
   bitmap.alpha=0.3
   mySprite.addChild(bitmap)
   picx=Math.round(stage.stageWidth-150-picWidth)/2  //图X坐标
   picy=Math.round(stage.stageHeight-picHeight)/2   //图Y坐标
   mySprite.x=picx
   mySprite.y=picy
   stage.addChild(mySprite)
   copyPixel()
  }
  public function copyPixel(){
   massWidth=Math.round(picWidth/n)   //块宽,round是让空隙出现在边缘
   massHeight=Math.round(picHeight/n)   //块高,同上
   leaveNum=n*n        //剩下的块数
   var massBD:BitmapData
   var massBitmap:Bitmap
   var massSprite:Sprite      //块置放的容器
   for(var i=0;i<n;i++){
    for(var j=0;j<n;j++){
     massBD=new BitmapData(massWidth,massHeight)
     massBD.copyPixels(picBD,new Rectangle(i*massWidth,j*massHeight,massWidth,massHeight),new Point(0,0))
     massBitmap=new Bitmap(massBD)
     massSprite=new Sprite()
     massSprite.x=stage.stageWidth-150+Math.random()*100-picx
     massSprite.y=(stage.stageHeight-200)*Math.random()-picy
     massSprite.addEventListener(MouseEvent.MOUSE_DOWN,massDown)
     massSprite.addEventListener(MouseEvent.MOUSE_UP,massUp)
     massSprite.name="MS"+i+j
     massSprite.addChild(massBitmap)
     mySprite.addChild(massSprite)
    }
   }
   function massDown(e:MouseEvent){
    e.target.startDrag()
    mySprite.addChild(Sprite(e.target))    //让拖动的图片显示在最上面
   }
   function massUp(e:MouseEvent){
    e.target.stopDrag()
    var pici=Number(e.target.name.charAt(2))
    var picj=Number(e.target.name.charAt(3))
    if(Math.abs(e.target.x-pici*massWidth)<=20&&Math.abs(e.target.y-picj*massHeight)<=20){  //粘合
    e.target.removeEventListener(MouseEvent.MOUSE_UP,massUp)
    e.target.removeEventListener(MouseEvent.MOUSE_DOWN,massDown)
    e.target.x=pici*massWidth
    e.target.y=picj*massHeight
   //判断拼图是否完成<
    leaveNum--
    if(leaveNum<=0){
     var end_txt:TextField=new TextField()
     end_txt.text="拼图完成"
     end_txt.x=stage.stageWidth-100
     end_txt.y=stage.stageHeight/2
     stage.addChild(end_txt)
    }
   //判断拼图是否完成>
  }
   }
  }
 }
 
}

loadMC.as:

package bang{
 import flash.display.MovieClip
 import flash.events.MouseEvent
 import flash.text.TextField
 import bang.together
 public class loadMC extends MovieClip{
  public function loadMC(){
   this.load_btn.buttonMode=true
   this.load_btn.addEventListener(MouseEvent.MOUSE_DOWN,onMcDown)
   var s:together=new together()
   function onMcDown(e:MouseEvent){
   //删除上一张图片的内容<
   try{
    while(s.mySprite.numChildren>0){
     s.mySprite.removeChildAt(0)
    }
   }
   catch(e){
   }
   //删除上一张图片的内容>   
   var inputURL:String=e.target.parent.url_txt.text
   var inputN:uint=uint(e.target.parent.n_txt.text)
   s.ul=inputURL
   s.n=inputN
   s.loaderPic()
   stage.addChild(s)
   }
  }
 }
}

 
[AS3]拼图游戏 - 贫嘴的鱼 - 贫嘴的鱼的博客together.swf (3.81 KB)
//似乎会因为FLASH设置的问题无法访问图片,看不到效果的话就下载下面的看吧。
[AS3]拼图游戏 - 贫嘴的鱼 - 贫嘴的鱼的博客拼图.rar (95.32 KB)  
 
附:解决FLASH PLAYER安全性问题:
打开C:\Documents and Settings\Administrator\Application Data\Macromedia\Flash Player(我计算机用户名为Administrator,记得把隐藏文件夹打开)

用记事本打开这个目录下的FlashPlayerTrust文件夹下的myTrustFiles.cfg文件(没有就创建一个)允许哪个路径下的 SWF文件可以与internet通信的就把路径写上。。最简便的方法当然是把你机器里所有的盘都给他写上...保存关闭。OK了

2013-06-02 23:17:45 lisiben 阅读数 399


package {
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.geom.*;
import flash.utils.Timer;

public class JigsawPuzzle extends MovieClip {
// number of pieces
const numPiecesHoriz:int = 3;
const numPiecesVert:int = 3;

// size of pieces
var pieceWidth:Number;
var pieceHeight:Number;

// game pieces
var puzzleObjects:Array;

// two levels of sprites
var selectedPieces:Sprite;
var otherPieces:Sprite;

// pieces being dragged
var beingDragged:Array = new Array();

// load picture and set up sprites
public function startJigsawPuzzle() {
// load the bitmap
loadBitmap("jigsawimage.jpg");

// set up two sprites
otherPieces = new Sprite();
selectedPieces = new Sprite();
addChild(otherPieces);
addChild(selectedPieces); // selected on top
}

// get the bitmap from an external source
public function loadBitmap(bitmapFile:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var request:URLRequest = new URLRequest(bitmapFile);
loader.load(request);
}

// bitmap done loading, cut into pieces
private function loadingDone(event:Event):void {
// create new image to hold loaded bitmap
var image:Bitmap = Bitmap(event.target.loader.content);
pieceWidth = Math.floor((image.width/numPiecesHoriz)/10)*10;
pieceHeight = Math.floor((image.height/numPiecesVert)/10)*10;

// place loaded bitmap in image
var bitmapData:BitmapData = image.bitmapData;

// cut into puzzle pieces
makePuzzlePieces(bitmapData);

// set up movement and mouse up events
addEventListener(Event.ENTER_FRAME,movePieces);
stage.addEventListener(MouseEvent.MOUSE_UP,liftMouseUp);
}

// cut bitmap into pieces
private function makePuzzlePieces(bitmapData:BitmapData) {
puzzleObjects = new Array();
for(var x:uint=0;x<numPiecesHoriz;x++) {
for (var y:uint=0;y<numPiecesVert;y++) {
// create new puzzle piece bitmap and sprite
var newPuzzlePieceBitmap:Bitmap = new Bitmap(new BitmapData(pieceWidth,pieceHeight));
newPuzzlePieceBitmap.bitmapData.copyPixels(bitmapData,new Rectangle(x*pieceWidth,y*pieceHeight,pieceWidth,pieceHeight),new Point(0,0));
var newPuzzlePiece:Sprite = new Sprite();
newPuzzlePiece.addChild(newPuzzlePieceBitmap);

// place in bottom sprite
otherPieces.addChild(newPuzzlePiece);

// create object to store in array
var newPuzzleObject:Object = new Object();
newPuzzleObject.loc = new Point(x,y); // location in puzzle
newPuzzleObject.dragOffset = null; // offset from cursor
newPuzzleObject.piece = newPuzzlePiece;
newPuzzlePiece.addEventListener(MouseEvent.MOUSE_DOWN,clickPuzzlePiece);
puzzleObjects.push(newPuzzleObject);
}
}

// randomize locations of pieces
shufflePieces();

}

// random locations for the pieces
public function shufflePieces() {
// pick random x and y
for(var i in puzzleObjects) {
puzzleObjects[i].piece.x = Math.random()*400+50;
puzzleObjects[i].piece.y = Math.random()*250+50;
}
// lock all pieces to 10x10 grid
lockPiecesToGrid();
}

public function clickPuzzlePiece(event:MouseEvent) {
// click location
var clickLoc:Point = new Point(event.stageX, event.stageY);

beingDragged = new Array();

// find piece clicked
for(var i in puzzleObjects) {
if (puzzleObjects[i].piece == event.currentTarget) { // this is it
// add to drag list
beingDragged.push(puzzleObjects[i]);
// get offset from cursor
puzzleObjects[i].dragOffset = new Point(clickLoc.x - puzzleObjects[i].piece.x, clickLoc.y - puzzleObjects[i].piece.y);
// move from bottom sprite to top one
selectedPieces.addChild(puzzleObjects[i].piece);
// find other pieces locked to this one
findLockedPieces(i,clickLoc);
break;
}
}
}

// move all selected pieces according to mouse location
public function movePieces(event:Event) {
for (var i in beingDragged) {
beingDragged[i].piece.x = mouseX - beingDragged[i].dragOffset.x;
beingDragged[i].piece.y = mouseY - beingDragged[i].dragOffset.y;
}
}

// find pieces that should move together
public function findLockedPieces(clickedPiece:uint, clickLoc:Point) {
// get list of puzzle objects sorted by distance to the clicked object
var sortedObjects:Array = new Array();
for (var i in puzzleObjects) {
if (i == clickedPiece) continue;
sortedObjects.push({dist: Point.distance(puzzleObjects[clickedPiece].loc,puzzleObjects[i].loc), num: i});
}
sortedObjects.sortOn("dist",Array.DESCENDING);

// loop until all linked piece found
do {
var oneLinkFound:Boolean = false;
// look at each object, starting with closest
for(i=sortedObjects.length-1;i>=0;i--) {
var n:uint = sortedObjects[i].num; // actual object number
// get the position relative to the clicked object
var diffX:int = puzzleObjects[n].loc.x - puzzleObjects[clickedPiece].loc.x;
var diffY:int = puzzleObjects[n].loc.y - puzzleObjects[clickedPiece].loc.y;
// see if this object is appropriately placed to be locked to the clicked one
if (puzzleObjects[n].piece.x == (puzzleObjects[clickedPiece].piece.x + pieceWidth*diffX)) {
if (puzzleObjects[n].piece.y == (puzzleObjects[clickedPiece].piece.y + pieceHeight*diffY)) {
// see if this object is adjacent to one already selected
if (isConnected(puzzleObjects[n])) {
// add to selection list and set offset
beingDragged.push(puzzleObjects[n]);
puzzleObjects[n].dragOffset = new Point(clickLoc.x - puzzleObjects[n].piece.x, clickLoc.y - puzzleObjects[n].piece.y);
// move to top sprite
selectedPieces.addChild(puzzleObjects[n].piece);
// link found, remove from array
oneLinkFound = true;
sortedObjects.splice(i,1);
}
}
}
}
} while (oneLinkFound);
}

// takes an object and determines if it is directly next to one already selected
public function isConnected(newPuzzleObject:Object):Boolean {
for(var i in beingDragged) {
var horizDist:int = Math.abs(newPuzzleObject.loc.x - beingDragged[i].loc.x);
var vertDist:int = Math.abs(newPuzzleObject.loc.y - beingDragged[i].loc.y);
if ((horizDist == 1) && (vertDist == 0)) return true;
if ((horizDist == 0) && (vertDist == 1)) return true;
}
return false;
}

// stage sends mouse up event, drag is over
public function liftMouseUp(event:MouseEvent) {
// lock all pieces back to grid
lockPiecesToGrid();
// move pieces back to bottom sprite
for(var i in beingDragged) {
otherPieces.addChild(beingDragged[i].piece);
}
// clear drag array
beingDragged = new Array();

// see if the game is over
if (puzzleTogether()) {
cleanUpJigsaw();
gotoAndStop("gameover");
}
}

// take all pieces and lock them to the nearest 10x10 location
public function lockPiecesToGrid() {
for(var i in puzzleObjects) {
puzzleObjects[i].piece.x = 10*Math.round(puzzleObjects[i].piece.x/10);
puzzleObjects[i].piece.y = 10*Math.round(puzzleObjects[i].piece.y/10);
}
}

public function puzzleTogether():Boolean {
for(var i:uint=1;i<puzzleObjects.length;i++) {
// get the position relative to the first object
var diffX:int = puzzleObjects[i].loc.x - puzzleObjects[0].loc.x;
var diffY:int = puzzleObjects[i].loc.y - puzzleObjects[0].loc.y;
// see if this object is appropriately placed to be locked to the first one
if (puzzleObjects[i].piece.x != (puzzleObjects[0].piece.x + pieceWidth*diffX)) return false;
if (puzzleObjects[i].piece.y != (puzzleObjects[0].piece.y + pieceHeight*diffY)) return false
}
return true;
}

public function cleanUpJigsaw() {
removeChild(selectedPieces);
removeChild(otherPieces);
selectedPieces = null;
otherPieces = null;
puzzleObjects = null;
beingDragged = null;
removeEventListener(Event.ENTER_FRAME,movePieces);
stage.removeEventListener(MouseEvent.MOUSE_UP,liftMouseUp);
}

}
}