- //MARGIN_LEFT:左边距
- var MARGIN_LEFT:Number = 50;
- //MARGIN_TOP:上边距
- var MARGIN_TOP:Number = 50;
- //W:图块边长
- var W:Number = 50;
- //ROW:图块行数
- var ROW:Number = 7;
- //COL:图块列数
- var COL:Number = 7;
- //PIC:图案种数
- var PIC:Number = 6;
- //GRID:矩阵图块数
- var GRID:Number = ROW*COL;
- //XA:第1个图块X坐标
- var XA:Number = -1;
- //YA:第1个图块Y坐标
- var YA:Number = -1;
- //XB:第2个图块X坐标
- var XB:Number = -1;
- //YB:第2个图块Y坐标
- var YB:Number = -1;
- //PICIDX:图片序列号
- var PICIDX:Number = 0;
- //MOVEDELAY:移动延迟
- var MOVEDELAY:Number = 4;
- //DISADELAY:消失延迟
- var DISADELAY:Number = 5;
- //SCORE:得分
- var SCORE:Number = 0;
- //matrix:记录每个图块图案的二维数组
- var matrix:Array = new Array(ROW);
- for (var i = 0; i<ROW; i++) {
- matrix[i] = new Array(COL);
- }
- //调用initGrid()函数初始化游戏
- initGrid();
- function initGrid() {
- //图块序号清0
- PICIDX = 0;
- //计分清0
- SCORE = 0;
- _root.txtScore.text = SCORE;
- //调用initXY()初始化被选图块坐标
- initXY();
- //调用initMatrix()将二维数组清0
- initMatrix();
- //调用randMatrix()生成随机二维数组
- randMatrix();
- //调用loadPic加载图片
- loadPic();
- }
- //函数clearPic()删除所有图块
- function clearPic() {
- for (var i = 0; i<=PICIDX; i++) {
- var mc:MovieClip = _root["pic"+i];
- mc.removeMovieClip();
- }
- }
- //函数initXY()初始化被选图块坐标
- function initXY() {
- XA = -1;
- YA = -1;
- XB = -1;
- YB = -1;
- }
- //函数initMatrix()将二维数组初始化为0
- function initMatrix() {
- for (var i = 0; i<ROW; i++) {
- for (var j = 0; j<COL; j++) {
- matrix[j][i] = 0;
- }
- }
- }
- //函数randMatrix()生成随机二维数组
- function randMatrix() {
- var rand:Number;
- for (var i = 0; i<ROW; i++) {
- for (var j = 0; j<COL; j++) {
- //为二维数组元素matrix[j][i]生成随机图片
- do {
- rand = Math.floor(Math.random()*PIC);
- matrix[j][i] = Number(rand+1);
- } while (checkInitLine(i, j));
- //直到生成的二维数组中不再3个以上的相同图块连在一起
- }
- }
- }
- //函数checkInitLine()在初始化游戏时检查是否有3个以上的相同图块连在一起
- function checkInitLine(X, Y:Number):Boolean {
- var boolX, boolY:Boolean;
- boolX = boolY=false;
- var total:Number = 0;
- if (X>=2) {
- //从当前图块开始向左比较3个图块
- boolX = (matrix[Y][X] == matrix[Y][X-1] && matrix[Y][X] == matrix[Y][X-2]);
- }
- if (Y>=2) {
- //从当前图块开始向上比较3个图块
- boolY = (matrix[Y][X] == matrix[Y-1][X] && matrix[Y][X] == matrix[Y-2][X]);
- }
- return boolX || boolY;
- }
- //函数loadPic()加载图片并为影片剪辑添加事件处理函数
- function loadPic() {
- for (var i = 0; i<ROW; i++) {
- for (var j = 0; j<COL; j++) {
- PICIDX = j*COL+i;
- var picMC:MovieClip = _root.createEmptyMovieClip("pic"+PICIDX, PICIDX);
- picMC._x = MARGIN_LEFT+i*W;
- picMC._y = MARGIN_TOP+j*W;
- picMC.X = i;
- picMC.Y = j;
- //鼠标经过,改变透明度
- picMC.onRollOver = function() {
- this._alpha = 50;
- };
- //鼠标移出,透明度还原
- picMC.onRollOut = function() {
- this._alpha = 100;
- };
- //单击图块时
- picMC.onRelease = function() {
- if (XA == -1 && YA == -1) {
- //未选第1个图块时置当前单击图块为第1个图块
- XA = this.X;
- YA = this.Y;
- } else {
- //已选第1个图块时置当前单击图块为第2个图块
- XB = this.X;
- YB = this.Y;
- var distance:Number = Math.abs(XB-XA)+Math.abs(YB-YA);
- //distance<1:两个图块为同一位置图块,不作处理
- //distance>1:两个图块不相邻,不作处理
- //distance==1:两个图块相邻,交换位置
- if (distance == 1) {
- //调用changePos()交换两个图块的位置
- changePos();
- if (checkMatch()) {
- //有3个以上的连续相同图块,消除之
- while (checkMatch()) {
- }
- //继续检查和消除,直到不再有3个以上的连续相同图块
- } else {
- //没有3个以上的连续相同图块
- XA ^= XB;
- XB ^= XA;
- XA ^= XB;
- YA ^= YB;
- YB ^= YA;
- YA ^= YB;
- //将两个图块交换回原来的位置
- changePos();
- //调用doUnmatchMove()显示动画:两个图块交换位置后回到各自的原来位置
- doUnmatchMove(XA, YA, XB, YB);
- }
- }
- initXY();
- }
- };
- //加载图片
- var innerMC:MovieClip = picMC.createEmptyMovieClip("innerMC", 1);
- innerMC.loadMovie("img/"+matrix[j][i]+".jpg");
- }
- }
- }
- //函数changePos()交换两个图块的位置
- function changePos() {
- matrix[YA][XA] ^= matrix[YB][XB];
- matrix[YB][XB] ^= matrix[YA][XA];
- matrix[YA][XA] ^= matrix[YB][XB];
- var mcA:MovieClip;
- var mcB:MovieClip;
- for (var i in _root) {
- if ((_root[i].X == XA) && (_root[i].Y == YA)) {
- mcA = _root[i];
- }
- if ((_root[i].X == XB) && (_root[i].Y == YB)) {
- mcB = _root[i];
- }
- }
- mcA._x ^= mcB._x;
- mcB._x ^= mcA._x;
- mcA._x ^= mcB._x;
- mcA._y ^= mcB._y;
- mcB._y ^= mcA._y;
- mcA._y ^= mcB._y;
- mcA.X ^= mcB.X;
- mcB.X ^= mcA.X;
- mcA.X ^= mcB.X;
- mcA.Y ^= mcB.Y;
- mcB.Y ^= mcA.Y;
- mcA.Y ^= mcB.Y;
- }
- //函数doUnmatchMove()显示两个图块交换位置并回到各自的原来位置的动画
- function doUnmatchMove(x0, y0, x1, y1) {
- var mc0:MovieClip;
- var mc1:MovieClip;
- for (var i in _root) {
- if ((_root[i].X == x0) && (_root[i].Y == y0)) {
- mc0 = _root[i];
- }
- if ((_root[i].X == x1) && (_root[i].Y == y1)) {
- mc1 = _root[i];
- }
- }
- mc0.MoveFlag = 1;
- mc1.MoveFlag = 1;
- mc0.onEnterFrame = function() {
- if (this.MoveFlag>0) {
- this._x += (x1-x0)/MOVEDELAY*W;
- this._y += (y1-y0)/MOVEDELAY*W;
- this._xscale -= 10;
- this._yscale -= 10;
- this.MoveFlag++;
- if (this.MoveFlag == MOVEDELAY) {
- this.MoveFlag = -1;
- }
- } else if (this.MoveFlag<0) {
- this._x -= (x1-x0)/MOVEDELAY*W;
- this._y -= (y1-y0)/MOVEDELAY*W;
- this._xscale += 10;
- this._yscale += 10;
- this.MoveFlag--;
- if (this.MoveFlag == -MOVEDELAY) {
- this.MoveFlag = 0;
- }
- } else if (this.MoveFlag == 0) {
- this._x = MARGIN_LEFT+x0*W;
- this._y = MARGIN_TOP+y0*W;
- this._xscale = 100;
- this._yscale = 100;
- delete this.onEnterFrame;
- }
- };
- mc1.onEnterFrame = function() {
- if (this.MoveFlag>0) {
- this._x += (x0-x1)/MOVEDELAY*W;
- this._y += (y0-y1)/MOVEDELAY*W;
- this._xscale -= 10;
- this._yscale -= 10;
- this.MoveFlag++;
- if (this.MoveFlag == MOVEDELAY) {
- this.MoveFlag = -1;
- }
- } else if (this.MoveFlag<0) {
- this._x -= (x0-x1)/MOVEDELAY*W;
- this._y -= (y0-y1)/MOVEDELAY*W;
- this._xscale += 10;
- this._yscale += 10;
- this.MoveFlag--;
- if (this.MoveFlag == -MOVEDELAY) {
- this.MoveFlag = 0;
- }
- } else if (this.MoveFlag == 0) {
- this._x = MARGIN_LEFT+x1*W;
- this._y = MARGIN_TOP+y1*W;
- this._xscale = 100;
- this._yscale = 100;
- delete this.onEnterFrame;
- }
- };
- }
- //函数checkMatch()检查图块矩阵中是否有3个以上的相同图块相连
- function checkMatch():Boolean {
- var pic:Number;
- var count:Number;
- //判断X方向上是否有3个以上的相同图块相连
- for (var i = ROW-1; i>=0; i--) {
- pic = -1;
- count = 0;
- //针对每一行进行检查
- for (var j = COL-1; j>=0; j--) {
- if (pic == matrix[i][j] && pic != -1) {
- count++;
- } else {
- pic = matrix[i][j];
- if (count>=3) {
- //调用deleteLine()清除相连图块
- deleteLine("X", j+1, i, count);
- return true;
- }
- count = 1;
- }
- if (j == 0 && count>=3) {
- //调用deleteLine()清除相连图块
- deleteLine("X", j, i, count);
- return true;
- }
- }
- }
- //判断Y方向上是否有3个以上的相同图块相连
- for (var i = COL-1; i>=0; i--) {
- pic = -1;
- count = 0;
- //针对每一列进行检查
- for (var j = ROW-1; j>=0; j--) {
- if (pic == matrix[j][i] && pic != -1) {
- count++;
- } else {
- pic = matrix[j][i];
- if (count>=3) {
- deleteLine("Y", i, j+1, count);
- return true;
- }
- count = 1;
- }
- if (j == 0 && count>=3) {
- deleteLine("Y", i, j, count);
- return true;
- }
- }
- }
- return false;
- }
- //函数deleteLine()删除3个图块以上的连接
- function deleteLine(orient:String, X, Y, L:Number) {
- //orient=="X":在X方向上连接
- if (orient == "X") {
- //清除连通行
- for (var i = 0; i<L; i++) {
- matrix[Y][X+i] = -1;
- for (var j in _root) {
- if ((_root[j].X == X+i) && (_root[j].Y == Y)) {
- //显示消失动画
- doDisapear(_root[j]);
- }
- }
- }
- //连通行以上的图块下落一行
- for (var i = 0; i<L; i++) {
- for (var j in _root) {
- if (_root[j].X == X+i && _root[j].Y<Y) {
- _root[j]._y += W;
- _root[j].Y += 1;
- }
- }
- }
- for (var i = 0; i<L; i++) {
- //连通行以上的图块对应的数组下落一行
- for (var j = Y; j>0; j--) {
- matrix[j][X+i] = matrix[j-1][X+i];
- }
- //最顶行加新图块
- PICIDX++;
- var picMC:MovieClip = _root.createEmptyMovieClip("pic"+PICIDX, PICIDX);
- picMC._x = MARGIN_LEFT+(X+i)*W;
- picMC._y = MARGIN_TOP+0*W;
- picMC.X = X+i;
- picMC.Y = 0;
- picMC.onRollOver = function() {
- this._alpha = 50;
- };
- picMC.onRollOut = function() {
- this._alpha = 100;
- };
- picMC.onRelease = function() {
- if (XA == -1 && YA == -1) {
- XA = this.X;
- YA = this.Y;
- } else {
- XB = this.X;
- YB = this.Y;
- var distance:Number = Math.abs(XB-XA)+Math.abs(YB-YA);
- if (distance == 1) {
- changePos();
- if (checkMatch()) {
- while (checkMatch()) {
- }
- } else {
- XA ^= XB;
- XB ^= XA;
- XA ^= XB;
- YA ^= YB;
- YB ^= YA;
- YA ^= YB;
- changePos();
- doUnmatchMove(XA, YA, XB, YB);
- }
- }
- initXY();
- }
- };
- var rand:Number = Math.floor(Math.random()*PIC);
- matrix[0][X+i] = Number(rand+1);
- var innerMC:MovieClip = picMC.createEmptyMovieClip("innerMC", 1);
- innerMC.loadMovie("img/"+matrix[0][X+i]+".jpg");
- }
- }
- //orient=="Y":在Y方向上连接
- if (orient == "Y") {
- //清除连通列
- for (var i = 0; i<L; i++) {
- matrix[Y+i][X] = -1;
- for (var j in _root) {
- if ((_root[j].X == X) && (_root[j].Y == Y+i)) {
- //显示消失动画
- doDisapear(_root[j]);
- }
- }
- }
- //连通列以上的图块下落L行
- for (var j in _root) {
- if (_root[j].X == X && _root[j].Y<Y) {
- _root[j]._y += L*W;
- _root[j].Y += L;
- }
- }
- //连通行以上的图块对应的数组元素下落一行
- for (var i = Y-1; i>=0; i--) {
- matrix[i+L][X] = matrix[i][X];
- }
- for (var i = L-1; i>=0; i--) {
- PICIDX += 1;
- var picMC:MovieClip = _root.createEmptyMovieClip("pic"+PICIDX, PICIDX);
- picMC._x = MARGIN_LEFT+X*W;
- picMC._y = MARGIN_TOP+i*W;
- picMC.X = X;
- picMC.Y = i;
- picMC.onRollOver = function() {
- this._alpha = 50;
- };
- picMC.onRollOut = function() {
- this._alpha = 100;
- };
- picMC.onRelease = function() {
- if (XA == -1 && YA == -1) {
- XA = this.X;
- YA = this.Y;
- } else {
- XB = this.X;
- YB = this.Y;
- var distance:Number = Math.abs(XB-XA)+Math.abs(YB-YA);
- if (distance == 1) {
- changePos();
- if (checkMatch()) {
- while (checkMatch()) {
- }
- } else {
- XA ^= XB;
- XB ^= XA;
- XA ^= XB;
- YA ^= YB;
- YB ^= YA;
- YA ^= YB;
- changePos();
- doUnmatchMove(XA, YA, XB, YB);
- }
- }
- initXY();
- }
- };
- var rand:Number = Math.floor(Math.random()*PIC);
- matrix[i][X] = Number(rand+1);
- var innerMC:MovieClip = picMC.createEmptyMovieClip("innerMC", 1);
- innerMC.loadMovie("img/"+matrix[i][X]+".jpg");
- }
- }
- }
- //函数doDisapear()显示图块的消失动画
- function doDisapear(obj:MovieClip) {
- delete obj.onRollOver;
- delete obj.onRollOut;
- obj.onEnterFrame = function() {
- obj.swapDepths(_root.getNextHighestDepth()-1);
- this._xscale -= 100/DISADELAY;
- this._yscale -= 100/DISADELAY;
- if (this._xscale<=0) {
- //删除图块
- this.removeMovieClip();
- //得分加1
- SCORE++;
- _root.txtScore.text = SCORE;
- }
- };
- }
- //添加右键菜单
- var myMenu = new ContextMenu();
- myMenu.hideBuiltInItems();
- myMenu.customItems.push(new ContextMenuItem("重新开始", againStart));
- _root.menu = myMenu;
- //函数againStart()重新开始游戏
- function againStart(obj:Object, menu:ContextMenu) {
- //调用clearPic()删除图块
- clearPic();
- //调用initGrid()初始化游戏
- initGrid();
- }
复制代码 flash游戏对对碰源代码 |