03 3-1 3-2 3-3 3-4 3-5
3-1 Wii ( ) 3-2 3-3 8 8 3-4 3-5 3-4 3-3 3-2
3-5 8 ( sin cos ) 3-4 3-5 3-2 CH03_key4.fla ActionScript 3 12 "block_mc" + + "_" + 8 block_mc2_3 x_num y_num 1 01 02 03 04 05 06 07 08 09 10 3-3
(x_num y_ num) (1) ( ) ( ) actor_mc ActionScript 3 01 02 03 04 05 06 07 08 const x_num:int = 3; const y_num:int = 4; var actor_x:int = 1; var actor_y:int = 1; actor_mc.x = this["block_mc" + actor_x + "_" + actor_y].x; actor_mc.y = this["block_mc" + actor_x + "_" + actor_y].y; stage.addeventlistener(keyboardevent.key_down, playkeydown); stop(); 09 10 11 12 13 14 function playkeydown(event:keyboardevent) { var codec:int = event.charcode; if ( codec == 42 ) { // * actor_x = 1; actor_y = 4; else if ( codec == 35 ) { // # 3-4
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 actor_x = 3; actor_y = 4; else { var codek:int = event.keycode; if (codek == 38 ) { // up actor_y --; if ( actor_y < 1 ) { actor_y = y_num; else if (codek == 40) { // down actor_y ++; if ( actor_y > y_num ) { actor_y = 1; else if (codek == 37) { // left actor_x --; if ( actor_x < 1 ) { actor_x = x_num; else if (codek == 39) { // right actor_x ++; if ( actor_x > x_num ) { actor_x = 1; else if (codek <= 57 && codek >=48) { // 0-9 var digit:int = codek-48; if ( digit == 0 ) { actor_x = 2; actor_y = 4; else { actor_x = digit % x_num; if ( actor_x == 0 ) { actor_x = x_num; actor_y = (digit - actor_x)/x_num + 1; 01 02 03 04 05 06 07 08 09 10 3-5
51 52 53 54 55 actor_mc.x = this["block_mc" + actor_x + "_" + actor_y].x; actor_mc.y = this["block_mc" + actor_x + "_" + actor_y].y; 解說 01 02 x_num y_num 03 04 05 06 x y 07 stage KEY_DOWN playkeydown() stage stage KEY_UP 08 0 9 5 5 p l a y K e y D o w n ( ) event:keyboardevent ActionScript 3 charcode keycode keycode charcode charcode shift keycode shif 3-6
10 charcode 11 17 18 51 keycode 19 38 39 50 0 9 keycode 48 57 48( 40) 0 ( 42 43) 1 9 ( 45 49) 53 54 x y charcode keycode 01 02 03 04 05 06 07 08 09 10 3-7
3-3 CH03_key8.fla 45 ( ) land_mc 2 2 land_ btn1 ( 1) land_btn2 ( 2) actor_mc 90 90 45 actor_mc 3-8
8 actor actor 3 1 2 3 2 影格 1, 初值 01 actor_mc.gotoandstop(" "); 02 var dx:int = 5; 03 var dy:int = 5; 04 var landtype:int = 1; 05 var dir_now, dir_old:int; 06 var dir1:boolean = false; 07 var dir2:boolean = false; 08 var dir3:boolean = false; 09 var dir4:boolean = false; 10 stage.addeventlistener(keyboardevent.key_down, playkeydown); 11 stage.addeventlistener(keyboardevent.key_up, playkeyup); 12 land_btn1.addeventlistener(mouseevent.click, land1); 13 land_btn2.addeventlistener(mouseevent.click, land2); 14 function playkeydown(event:keyboardevent) { 15 var codek:int = event.keycode; 16 if ( codek >= 37 && codek <= 40 ) { 17 this["dir" + (codek - 36)] = true; 18 getdirection(); 19 20 21 function playkeyup(event:keyboardevent) { 01 02 03 04 05 06 07 08 09 10 3-9
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 var codek:int = event.keycode; if ( codek >= 37 && codek <= 40 ) { this["dir" + (codek - 36)] = false; getdirection(); function getdirection() { if ( dir1 &&!dir2 &&!dir3 &&!dir4 ) { dir_now = 1; // else if ( dir1 && dir2 &&!dir3 &&!dir4 ) { dir_now = 2; // else if (!dir1 && dir2 &&!dir3 &&!dir4 ) { dir_now = 3; // else if (!dir1 && dir2 && dir3 &&!dir4 ) { dir_now = 4; // else if (!dir1 &&!dir2 && dir3 &&!dir4 ) { dir_now = 5; // else if (!dir1 &&!dir2 && dir3 && dir4 ) { dir_now = 6; // else if (!dir1 &&!dir2 &&!dir3 && dir4 ) { dir_now = 7; // else if ( dir1 &&!dir2 &&!dir3 && dir4 ) { dir_now = 8; // if ( dir_now!= dir_old ) { actor_mc.gotoandstop(dir_now); dir_old = dir_now; function land1(event:mouseevent) { landtype = 1; actor_mc.x = 320; actor_mc.y = 200; land_mc.gotoandstop(landtype); function land2(event:mouseevent) { 3-10
58 landtype = 2; 59 actor_mc.x = 320; 60 actor_mc.y = 200; 61 land_mc.gotoandstop(landtype); 62 解說 01 actor_mc ( ) 02 03 actor_mc actor_mc actor_mc 04 int 1 land_mc 1 2 05 2 actor_mc int dir_now ( ) dir_old ( ) actor_mc 06 09 Boolean false( ) true 10 stage KEY_DOWN playkeydown() 11 s t a g e K E Y _ U P playkeyup() 12 13 land_btn1 land_btn2 land1() land2() 01 02 03 04 05 06 07 08 09 10 3-11
1 4 2 0 p l a y K e y D o w n ( ) event:keyboardevent 15 keycode 16 19 37 40 true( 17) getdirection() ( 18) 2 1 2 7 p l a y K e y U p ( ) event:keyboardevent 22 26 37 40 false( 24) getdirection() ( 25) 28 50 getdirection() actor_mc 29 45 true false true( 30 34 38 42) 90 true( 32 36 40 44) 3-12
46 49 actor_mc ( 47) ( 48) 51 56 land1() event:mouseevent land_btn1 1( 52) actor_mc ( 53 54) land_mc ( 55) 57 62 land2() event:mouseevent land_btn2 2( 58) land1() 影格 2, 遊戲中 01 if ( dir1 dir2 dir3 dir4 ) { 02 actor_mc.actor.play(); 03 moveactor(); 04 else { 05 actor_mc.actor.gotoandstop(1); 06 07 function moveactor() { 08 var new_x, new_y:number; 09 var mx:int = 0; 10 var my:int = 0; 11 if ( dir_now == 1 ) { 12 mx = -dx; 13 else if ( dir_now == 2 ) { 14 mx = -dx; 15 my = -dy; 16 else if ( dir_now == 3 ) { 17 my = -dy; 18 else if ( dir_now == 4 ) { 01 02 03 04 05 06 07 08 09 10 3-13
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 mx = dx; my = -dy; else if ( dir_now == 5 ) { mx = dx; else if ( dir_now == 6 ) { mx = dx; my = dy; else if ( dir_now == 7 ) { my = dy; else if ( dir_now == 8 ) { mx = -dx; my = dy; new_x = actor_mc.x + mx; new_y = actor_mc.y + my; if ( landtype == 1 ) { if ( new_x>=100 && new_x<=540 && new_y>=60 && new_y<=340 ) { actor_mc.x = new_x; actor_mc.y = new_y; else { if ( land_mc.hittestpoint(new_x, new_y, true)) { actor_mc.x = new_x; actor_mc.y = new_y; 解說 01 06 true actor_mc actor_mc actor ( 02 ) moveactor() ( 03) actor_mc 3-14
05 false actor_mc actor_mc actor 1( ) 07 45 moveactor() 08 2 Number actor_mc 09 10 2 int actor_mc 0 11 31 ( ) 32 33 actor_mc 34 44 35 38 40 43 35 38 actor_mc actor_mc ( 36 37) 40 44 hittestpoint() land_mc actor_mc actor_mc ( 41 42) 01 02 03 04 05 06 07 08 09 10 3-15
影格 3 01 gotoandplay(" "); 解說 01 3-4 3-16
CH03_1.fla ( ) ( ) ( ) 3-4-1 遊戲流程 ( ) <Enter> 01 02 03 04 05 06 07 08 09 10 3-17
( ) stage KEY_DOWN KEY_UP <Enter> 3-4-2 舞台圖層配置 3-18
map_mc actor_mc block_ mc map_mc actor_mc map_mc actor_mc map_mc map_mc actor_mc map_mc map_mc map_mc map_mc map_mc score_txt smallmap_mc 01 02 03 04 05 06 07 08 09 10 3-19
3-4-3 主時間軸程式 影格 1, 載入進度 2-4 1 影格 2, 說明畫面 01 02 03 map_mc.actor_mc.visible = false; stage.addeventlistener(keyboardevent.key_down, gamestartkey); stop(); 04 05 06 07 08 09 function gamestartkey(event:keyboardevent) { var codek:int = event.keycode; if ( codek == 13 ) { gotoandplay(" "); 解說 01 map_mc actor_mc 02 stage gamestartkey() Flash 03 <Enter> 04 09 gamestartkey() keycode ( 05) 13 <Enter> ( 07) 3-20
影格 3, 初值化 01 stage.removeeventlistener(keyboardevent.key_down, gamestartkey); 02 map_mc.actor_mc.visible = true; 03 map_mc.actor_mc.gotoandstop(" "); 04 smallmap_mc.actor_mc.visible = false; 05 var spots:int = 5; 06 var score:int = 0; 07 var dx:int = 5; 08 var dy:int = 5; 09 var dir_now, dir_old:int; 10 var dir1:boolean = false; 11 var dir2:boolean = false; 12 var dir3:boolean = false; 13 var dir4:boolean = false; 解說 01 stage gamestartkey() 02 map_mc actor_mc 03 actor_mc 04 smallmap_m actor_mc actor_mc 05 int 06 int 0 07 13 影格 4, 遊戲開始 01 for (var i:int = 1; i<=spots; i++ ) { 02 while ( true ) { 01 02 03 04 05 06 07 08 09 10 3-21
03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 var mx:number = Math.floor(Math.random()*map_mc.width); var my:number = Math.floor(Math.random()*map_mc.height); if (!map_mc.block_mc.hittestpoint(mx+map_mc.x, my+map_mc.y, true) &&!map_mc.actor_mc.hittestpoint(mx+map_mc.x, my+map_mc.y)) { this["spot_mc" + i] = new spot_mc(); this["spot_mc" + i].x = mx; this["spot_mc" + i].y = my; this["spot_mc" + i].num = i; this["spot_mc" + i].gotoandplay(" "); map_mc.addchild(this["spot_mc" + i]); this["reddot_mc" + i] = new reddot_mc(); this["reddot_mc" + i].x = smallmap_mc.x + (mx)/10; this["reddot_mc" + i].y = smallmap_mc.y + (my)/10; stage.addchild(this["reddot_mc" + i]); break; centermap(); map_mc.setchildindex(map_mc.actor_mc, map_mc.numchildren-1); var bluedot:bluedot_mc = new bluedot_mc(); stage.addchild(bluedot); stage.addeventlistener(keyboardevent.key_down, playkeydown); stage.addeventlistener(keyboardevent.key_up, playkeyup); 26 27 28 29 30 31 32 33 34 35 36 function centermap() { map_mc.x = 320 - map_mc.actor_mc.x; map_mc.y = 240 - map_mc.actor_mc.y; function playkeydown(event:keyboardevent) { var codek:int = event.keycode; if ( codek >= 37 && codek <= 40 ) { this["dir" + (codek - 36)] = true; getdirection(); 3-22
37 function playkeyup(event:keyboardevent) { 38 var codek:int = event.keycode; 39 if ( codek >= 37 && codek <= 40 ) { 40 this["dir" + (codek - 36)] = false; 41 getdirection(); 42 43 44 function getdirection() { 45 if ( dir1 &&!dir2 &&!dir3 &&!dir4 ) { 46 dir_now = 1; // 47 else if ( dir1 && dir2 &&!dir3 &&!dir4 ) { 48 dir_now = 2; // 49 else if (!dir1 && dir2 &&!dir3 &&!dir4 ) { 50 dir_now = 3; // 51 else if (!dir1 && dir2 && dir3 &&!dir4 ) { 52 dir_now = 4; // 53 else if (!dir1 &&!dir2 && dir3 &&!dir4 ) { 54 dir_now = 5; // 55 else if (!dir1 &&!dir2 && dir3 && dir4 ) { 56 dir_now = 6; // 57 else if (!dir1 &&!dir2 &&!dir3 && dir4 ) { 58 dir_now = 7; // 59 else if ( dir1 &&!dir2 &&!dir3 && dir4 ) { 60 dir_now = 8; // 61 62 if ( dir_now!= dir_old ) { 63 map_mc.actor_mc.gotoandstop(dir_now); 64 dir_old = dir_now; 65 66 解說 01 19 map_mc smallmap_mc 01 02 03 04 05 06 07 08 09 10 3-23
02 18 map_mc while 03 04 x y Number x 0 map_mc y 0 map_mc 05 17 map_ mc block_mc actor_mc actor_mc hittestpoint() 2-3 stage (mx,my) map_mc stage hittestpoint() false 06 spot_mc spot_mc+ 07 08 x y 09 10 actor_mc 11 map_mc map_mc 3-24
12 reddot_mc reddot_ mc+ 13 14 x y 10( smallmap_mc map_mc 10%) smallmap_mc ( smallmap_mc ) 15 stag stage 16 while 20 centermap() actor_mc actor_mc 21 setchildindex() actor_mc map_ mc actor_mc actor_mc setchildindex() 2 1 2 numchildren 1( 0 ) 22 bluedot_mc bluedot smallmap_mc actor_mc 23 stag stage actor_mc 24 25 stage KEY_DOWN KEY_UP playkeydown() playkeyup() 01 02 03 04 05 06 07 08 09 10 3-25
26 29 actor_mc centermap() map_mc (320,240) actor_mc 30 36 playkeydown() 37 43 playkeyup() 44 66 getdirection() actor_mc map_mc( 63) actor_mc map_mc 影格 5, 遊戲中 01 02 03 04 05 06 07 08 09 10 updatescore(); if ( dir1 dir2 dir3 dir4 ) { map_mc.actor_mc.actor.play(); moveactor(); centermap(); else { map_mc.actor_mc.actor.gotoandstop(1); bluedot.x = smallmap_mc.x + (map_mc.actor_mc.x)/10; bluedot.y = smallmap_mc.y + (map_mc.actor_mc.y)/10; 11 12 13 14 15 16 17 18 19 20 function updatescore() { score_txt.text = score; function moveactor() { var new_x, new_y:number; var mx:int = 0; var my:int = 0; if ( dir_now == 1 ) { mx = -dx; else if ( dir_now == 2 ) { 3-26
21 mx = -dx; 22 my = -dy; 23 else if ( dir_now == 3 ) { 24 my = -dy; 25 else if ( dir_now == 4 ) { 26 mx = dx; 27 my = -dy; 28 else if ( dir_now == 5 ) { 29 mx = dx; 30 else if ( dir_now == 6 ) { 31 mx = dx; 32 my = dy; 33 else if ( dir_now == 7 ) { 34 my = dy; 35 else if ( dir_now == 8 ) { 36 mx = -dx; 37 my = dy; 38 39 new_x = map_mc.actor_mc.x + mx; 40 new_y = map_mc.actor_mc.y + my; 41 if (!map_mc.block_mc.hittestpoint(new_x+map_mc.x, new_y+map_mc.y, true)) { 42 map_mc.actor_mc.x = new_x; 43 map_mc.actor_mc.y = new_y; 44 45 解說 01 02 08 true actor_mc ( 03) moveactor() ( 04) actor_mc actor_mc actor_mc ( 05) 01 02 03 04 05 06 07 08 09 10 3-27
09 10 actor_mc 11 13 updatescore() score_txt 14 45 moveactor() hittestpoint() map_mc block_mc actor_mc 影格 6 01 02 03 04 05 if ( score == spots ) { gotoandstop(" "); else { gotoandplay(" "); 解說 01 05 ( 02) ( 04) 影格 7, 成功 01 02 03 map_mc.actor_mc.visible = false; stage.removechild(bluedot); stage.removeeventlistener(keyboardevent.key_down, playkeydown); 3-28
04 stage.removeeventlistener(keyboardevent.key_up, playkeyup); 05 stage.addeventlistener(keyboardevent.key_down, gamestartkey); 06 stop(); 解說 01 map_mc actor_mc 02 stage 03 04 stage 05 stage gamestartkey() <Enter> 06 3-4-4 特殊影片片段 01 02 03 04 05 06 07 08 09 10 3-29
1 3 actor_mc ( ) ( ) block_mc actor_mc hittestpoint ( ) ( ) alpha 0% ( ) actor_mc actor_mc ( ) ( ) hittestpoint block_mc actor_ mc 3-30
actor_mc Action & 影格 1, 初值 : 01 var num:int; 02 stop(); 解說 01 02 影格 2, 遊戲中 : actor_mc actor_ mc 01 02 03 04 05 06 07 08 09 10 3-31
01 02 03 04 05 06 07 08 if (Point.distance(new Point(this.x, this.y), new Point(parent.actor_mc.x,parent.actor_ mc.y)) < 70 ) { this.visible = true; else { this.visible = false; if ( this.hittestpoint(parent.actor_mc.x+parent.parent.map_mc.x, parent.actor_ mc.y+parent.parent.map_mc.y, true) ) { this.gotoandplay(" "); 解說 01 05 distance() actor_mc distance() 2 70 actor_mc 70 ( 02) ( 04) actor_mc 06 08 hittestpoint() actor_mc actor_mc stage x y map_mc ( 07) 影格 3: 01 gotoandplay(" "); 解說 01 3-32
影格 4, 找到 : 6 影格 6: 01 parent.parent.score ++; 02 stage.removechild(parent.parent["reddot_mc" + num]); 03 parent.removechild(this); 04 stop(); 解說 01 1 02 03 04 01 02 03 04 05 06 07 08 09 10 3-33
pose pose 8 actor 1 8 actor_mc 8 pose actor ( ) 1 10 2 actor_mc 3-5 3-34