Flash as 3.0 keyboard control code

Source: Internet
Author: User
Tags addchild arrays rand reset

Flash as 3.0 keyboard control code

Package sxl {// package path, that is, create an sxl folder, which contains the MainMap. as file
Import flash. display. Sprite; // display object container
Import flash. display. MovieClip; // you need to attach coordinates and other attributes to the block, so select it.
Import flash. display. DisplayObject; // you can see it for yourself.
Import flash. text. TextField;
Import flash. events. Event;
Import flash. events. MouseEvent;
Import flash. events. KeyboardEvent;
Import flash. ui. Keyboard;
Import flash. display. StageScaleMode;
Public class MainMap extends MovieClip {
/* | Container Road and obstacle block container | man game protagonist | GoldCoin GoldPos_array GoldXY gold coins can be placed in coordinates to generate sequence of gold coins
| Score_txt score | number of columns in the row col column number of square lines | length of the W_H square side and horizontal and vertical walking range of each step
| Width and height of stageW stageH | list_array stores all the ground blocks (accessible green blocks and obstacle blocks)
*/
Private var container: Sprite = new Sprite;
Private var man: MovieClip;
Private var GoldCoin: MovieClip;
Private var GoldPos_array: Array;
Private var GoldXY: uint = 0;
Private var Score_txt: TextField;
Private var row: uint = 28;
Private var col: uint = 20;
Private var W_H: uint = 20;
Private var stageW: uint = 560;
Private var stageH: uint = 420;
Private var list_array: Array;
Public function MainMap (){
Stage. scaleMode = StageScaleMode. NO_SCALE;
// Background
Var Bg: MovieClip = new MovieClip;
CreateRect (Bg, stageW, stageH, 0x808080, 0xDCF76F );
AddChild (Bg );
AddChild (container );
InitList_array ();
// Create the main character
Man = new MovieClip;
CreateRect (man, W_H, W_H, 0xFFFF00, 0xFF0000 );
Man. x = 0;
Man. y = W_H;
Man. X = 0;
Man. Y = 0;
AddChild (man );
// Create a background
SetTopBg ();
// Create a ground
InitMap ();
  }
//..... Create a background area .................................... ...............................
Private function setTopBg (){
Var topBg: MovieClip = new MovieClip;
CreateRect (topBg, 560, W_H, 0x000000, 0 xEEEEEE );
TopBg. y =-1;
Var title_txt: TextField = new TextField;
Title_txt.text = "by lausu QQ: 285510591 place chart set score :";
Title_txt.width = 400;
Title_txt.height = 18;
Title_txt.x = 10;
TopBg. addChild (title_txt );
Score_txt = new TextField ();
Score_txt.width = 60;
Score_txt.height = 18;
Score_txt.x = 400;
TopBg. addChild (Score_txt );
AddChild (topBg );
Var textBg: MovieClip = new MovieClip;
CreateRect (textBg, 55,16, 0 xEEEEEE, 0 xEEEEEE );
TextBg. x = 500;
Var txt: TextField = new TextField;
Txt. text = "Magic Map ";
Txt. height = 18;
Txt. width = 55;
TextBg. addChild (txt );
TextBg. mouseChildren = false;
TextBg. addEventListener (MouseEvent. CLICK, onReSet );
AddChild (textBg );
  }
//..... Reset the main character and ground area .................................. ...................................
Private function onReSet (event: MouseEvent ){
If (GoldXY> 0 ){
GoldXY --;
Var score: uint = GoldXY * 50;
Score_txt.text = String (score );
} Else {
Score_txt.text = "magic used up ";
Event.tar get. removeEventListener (MouseEvent. CLICK, onReSet );
   }
// Create the main character
Man = new MovieClip;
CreateRect (man, W_H, W_H, 0xFFFF00, 0xFF0000 );
Man. x = 0;
Man. y = W_H;
Man. X = 0;
Man. Y = 0;
AddChild (man );
// Reset the map
ReSetMap ();
  }
//..... Reset the ground area .................................... .....................................
Private function reSetMap (){
// Clear sub-elements in the INER container (that is, all ground blocks: green blocks and obstacle blocks)
Var long: int = container. numChildren-1;
For (var I: int = long; I> = 0; I --){
Container. removeChildAt (I );
   }
Stage. removeEventListener (KeyboardEvent. KEY_DOWN, onKeyDowns );
// Clear the elements in the list_array array and initialize it as a two-dimensional array
InitList_array ();
// Lay ground blocks
InitMap ();
  }
// Initialize list_array as a two-dimensional array
Private function initList_array (){
List_array = [];
For (var I: uint = 0; I <row; I ++ ){
List_array = new Array (col );
   }
  }
//........ Lay the ground block .................................... .................
Private function initMap (){
// Clear the gold coins and their coordinate values to be placed
GoldPos_array = [];
// N rand controls the creation of dark walls | m no controls the creation of yellow blocks
Var n: uint = 0;
Var m: uint = Math. random () * (row-1) * (col-1) + 2;
Var no: uint = 1;
For (var I: uint = 0; I <row; I ++ ){
For (var j: uint = 0; j <col; j ++ ){
No ++;
Var cell: MovieClip = new MovieClip;
// The rand random number determines the number of barrier blocks, that is, the complexity of accessible ground.
Var rand: uint = Math. random () * 2 + 1;
Cell. x = I * W_H;
Cell. y = W_H + j * W_H;
Cell. X = I;
Cell. Y = j;
If (rand = 2 ){
N ++;
     }
If (n % 3 = 0 & no! = M ){
If (cell. x = man. x & cell. y = man. y ){
// Create a passable green box. Attribute K = 0 indicates passable
CreateRect (cell, W_H, W_H, 0x808080, 0xDCF76F );
Cell. K = 0;
// The coordinates of the gold coins to be placed
GoldPos_array.push (cell );
} Else {
// Create a dark wall for an inaccessible obstacle. Attribute K = 1 indicates that the wall is not accessible.
CreateRect (cell, W_H, W_H, 0x808080, 0xCEBB0F );
Cell. K = 1;
      }
} Else if (no! = M ){
// Create a passable green box. Attribute K = 0 indicates passable
CreateRect (cell, W_H, W_H, 0x808080, 0xDCF76F );
Cell. K = 0;
// The coordinates of the gold coins to be placed
GoldPos_array.push (cell );
} Else {
// Create a passable green box. Attribute K = 0 indicates passable
// CreateRect (cell, W_H, W_H, 0x808080, 0xDCF76F );
Cell. K = 0;
// The coordinates of the gold coins to be placed
GoldPos_array.push (cell );
GoldCoin = new MovieClip;
CreateRect (GoldCoin, W_H, W_H, 0x808080, 0x2BBFFF );
GoldCoin. name = "GoldCoin ";
// If (goldBoolean ){
GoldCoin. x = cell. x;
GoldCoin. y = cell. y;
Container. addChild (GoldCoin );
     }
// Store squares in arrays
List_array [j] = cell;
// Add the square to the container
Container. addChild (cell );
    }
   }
AddChild (container );
// The main character is on the container, and the implementation is always visible.
This. swapChildren (man, container );
// The Order of the gold coins and their available coordinate arrays is out of order
RandomArray (GoldPos_array );
// After the ground is created, add the keyboard listener to implement the function of moving the main character
Stage. addEventListener (KeyboardEvent. KEY_DOWN, onKeyDowns );
  }
//..... Keyboard listening area ..................................... ........................................ ........................................ ...................
Private function onKeyDowns (event: KeyboardEvent ){
// Obtain the x y value of the main character, which is used to determine whether the x y value can be moved. (based on the x y value, the top, bottom, left, and right sides of the ground Square are determined by the list_array array, whether the k value of the top, bottom, left, and right sides of the square on the ground is 0, 0 is movable, and 1 is not movable)
Var X: uint = man. X;
Var Y: uint = man. Y;
Switch (event. keyCode ){
Case Keyboard. UP:
If (Y = 0 ){
// When the upper boundary is exceeded, set the coordinate of the main character to the bottom, and reset the ground block to enter a new ground.
Man. Y = col-1;
Man. y = stageH-W_H;
ReSetMap ();
} Else if (list_array [X] [Y-1]. K = 0 ){
// If K is 0, it indicates that it is a passable green block.
// The main character moves one cell up. At the same time, the Y value of the main character is reduced by 1.
Man. y-= W_H;
Man. Y --;
     }
Break;
Case Keyboard. DOWN:
If (Y = col-1 ){
// When the bottom boundary is exceeded, set the coordinate of the main character to the top, and reset the ground block to enter a new ground.
Man. Y = 0;
Man. y = W_H;
ReSetMap ();
} Else if (list_array [X] [Y + 1]. K = 0 ){
// If K is 0 in the lower section of the adjacent area, it indicates that it is a passable Green Square.
// Move the main character one by one. At the same time, the Y value of the main character is increased by 1.
Man. y + = W_H;
Man. Y ++;
     }
Break;
Case Keyboard. LEFT:
If (X = 0 ){
// When the left boundary is exceeded, set the coordinate of the main character to the rightmost, and reset the ground block to enter a new ground.
Man. X = row-1;
Man. x = stageW-W_H;
ReSetMap ();
} Else if (list_array [X-1] [Y]. K = 0 ){
// If K of the left square is 0, it indicates that it is a passable Green Square.
// The main character moves one cell to the left. At the same time, the X value of the main character is reduced by 1.
Man. x-= W_H;
Man. X --;
     }
Break;
Case Keyboard. RIGHT:
If (X = row-1 ){
// When the right boundary is exceeded, set the coordinate of the main character to the leftmost, and reset the ground block to enter a new ground.
Man. x = 0;
Man. X = 0;
ReSetMap ();
} Else if (list_array [X + 1] [Y]. K = 0 ){
// If K is 0 in the right square, it indicates that it is a passable Green Square.
// The protagonist moves one cell to the right. At the same time, the X value of the protagonist increases by 1.
Man. x + = W_H;
Man. X ++;
     }
Break;
Default:
Break;
   }
Var objisplayObject = container. getChildByName ("GoldCoin ");
If (man. x = obj. x & man. y = obj. y ){
Container. removeChild (obj );
// Create GoldCoin
// Order of gold coins
If (GoldXY <GoldPos_array.length ){
GoldXY ++;
Var score: uint = GoldXY * 50;
Score_txt.text = String (score );
} Else {
// Game over
Score_txt.text = "you vin! ";
    }
GoldCoin = new MovieClip;
CreateRect (GoldCoin, W_H, W_H, 0x808080, 0x2BBFFF );
GoldCoin. name = "GoldCoin ";
GoldCoin. x = GoldPos_array [GoldXY]. x;
GoldCoin. y = GoldPos_array [GoldXY]. y;
Container. addChild (GoldCoin );
   }
  }
//..... Create a rectangle ..................................... ......................................
Private function createRect (sprite: MovieClip, w: uint, h: uint, lineCor: uint, fillCor: uint ){
Sprite. graphics. lineStyle (1, lineCor );
Sprite. graphics. beginFill (fillCor );
Sprite. graphics. drawRect (0, 0, w, h );
Sprite. graphics. endFill ();
  }
// ----- Out-of-order array partition -------------------------------------------------------------
Private function randomArray (Target_array: Array ){
Target_array.sort (function (){
Return Math. round (Math. random ())? 1:-1;
});
  }
}
}

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.