flash as3 连连看小游戏实现方法

www.111cn.net 更新:2011-04-22 编辑:kp12345 来源:转载

建fla文件.取名随便(我取的是testllk.fla)
在fla里新建影片剪辑(取名随便 我取的是mov_tupian),在里面添加自己喜欢的图片,每帧一张,对齐原点.在第一帧写上stop();把连接类改为tupian.
在fla里新建影片剪辑(取名随便 我取的是line-mov),在里面添加一个框,和刚才的影片剪辑里的图片一样大小.;把连接类改为kuan.
在文当类里写testllkan.

llkan.as代码

package {
/**
* 连连看算法
*
* @author huangxianchang
* @version1.0
* @date090329
*/
import flash.geom.point;
public class llkan {
//地图数组
private var m_map:array;
/**
* 连连看算法
*/
public function llkan(map_arr :array) {
this.m_map = map_arr;
}
//====================================
//properties
//====================================
/**
* 地图数组
*/
public function set map(map_arr : array):void {
this.m_map = map_arr;
}
//====================================
//public methods
//====================================
/**
* @param p_startx起点x坐标
* @param p_starty起点y坐标
* @param p_endx终点x坐标
* @param p_endy终点y坐标
*
* @return 找到的路径(数组 : [p_startx, p_starty], ... , [p_endx, p_endy])
*/
public function find(p_startx : int, p_starty : int, p_endx : int, p_endy : int):array {
if (isblock(p_startx,p_starty)||isblock(p_endx,p_endy)) {
return null;//超出边界或者为0返回空;
}
//trace(this.m_map[p_starty][p_startx]+"----"+ this.m_map[p_endy][p_endx])
if (this.m_map[p_starty][p_startx]== this.m_map[p_endy][p_endx]) {//2个点内容相同才检测
//返回的4个点
var point1oint = new point(p_startx,p_starty);
var point4oint = new point(p_endx,p_endy);
var point2oint;
var point3oint;
//trace([point1,point4]);
//保存这2个点的内容,并把它设为0表示可通过
var dian1:int=this.m_map[p_starty][p_startx];
var dian2:int=this.m_map[p_endy][p_endx];
this.m_map[p_starty][p_startx]=0;
this.m_map[p_endy][p_endx]=0;
var i:int,j:int,linnum:int;
var shucanxiao:boolean = false;//竖可连不?
var hengcanxiao:boolean = false;//行可连不?
//对以下形状做判断
// | |
// | |
// |----------|
// | |
// | |
var one_blank_shu_arr:array = new array();
var one_blank_shu_arr1:array = new array();
var one_blank_shu_arr2:array= new array();
//starty
one_blank_shu_arr1.push(p_starty);
linnum = p_starty+1;
while ( this.chaoshu(linnum) && this.m_map[linnum][p_startx]==0) {
one_blank_shu_arr1.push(linnum);
linnum++;
}
linnum = p_starty-1;
while ( this.chaoshu(linnum) && this.m_map[linnum][p_startx]==0) {
one_blank_shu_arr1.push(linnum);
linnum--;
}
//endy
one_blank_shu_arr2.push(p_endy);
linnum = p_endy+1;
while ( this.chaoshu(linnum) && this.m_map[linnum][p_endx]==0) {
one_blank_shu_arr2.push(linnum);
linnum++;
}
linnum = p_endy-1;
while ( this.chaoshu(linnum) && this.m_map[linnum][p_endx]==0) {
one_blank_shu_arr2.push(linnum);
linnum--;
}
var on_blank_shu_num:int=0;
//找出2个数组相同的,也就是同一行的y坐标.
var one_blank_shu_arr1_len:int=one_blank_shu_arr1.length;
var one_blank_shu_arr2_len:int=one_blank_shu_arr2.length;
for (i=0; i<one_blank_shu_arr1_len; i++) {
for (j=0; j<one_blank_shu_arr2_len; j++) {
if (one_blank_shu_arr1 == one_blank_shu_arr2[j]) {
one_blank_shu_arr[on_blank_shu_num] = one_blank_shu_arr1;
on_blank_shu_num++;
break;
}
}
}
//看看可连不
var one_blank_shu_len:int=one_blank_shu_arr.length;
for (i=0; i<one_blank_shu_len; i++) {
var xlinnum1:int = math.min(p_startx, p_endx);
var xlinnum2:int = math.max(p_startx, p_endx);
var xlincannum:int = 0;
point2= new point(p_startx,one_blank_shu_arr);
point3= new point(p_endx,one_blank_shu_arr);
//取出在竖相同2点的行比较
var shu_y:int=one_blank_shu_arr;
//行是否可连通
for (var k:int=xlinnum1; k<=xlinnum2; k++) {
if (this.m_map[shu_y][k]==0) {
xlincannum++;
} else {
break;
}
}
if (xlincannum == (xlinnum2-xlinnum1)+1) {
shucanxiao = true;//竖可连
break;
}
}
//竖结束
//shucanxiao = false;
if (shucanxiao) {
//竖可连
} else {
//竖不可连,对行判断
//对以下形状做判断
// ---------------
// |
// |
// ----------------
var one_blank_heng_arr:array = new array();
var one_blank_heng_arr1:array = new array();
var one_blank_heng_arr2:array = new array();
//startx
one_blank_heng_arr1.push(p_startx);
linnum = p_startx+1;
while ( this.chaohan(linnum) && this.m_map[p_starty][linnum]==0) {
one_blank_heng_arr1.push(linnum);
linnum++;
}
linnum = p_startx-1;
while ( this.chaohan(linnum) && this.m_map[p_starty][linnum]==0) {
one_blank_heng_arr1.push(linnum);
linnum--;
}
//endx
one_blank_heng_arr2.push(p_endx);
linnum = p_endx+1;
while ( this.chaohan(linnum) && this.m_map[p_endy][linnum]==0) {
one_blank_heng_arr2.push(linnum);
linnum++;
}
linnum = p_endx-1;
while ( this.chaohan(linnum) && this.m_map[p_endy][linnum]==0) {
one_blank_heng_arr2.push(linnum);
linnum--;
}
var on_blank_heng_num:int = 0;
var one_blank_heng_arr1_len:int=one_blank_heng_arr1.length;
var one_blank_heng_arr2_len:int=one_blank_heng_arr2.length;
//找出2个数组相同的,也就是同一竖的x坐标.
for (i=0; i<one_blank_heng_arr1_len; i++) {
for (j=0; j<one_blank_heng_arr2_len; j++) {
if (one_blank_heng_arr1 == one_blank_heng_arr2[j]) {
one_blank_heng_arr[on_blank_heng_num] = one_blank_heng_arr1;
on_blank_heng_num++;
break;
}
}
}
//看看可连不
var one_blank_heng_len:int=one_blank_heng_arr.length;
for (i=0; i<one_blank_heng_len; i++) {
var ylinnum1:int = math.min(p_starty, p_endy);
var ylinnum2:int = math.max(p_starty, p_endy);
var ylincannum:int = 0;
point2 = new point(one_blank_heng_arr,p_starty);
point3 = new point(one_blank_heng_arr,p_endy);
var shu_x:int=one_blank_heng_arr;
for (k=ylinnum1; k<=ylinnum2; k++) {
if (this.m_map[k][shu_x]==0) {
ylincannum++;
} else {
break;
}
}
if (ylincannum == (ylinnum2-ylinnum1)+1) {
hengcanxiao = true;
break;
}
}
}
//行结束
//还原被刚才设置的点
this.m_map[p_starty][p_startx]=dian1;
this.m_map[p_endy][p_endx]=dian2;
if (hengcanxiao || shucanxiao) {
var arr:array=[point1,point2,point3,point4];
return arr;
}
}
return null;
}
//====================================
//private methods
//====================================
private function isblock( p_endx : int, p_endy : int):boolean {//是否超出边界
var mapheight: int = this.m_map.length;//y
var mapwidth : int = this.m_map[0].length;//x
if (p_endx < 0 || p_endx >= mapwidth || p_endy < 0 || p_endy >= mapheight) {
trace("chaochu~~~~");
return true;//超出边界
}
if (this.m_map[p_endy][p_endx]==0||this.m_map[p_endy][p_endx]==null) {
trace("wei 0~~~~");
return true;
//为0或没有不做比较
}
return false;//没有超出边界
}
private function chaoshu( p_endy : int):boolean {//是否超出竖的边界
var mapheight: int = this.m_map.length;
if (p_endy < 0 || p_endy >= mapheight) {
return false;//超出
}
return true;//未超出
}
private function chaohan( p_endx : int):boolean {//是否超出行的边界
var mapwidth : int = this.m_map[0].length;
if (p_endx < 0 || p_endx >= mapwidth) {
return false;//超出
}
return true;//未超出
}
}
}


testllkan.as代码:

package {
import flash.display.sprite;
import flash.display.stagealign;
import flash.display.stagescalemode;
import flash.events.event;
import flash.events.mouseevent;
import flash.geom.point;
import flash.utils.gettimer;
import llkan;
import flash.text.textfield;
import flash.text.textfieldautosize;
import flash.utils.timer;
import flash.events.timerevent;
/**
* 连连看测试类
*
* @author huangxianchang
* @version1.0
* @date090213
*/
public class testllkan extends sprite {
//====================================
//member variables
//====================================
private var scalexy=1.0;//定义缩放;
private var llk_leng=12;//连连看图片种类数本例总21
private var m_player :kuan;
private var m_map : array=new array();//连连看地图
private var m_llkan : llkan;


private var mytimer:timer;
private var child:sprite;
private var oldpiontoint;
private var canclick:boolean=false;
private var m_mapw : int = 14;//几行x
private var m_maph : int = 9;//几列y
private var sel_num:uint=0;
private var m_mapx : int = 20;//起始位置
private var m_mapy : int = 40;//起始位置
private var xstep:number;
private var ystep:number;
private var m_m_clograte : number = 0.3;
private var m_path : array;//返回路径
private var m_outtxt : textfield;
//====================================
//constructor
//====================================
public function testllkan() {
stage.framerate=24;//帧频
stage.align = stagealign.top_left;
stage.scalemode = stagescalemode.no_scale;
stage.doubleclickenabled = true;//双击
stage.addeventlistener(mouseevent.double_click, resethandle);

this.m_outtxt = new textfield();
addchild(this.m_outtxt);
with (this.m_outtxt) {
x = y = 10;
selectable = false;
autosize = textfieldautosize.left;
}
this.m_llkan = new llkan(m_map);
this.reset();
}
//====================================
//private methods
//====================================
private function reset():void {
var t:tupian = new tupian();
t.scalex=t.scaley=scalexy;
xstep= t.width+3;
ystep= t.height+3;//得到宽和高
t=null;
var all_num:uint = m_mapw*m_maph/2;
var stoparr = new array();//初始化数组
for (var loc:uint=0; loc<m_mapw*m_maph; loc++) {
if (loc<=all_num-1) {
stoparr[loc] = uint(math.random()*llk_leng)+1;
} else {
stoparr[loc] = stoparr[loc-all_num];
}
}
this.luan(stoparr);//打乱数组
var nn:uint=0;
//要算旁边的 所以加0和m_maph+1
for (var i : uint = 0; i <= m_maph+1; i++) {//y
this.m_map = new array();
for (var j : uint = 0; j <= m_mapw+1; j++) {//x
if (i == 0 || j == 0 || i == m_maph+1 || j == m_mapw+1) {
m_map[j]=0;//旁边为0,表示没有东西.可通过
} else {
var tile:tupian = new tupian();
tile.addeventlistener(mouseevent.click, clickhandle);
tile.x = m_mapx + (j-1) * xstep;
tile.y = m_mapy + (i-1) * ystep;
tile.scalex=tile.scaley=this.scalexy;
addchild(tile);
tile.name=i+"-"+j;//y-x
var temp:uint=stoparr[nn];
m_map[j] = temp;
tile.gotoandstop(temp);
//trace(tile.currentframe)
nn++;
}

}
}
m_player= new kuan();
addchild(m_player);
m_player.scalex=m_player.scaley=this.scalexy;
m_player.x=-1000;
m_player.y=-1000;
sel_num=0;
oldpiont=null;
canclick=true;
output("双击空白区域重排游戏 ");
}
private function getxy(p_s oint)oint {//换算成真正的坐标
var p_x:int =m_mapx+xstep*(p_s.x-0.5);
var p_y:int =m_mapy+ystep*(p_s.y-0.5);
return new point(p_x,p_y);
}
private function output(p_info : string):void {
this.m_outtxt.htmltext = "<b><font color='#0000ff' size='16'>连连看示例</font></b>ttt" + p_info;
}
//====================================
//event handles
//====================================
private function luan(luan_arr:array):void {//打乱数组
var luan_arr_len:uint=luan_arr.length;
for (var luan_arr_i:uint=0; luan_arr_i<luan_arr_len; luan_arr_i++) {
var luan_lin_num:uint = uint(math.random()*luan_arr_len);
var luna_lin_arr_num:uint = luan_arr[luan_lin_num];
luan_arr[luan_lin_num] = luan_arr[luan_arr_i];
luan_arr[luan_arr_i] = luna_lin_arr_num;
}
}
private function resethandle(event : mouseevent):void {
this.removechild(m_player);
while (this.numchildren > 1) {
var tile : tupian = this.getchildat(1) as tupian;
if (tile.haseventlistener(mouseevent.click)) {
tile.removeeventlistener(mouseevent.click, clickhandle);
}
this.removechild(tile);
}
this.reset();
}
private function getpoint(p_s :string)oint {
var p_x:int =p_s.split("-")[1];
var p_y:int =p_s.split("-")[0];
return new point(p_x,p_y);
}
private function clickhandle(event : mouseevent):void {
if (canclick) {
var findpiont : point =getpoint(event.target.name);
m_player.x=event.target.x;
m_player.y=event.target.y;
if (sel_num == 0) {
oldpiont= findpiont;
sel_num = 1;
} else if (sel_num == 1) {
sel_num = 2;
canclick = false;
var kaishi:int=gettimer();
this.m_path = this.m_llkan.find(oldpiont.x, oldpiont.y, findpiont.x, findpiont.y);
trace(this.m_path);
var yongshi:int=gettimer()-kaishi;
if (this.m_path != null && this.m_path.length > 0) {
var name1:*=getchildbyname(string(oldpiont.y+"-"+oldpiont.x));
var name2:*=getchildbyname(string(findpiont.y+"-"+findpiont.x));
name1.removeeventlistener(mouseevent.click, clickhandle);
name2.removeeventlistener(mouseevent.click, clickhandle);
removechild(name1);
removechild(name2);
name1=null;
name2=null;
this.m_map[oldpiont.y][oldpiont.x]=0;
this.m_map[findpiont.y][findpiont.x]=0;
output("可以连通,正在画线 用时:"+yongshi);
huaxian();
//this.addeventlistener(event.enter_frame, enterframehandle);
} else {
output("无法连通 用时:"+yongshi);
canclick = true;
sel_num = 0;
}
m_player.x=-1000;
m_player.y=-1000;
}
}

}
private function timerhandler(event:timerevent):void {
{
if (this.m_path == null || this.m_path.length == 0) {
output("连连看,双击空白区域重排地图");
canclick = true;
sel_num = 0;
removechild(child);
child=null;
mytimer.removeeventlistener("timer", timerhandler);
return;
}
}
};
private function huaxian():void {
var noteoint = this.m_path[0];
var nowoint=getxy(note);
child = new sprite();
child.graphics.linestyle(4, 0xff0000);
child.graphics.moveto(now.x,now.y);
for (var i:int=1; i<4; i++) {
note = this.m_path;
now=getxy(note);
child.graphics.lineto(now.x,now.y);
}
addchild(child);
this.m_path=null;
mytimer= new timer(600, 1);
mytimer.addeventlistener("timer", timerhandler);
mytimer.start();
}
}
}

没有了123 下一篇
上一页:没有了      下一页:AS和JS互相调用有个很酷的特性(as2.0)