Flash ActionScript2.0 Object-oriented game development

Source: Internet
Author: User
Tags array class manager constructor
Object

Overview:

Flash ActionScript2.0 is a object-oriented programming language that can be used to develop powerful applications. Compared to the early ActionScript 1.0, it has a significant advantage in structured programming, enabling programs to be more portable and heavy, and scalability.

This article through a push box game development process to tell step-by-step how to use ActionScript2.0 to develop.

Structure Analysis:

Project planning is essential prior to the development of a project. Let's think about the necessary elements of the push box game.

    • One player (player)
    • At least one box (Box)
    • Same target as the number of boxes
    • Indefinite number of bricks (Brick)
    • The bricks make up the walls (Wall)
    • A game Manager (Manager)
    • Some controls that display game information

We all need to abstract these elements into objects (object), each containing its own unique properties and methods. For example, the player has a position attribute (Currentpos), which means that the player has a full location, and that there is a move method, It causes the player to move to the next position. Isosceles These problems are clear, we have to carry out concrete implementation.

Implementation process:

  1. Open Flash, create a new action script file, and then create the following. As files, and note that you want to put them in the same directory. The source file is as follows:
  2. The
  3. //*********pos.as*********
    //class used to represent the position of the object in the game
    //The position of all objects in the game is an instance of this class
    ///Col,row two quantities to determine the position of the object
    class Pos {
    Var col:number;//column value
    var row:number;//row value

    //constructor
    function Pos (c, R) {
    col = c;
    row = R;
    }

    //To determine if two locations are coincident
    function equals (t): Boolean {
    return (col = = T.col && row = = T.row);
    }

    ///According to the direction value, get the next position at this position
    function Getnextpos (d):P os{
    var nextpos:pos;
    Nextpos=new Pos (Col,row);
    Switch (d) {
    Case 0:
    nextpos.col++;

    break;
    Case 1:
    nextpos.row++
    Break
    Case 2:
    nextpos.col--
    Break
    Case 3:
    nextpos.row--
    Break
    }
    return Nextpos
    }
    }
  4. //************wall.as************
    //used to build the wall in the game
    Class Wall extends MovieClip {
    var brickmatrix:array;// Represents an array of wall shapes
    var left:number;//left offset
    var top:number;//the offset
    var bricks:array;//is used to hold the array of brick movie clips
    var _stepsize: number;//per brick offset

    //constructor
    Function Wall () {
    left = 0;
    top = 0;
    _stepsize = 40;


    }


    //Set Wall shape array
    function SetMatrix (b) {

    Bricks = new Array ();
    Brickmatrix = b;
    }

    //Wall-shaped array repair
    function build (): Void {
    var index = 0;
    for (var i = 0; i<brickmatrix.length; i++) {
    for (var j = 0; j<brickmatrix[0].length; j + +) {
    if (brickmatrix[i ][J] = = 1) {//Array value 1, draw a brick
    Bricks[index] = Attachmovie ("Brick", "bricks" +index, this.getnexthighestdepth ());
    bricks[index]._x = left+j*_stepsize;
    Bricks[index]._y = top+i*_stepsize;
    index++;
    }
    }
    }
    }
    }
  5. target.as*****************
    Class Target extends movieclip{
    var Pos:pos;
    var Finished:boolean;
    var Left:number;
    var Top:number;
    var _stepsize:number;
    function Target () {
    Finished=false;
    left=0;
    Top=0;
    _stepsize=40;

    }
    function SetPos (p) {
    Pos=p;
    This._x=left+pos.col*_stepsize;
    This._y=top+pos.row*_stepsize;
    }
    }
  6. player.as**************
    Player in the game
    Class Player extends MovieClip {

    var _stepsize:number;//step
    var _wall:movieclip;//wall Film cutting-ribbon series
    var startpos:pos;//initial Reset
    var currentpos:pos;//current Location
    var left:number;//left margin
    var top:number;//top margin
    var dir:number;//move Direction
    var Boxes:array;
    var player_path_stack:array;//player's Moving route
    var boxes_path_stack:array;//box-Moving course

    Constructor, initializing
    function Player () {
    _stepsize = 40;
    Currentpos = null;
    left = 0;
    top = 0;
    dir = 0;
    Player_path_stack=new Array ();
    Boxes_path_stack=new Array ();

    boxes = new Array ();
    }


    Set the player's starting position
    function Setstartpos (p) {
    startpos = new Pos (P.col, P.row);
    Currentpos = startpos;
    this._x = Currentpos.col*_stepsize+left;
    this._y = Currentpos.row*_stepsize+top;

    }

    Assign a wall instance to the player
    function Setwall (w) {
    _wall = W;
    }

    Assign a boxes array to the player, all instances of the case
    function Setboxes (b) {
    boxes = b;
    }


    Controlling the player by pressing the key
    function Setkeyhandle () {
    This.onkeydown = function () {

    if (Key.isdown (Key.left)) {
    Dir = 2;//Left

    else if (Key.isdown (key.right)) {
    DIR = 0;//Right button

    else if (Key.isdown (key.up)) {
    DIR = 3;//Upper Key

    else if (Key.isdown (Key.down)) {
    dir = 1;//Key

    }
    var nextobject = This.getnextobject (); Get the object at the next position of the player

    if (Nextobject = = "box") {//player next position is a box
    var box_pushed = Getpushedbox ();//Get this box at this location

    The box that was pushed to exist
    if (box_pushed) {


    if (box_pushed.getnextobject () = "Nothing") {//pushed to the next position of the box without obstruction

    Boxes_path_stack.push ({box:box_pushed,pos:new pos (box_pushed.pos.col,box_pushed.pos.row)});
    Box_pushed.move ()//Be pushed by the box to move one step
    This.move ()//player Move one step

    }

    }

    else if (Nextobject = "Nothing") {//player's next position plays an obstacle
    This.move ();
    Boxes_path_stack.push (NULL);
    }

    };
    Key.addlistener (this); Listening for key input
    }

    Get pushed into the box
    function Getpushedbox () {
    var Nextpos:pos;
    Nextpos =currentpos.getnextpos (dir);


    for (var i = 0; i<boxes.length; i++) {
    if (Boxes[i].pos.equals (Nextpos)) {
    return boxes[i];
    Break
    Trace (box_pushed);
    }
    }
    return null;
    }


    Get the type of block in the player's next position (walls, boxes, barrier-free)
    function GetNextObject (): String {
    var obj:string;
    var Nextpos:pos;
    Nextpos = Currentpos.getnextpos (dir);

    if (_wall.brickmatrix[nextpos.row][nextpos.col] = = 1) {
    return "WALL";
    }
    for (var i = 0; i<boxes.length; i++) {
    if (Boxes[i].pos.equals (Nextpos)) {
    return "box";
    }
    }
    Return to "nothing";
    }

    Move to the next location
    function Move () {
    Player_path_stack.push (Currentpos);

    var c=currentpos.getnextpos (dir);
    Currentpos=c;
    this._x = left+this.currentpos.col*_stepsize;
    this._y = top+this.currentpos.row*_stepsize;

    }

    Back to the previous step
    function Reback () {
    if (player_path_stack.length>0) {
    var pre=player_path_stack.pop ();
    Currentpos=pre;
    this._x = left+this.currentpos.col*_stepsize;
    this._y = top+this.currentpos.row*_stepsize;
    }
    if (boxes_path_stack.length>0) {
    var obj=boxes_path_stack.pop ();
    if (obj!=null) {
    var Box=obj.box;
    var Pos=obj.pos;
    Box.pos=pos;
    Box._x=box.left+box.pos.col*box._stepsize;
    Box._y=box.top+box.pos.row*box._stepsize;
    }
    }
    }
    }

  7. Class Box extends MovieClip {
    var Pos:pos;
    var Left:number;
    var Top:number;
    var _wall:movieclip;
    var Player:movieclip;
    var _stepsize:number;
    var _targets:array;
    var finished:boolean;//whether this box reaches the target

    function Box () {
    left = 0;
    top = 0;
    Finished = false;
    _stepsize = 40;
    }

    Set Target
    function Settargets (t) {
    _targets = new Array ();
    _targets = t;
    }

    Set the starting position of the box unheeded
    function Setstartpos (p) {
    pos = new POS (P.col, P.row);
    this._x = Pos.col*_stepsize+left;
    this._y = Pos.row*_stepsize+top;
    }

    Move to the next location
    function Move () {
    var c = Pos.getnextpos (Player.dir);
    pos = c;
    this._x = left+pos.col*_stepsize;
    this._y = top+pos.row*_stepsize;
    Gottarget ();
    }

    Whether to reach the target
    function Gottarget () {
    for (var i = 0; i<_targets.length; i++) {
    if (This.pos.equals (_targets[i].pos)) {
    if (This.getdepth () >_targets[i].getdepth ()) {
    This.swapdepths (_targets[i]);
    }
    This.finished = true;
    Return
    }
    }
    This.finished = false;
    }

    Get the type of object in a position
    function GetNextObject (): String {
    var obj:string;
    var Nextpos:pos;
    Nextpos = Pos.getnextpos (Player.dir);
    if (_wall.brickmatrix[nextpos.row][nextpos.col] = = 1) {
    return "WALL";
    }
    for (var i = 0; i<player.boxes.length; i++) {
    if (Player.boxes[i].pos.equals (Nextpos)) {
    return "box";
    }
    }
    Return to "nothing";
    }
    }
  8. Class matrix{
    Used to store the initial interface of the game's Guanzhong
    static Var Matrixs:array;

    function Matrix () {
    Matrixs=new Array ();
    0:null
    1:wall
    2:player
    3:box
    4:target
    5:target and Box
    1 2 3 4 5 6 7 8 9
    matrixs[0]= [[0, 0, 0, 0, 0, 0, 0, 0, 0],//1
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//2
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//3
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//4
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//5
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//6
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//7
    [0, 0, 0, 0, 0, 0, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];

    1 2 3 4 5 6 7 8 9
    matrixs[1]= [[0, 1, 1, 1, 1, 1, 1, 0, 0],//1
    [1, 1, 1, 0, 0, 0, 1, 0, 0],//2
    [1, 0, 3, 0, 1, 0, 1, 1, 0],//3
    [1, 0, 1, 0, 0, 4, 0, 1, 0],//4
    [1, 0, 0, 0, 0, 1, 0, 1, 0],//5
    [1, 1, 0, 1, 0, 0, 0, 1, 0],//6
    [0, 1, 2, 0, 0, 1, 1, 1, 0],//7
    [0, 1, 1, 1, 1, 1, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[2]= [[0, 0, 1, 1, 1, 1, 1, 0, 0],//1
    [1, 1, 1, 0, 0, 0, 1, 0, 0],//2
    [1, 0, 3, 0, 1, 0, 1, 1, 0],//3
    [1, 0, 1, 0, 0, 4, 0, 1, 0],//4
    [1, 0, 0, 0, 0, 1, 0, 1, 0],//5
    [1, 1, 3, 1, 4, 0, 0, 1, 0],//6
    [0, 1, 2, 0, 0, 1, 1, 1, 0],//7
    [0, 1, 1, 1, 1, 1, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];

    1 2 3 4 5 6 7 8 9
    matrixs[3]= [[1, 1, 1, 1, 1, 1, 1, 1, 0],//1
    [1, 0, 0, 0, 1, 0, 0, 1, 0],//2
    [1, 0, 0, 0, 0, 0, 0, 1, 0],//3
    [1, 1, 3, 1, 1, 0, 0, 1, 0],//4
    [0, 1, 0, 4, 1, 0, 1, 1, 1],//5
    [0, 1, 0, 0, 0, 0, 2, 0, 1],//6
    [0, 1, 0, 0, 1, 0, 0, 0, 1],//7
    [0, 1, 1, 1, 1, 1, 1, 1, 1],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];

    1 2 3 4 5 6 7 8 9
    matrixs[4]= [[1, 1, 1, 1, 1, 1, 1, 1, 0],//1
    [1, 0, 0, 0, 1, 0, 0, 1, 0],//2
    [1, 0, 0, 0, 0, 0, 0, 1, 0],//3
    [1, 1, 5, 1, 1, 0, 0, 1, 0],//4
    [0, 1, 0, 4, 1, 0, 1, 1, 1],//5
    [0, 1, 0, 5, 0, 3, 2, 0, 1],//6
    [0, 1, 0, 0, 1, 0, 0, 0, 1],//7
    [0, 1, 1, 1, 1, 1, 1, 1, 1],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[5]= [[0, 0, 1, 1, 1, 1, 0, 0, 0],//1
    [0, 1, 1, 0, 0, 1, 1, 1, 1],//2
    [1, 1, 0, 0, 0, 0, 0, 0, 1],//3
    [1, 0, 3, 0, 1, 2, 1, 0, 1],//4
    [1, 0, 1, 0, 4, 0, 0, 0, 1],//5
    [1, 0, 0, 3, 0, 1, 1, 1, 1],//6
    [1, 1, 1, 0, 4, 1, 0, 0, 0],//7
    [0, 0, 1, 1, 1, 1, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[6]= [[0, 0, 1, 1, 1, 1, 0, 0, 0],//1
    [0, 1, 1, 0, 0, 1, 1, 1, 1],//2
    [1, 1, 0, 0, 3, 0, 0, 0, 1],//3
    [1, 0, 3, 0, 1, 2, 1, 0, 1],//4
    [1, 0, 1, 0, 4, 0, 0, 0, 1],//5
    [1, 0, 4, 3, 0, 1, 1, 1, 1],//6
    [1, 1, 1, 0, 4, 1, 0, 0, 0],//7
    [0, 0, 1, 1, 1, 1, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[7]= [[0, 1, 1, 1, 1, 1, 0, 0, 0],//1
    [0, 1, 0, 0, 0, 1, 0, 0, 0],//2
    [1, 1, 3, 1, 0, 1, 1, 1, 0],//3
    [1, 0, 0, 0, 3, 2, 0, 1, 0],//4
    [1, 0, 1, 0, 0, 1, 0, 1, 0],//5
    [1, 0, 1, 4, 0, 4, 0, 1, 0],//6
    [1, 0, 0, 0, 1, 1, 1, 1, 0],//7
    [1, 1, 1, 1, 1, 0, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[8]= [[0, 1, 1, 1, 1, 1, 0, 0, 0],//1
    [0, 1, 0, 0, 0, 1, 0, 0, 0],//2
    [1, 1, 3, 1, 0, 1, 1, 1, 0],//3
    [1, 0, 0, 4, 3, 2, 0, 1, 0],//4
    [1, 0, 1, 0, 0, 1, 0, 1, 0],//5
    [1, 0, 1, 4, 4, 3, 0, 1, 0],//6
    [1, 0, 0, 0, 1, 1, 1, 1, 0],//7
    [1, 1, 1, 1, 1, 0, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[9]= [[0, 1, 1, 1, 1, 1, 1, 1, 0],//1
    [0, 1, 0, 2, 4, 4, 4, 1, 0],//2
    [0, 1, 0, 0, 0, 1, 1, 1, 1],//3
    [1, 1, 1, 3, 0, 0, 0, 0, 1],//4
    [1, 0, 0, 0, 1, 3, 1, 0, 1],//5
    [1, 0, 3, 0, 1, 0, 0, 0, 1],//6
    [1, 0, 0, 0, 1, 1, 1, 1, 1],//7
    [1, 1, 1, 1, 1, 0, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    1 2 3 4 5 6 7 8 9
    matrixs[10]= [[0, 0, 1, 1, 1, 1, 0, 0, 0],//1
    [0, 0, 1, 0, 0, 1, 0, 0, 0],//2
    [0, 0, 1, 0, 0, 1, 0, 0, 0],//3
    [0, 0, 1, 0, 0, 1, 1, 1, 0],//4
    [0, 0, 1, 4, 3, 3, 2, 1, 0],//5
    [0, 0, 1, 0, 0, 4, 0, 1, 0],//6
    [0, 0, 1, 0, 0, 1, 1, 1, 0],//7
    [0, 0, 1, 1, 1, 1, 0, 0, 0],//8
    [0, 0, 0, 0, 0, 0, 0, 0, 0]//9
    ];
    }

    function Getmatrix (d) {
    return matrixs[d];
    }

    }


  9. manager.as*************
    Used to manage games
    All the objects are in it.
    Subclasses of the MovieClip class
    Class Manager extends MovieClip {
    var Wall:movieclip;
    var Player:movieclip;
    var Boxes:array;
    var Targets:array;
    Current number of games for Var stagenum:number;//


    function Manager () {
    _x=50;
    _y=50;
    }

    Game initialization
    function init (s) {
    Stagenum = s;

    boxes = new Array ();
    Targets = new Array ();

    var brickmatrixt = new Matrix (). Getmatrix (Stagenum)//To obtain the corresponding matrix from the matrix class based on the number of closing numbers

    Repair Wall
    Wall = Attachmovie ("Wall", "Wall", this.getnexthighestdepth ());
    Wall.setmatrix (BRICKMATRIXT);
    Wall.build ();

    Create a player
    Player = Attachmovie ("Player", "Plyaer", this.getnexthighestdepth ());
    Player.setwall (wall);
    Player.setkeyhandle ()//listening for keyboard input


    for (var i = 0; i<brickmatrixt.length; i++) {
    for (var j = 0; j<brickmatrixt[0].length; j + +) {
    if (brickmatrixt[i][j] = = 2) {
    Player.setstartpos (New Pos (J, i));
    }
    if (brickmatrixt[i][j] = = 3) {
    var d = this.getnexthighestdepth ();
    var box = Attachmovie ("box", "box" +i+j, D);
    Box.setstartpos (New Pos (J, i));
    Box.player = player;
    Box._wall = wall;
    Boxes.push (box);
    }
    if (brickmatrixt[i][j] = = 4) {
    var d = this.getnexthighestdepth ();
    var t = Attachmovie ("target", "target" +i+j, D);
    T.setpos (New Pos (J, i));
    Targets.push (t);
    }
    if (brickmatrixt[i][j] = = 5) {
    var d = this.getnexthighestdepth ();
    var box = Attachmovie ("box", "box" +i+j, D);
    Box.setstartpos (New Pos (J, i));
    Box.player = player;
    Box._wall = wall;
    Box.finished = true;
    Boxes.push (box);
    var D1 = this.getnexthighestdepth ();
    var t = Attachmovie ("target", "target" +i+j, D1);
    T.setpos (New Pos (J, i));
    Targets.push (t);
    }
    }
    }


    for (var i = 0; i<boxes.length; i++) {
    Boxes[i].settargets (targets);
    }

    Player.setboxes (boxes);
    Player.swapdepths (This.getnexthighestdepth ());
    }
    Judge whether the game is over
    function Ifwin () {
    var win = true;
    for (var i = 0; i<boxes.length; i++) {
    Win = boxes[i].finished && win;
    }

    Return win;
    }
    }
  10. Create a new flash document named "Push_box" and place it in the same directory as the script file above. The next thing to do is to create a movie clip to use and link it to the corresponding script file.
  11. Press "Ctrl+f8", create a new movie clip, named "Player," to draw a long, wide 40-pixel villain in the editing state (_stepsize=40), note that the villain's registration point is placed on the left foot. After you finish painting, in the movie gallery, right-click the Player and select For action at the foot of this export ", other not selected." identifiers, and the As 2.0 class is the movie clip name, "Player." Be aware that capitalization is not a mistake, otherwise there will be an error.
  12. Create "box" and "Target" clips in sequence, as described above.
  13. Press 10 to create a clip named "Brick", but do not add "as 2.0" because it does not require external script files.
  14. Press 10 to create a "Wall" clip, slightly different, in the edit state, add "stop" in the first frame, and a "Brick" clip in the second frame. This is done because "Wall" is required to use "Brick" in the construction.
  15. Press 12 to create a "Manager" clip, which is different in that it needs to be "exported in first frame", and the second frame is not "Brick", but "Wall", "Player", "box", "Target".
  16. All right, we're ready. In the first frame on the main timeline, enter the statement: Attachmovie ("Manager", "Game1", _root.getnexthighestdepth ()); Game1.init (1); You can run the first turn.
  17. As for how to build a friendly user interface, we need to take a little bit of effort to get it done.

Example:

Click here to download the source file

If you still have any do not understand place, can send mail or QQ to me. xiake1860@ncepubj.edu.cn qq:407713 0



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.