Flash AS3 to see the implementation of small games

Source: Internet
Author: User
Tags addchild min reset

Build the FLA file. Name casually (I took Testllk.fla)
Create a new movie clip in the FLA (name is Mov_tupian), add your favorite pictures inside, each frame, align the origin. Write a Stop () in the first frame, and change the connection class to Tupian.
Create a new movie clip in the FLA (name me Line-mov) and add a box inside it, just as big as the picture in the movie clip. Change the connection class to Kuan.
Write Testllkan in the text as a class.

Llkan.as Code

Package {
/**
* Repeatedly look at the algorithm
*
* @author Huangxianchang
* @version1.0
* @date090329
*/
Import Flash.geom.point;
public class Llkan {
Array of maps
private Var M_map:array;
/**
* Repeatedly look at the algorithm
*/
Public Function Llkan (Map_arr:array) {
This.m_map = Map_arr;
}
//====================================
Properties
//====================================
/**
* Map Array
*/
Public function set Map (Map_arr:array): void {
This.m_map = Map_arr;
}
//====================================
Public methods
//====================================
/**
* @param p_startx start x coordinates
* @param p_starty start y-coordinate
* @param p_endx End x coordinates
* @param p_endy End y-coordinate
*
* @return found Path (array: [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;//exceeds the boundary or returns null for 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 Point content is identical to detect
4 points returned
var point1oint = new Point (P_startx,p_starty);
var point4oint = new Point (P_endx,p_endy);
var point2oint;
var point3oint;
Trace ([point1,point4]);
Save the contents of these 2 points and set it to 0 to indicate the
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;//Vertical can not connect?
var Hengcanxiao:boolean = false;//row can not connect?
Make judgments about the following shapes
// | |
// | |
// |----------|
// | |
// | |
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;
Find the same 2 arrays, which is the y-coordinate of the same row.
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
}
}
}
See if I can even
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);
Take out a row comparison with the same 2 points at the vertical
var Shu_y:int=one_blank_shu_arr;
Whether the row is connected
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;//Vertical can connect
Break
}
}
Vertical End
Shucanxiao = false;
if (Shucanxiao) {
Vertical connection
} else {
Vertical is not connected, judge the line
Make judgments about the following shapes
// ---------------
// |
// |
// ----------------
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;
Find the same x coordinate of 2 arrays, which is the same vertical.
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
}
}
}
See if I can even
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
}
}
}
End of Line
Restore the point that was just set
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 {//whether the bounds are exceeded
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;//beyond Boundary
}
if (this.m_map[p_endy][p_endx]==0| | This.m_map[p_endy][p_endx]==null) {
Trace ("Wei 0~~~~");
return true;
For 0 or no comparison without doing
}
Return false;//not beyond bounds
}
Private Function Chaoshu (P_endy:int): boolean {//whether to exceed vertical bounds
var mapheight:int = this.m_map.length;
if (P_endy < 0 | | | P_endy >= mapheight) {
Return false;//Beyond
}
return true;//not exceeded
}
Private Function Chaohan (P_endx:int): boolean {//is outside the bounds of the line
var mapwidth:int = this.m_map[0].length;
if (P_endx < 0 | | | p_endx >= mapwidth) {
Return false;//Beyond
}
return true;//not exceeded
}
}
}


Testllkan.as Code:

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;
/**
* Look at the test class
*
* @author Huangxianchang
* @version1.0
* @date090213
*/
public class test Llkan extends Sprite {
//====================================
//member variables
//====================== ==============
Private var scalexy=1.0;//defines scaling;
Private var llk_leng=12;//look at the number of pictures in this example total
private var M_player:kuan;
Private var m_map:array=new array (); Connect to see the map
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;//A few lines X
private var m_maph:int = 9;//several columns y
private Var sel_num:uint=0;
private var m_mapx:int = 20;//starting position
private var m_mapy:int = 40;//starting position
private Var Xstep:number;
private Var Ystep:number;
private var m_m_clograte:number = 0.3;
Private var m_path:array;//return path
private Var M_outtxt:textfield;
//====================================
Constructor
//====================================
Public Function Testllkan () {
stage.framerate=24;//Frame Frequency
Stage.align = Stagealign.top_left;
Stage.scalemode = Stagescalemode.no_scale;
stage.doubleclickenabled = true;//Double click
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;//get wide and high
T=null;
var all_num:uint = M_MAPW*M_MAPH/2;
var Stoparr = new Array ();//Initialize 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)//scrambled array
var nn:uint=0;
So add 0 and m_maph+1 to the side.
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;//next to 0, means nothing. Available through
} 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 ("Double click the blank area to rearrange the game");
}
Private Function Getxy (p_s oint) Oint {//converted to real coordinates
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 to New Point (p_x,p_y);
}
Private function Output (p_info:string): void {
This.m_outtxt.htmltext = "<b><font color= ' #0000ff ' size= '" > See example </font></b>ttt "+ p_info;
}
//====================================
Event handles
//====================================
Private function Luan (Luan_arr:array): void {//scrambled array
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 to 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 ("Can connect, is drawing line when:" +yongshi);
Huaxian ();
This.addeventlistener (Event.enter_frame, Enterframehandle);
} else {
Output ("Can not be connected to the general time:" +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 ("Repeatedly look, double-click the blank area rearrangement map");
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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.