The author's own level should be very high, but from the game mode, this game does not have too many innovations, still infinitely complex, but the idea is quite complete, there is such a good development Habits, I want to make a big stuff, I will not start. The author writes this article's main purpose is to introduce you how to write games with object-oriented AS2, so from this point of view, this article is still quite excellent, especially in the domestic data. At this time, this article can be described as the best in the snow. I translate it, on the one hand, I have improved, another aspect, and I will give the majority of Flasher to do some contributions ~~~~ (article has been included in the article , You can get the next source code http://www2.flash8.net/teach/2715.htm) (the first time to turn out the country, there is a place where you don't have authentic, please "right, if Author I sent you an email to say that I want to find Emilmatthew to collect Chinese version of the copyright fee, you don't know me, thank you ~~~ :))
This document is the first AS2 game written by OOP (object-oriented programming) method. This standard space combats the enemy's game, there are three enemy machines and a player's nozzle. There is only one level attempt, but it is designed to make the possibility of increased probability. There will be basic scores and health of the game. Like other people who are struggling to implement new ActionScript2.0 grammar in Flash MX2004, this document is also the benefit of my personal learning. I will follow the structure of a reasonable traditional OOP design. Thank you very much and respect Squize from http://www.1coinplay.com, let me use his little fake paper (?) As this example.
1 requires a document. 2 Use an event. 3 types of forms. 4 interacting form. 5 Test the initial code. 6 Iteration 7 Test. 8 released.
*********************************************************** ********************************** 1 Requirements: SpaceShip: Move up and down. Emunction of laser by a spacebar.
Enemy: Move from right to left in a random height. There are three examples. When it shifts the screen from the left, return to the right.
Background: moves from left to right. The foreground is fast than background movement (parallax))
Laser: When the space button is pressed out of the spacecraft.
GAME: If shooting to the enemy, the player gets the score. If the spacecraft hits the enemy, it will lose the value of life. If the player gets the score of victory, that he (she) will win, the game will start. If the loss of the loss is more than three, the game is over, the player failed.
2 Events used: The event used is to use the chart to represent (design) requirements. They show different events that occur in the game, or all different actions that will occur.
This is what you have to do. Just just implement them on a piece of paper (NUT?), This form can be good for the class and method that will eventually use. When you have finished these, what kind of property and method you want to use and what kind of properties you need to do.
Class form: The table of class shows the relationship between classes. In our table below: spacecraft, enemies, laser and background classes are included in the game class. This means that the entities of those classes are declared in the game class. This is called the inheritance of the "one layer" relationship, which is called "is a" relationship. ? The game class is gathered into my Flash file.
Description of Class: Cosmic Boat Properties or Method Type Description Speed Number X Variable Increment MOVESHIP () Method With Button to Maxi SetShipx (X) Setter_x Variable Get Value Getshipx () getter Return _x Value setshipy (Y ) SETTER _Y Variable Get Value Getshipy () getter Returns _Y Value Enemy Class Properties or Method Type Description Enspeed Number X Variables Moven () Method Random Mobile Enemy SetENX (X) Setter_x variable Get value Method GeTenx () getter returns _X value seteny (y) SETER _Y variable Getting value method geleny () getter returns _y value
Laser Class Attributes or Method Type Description Fire () Method Start When the space button is pressed, Method Getlasery () getter returns _x value setLasery (Y) setter _y variable GetLasery () Getter returns _y value
Background Class Properties or Method Type Description Scroll () Method removes from right to left setbackx (x) setter _x variable GetBackX () getter returns _X value setbacky (y) setter _y variable GetBacky () getter Return _Y value
Game Class Properties or Method Type Description SCORE NUMBER Gamer's score Lives Number player's Life CHECKCOLLISISONS () Method Collision Checklives (Lives) Method If the health is 0, stop the game. Checkscore () Method detection and display score
The interactive table interactive table describes the information passed by the method to meet the requirements raised at the beginning. I now have no concepts for this plan, because I don't know if Flash can handle a combination. Game classes contain other classes and are managers of the game. But I don't know if I can control a subclass (instance) inherited in the MovieClip class in a class. Well, here is this plan, let us look at it can work. The plan is not placed in the concrete. If they are invalid, you can throw them away, or modify, even from the draft. The most important thing is the process. Clear problems through the process and presented the way to handle the problem. This is like giving a movie's episode. You draft it, the idea of presenting scripts is also aimed at the needs of the audience. Once you have done this, you will throw it on the side and use the instinct to carry out the director. You know the background of the story, because you browse the process of the plurality of tunnels. The same is true here:
The above is a table of interactive interactions. I even made some changes and realized some content missions in the class form. Just like that Moveship method requires a direction (left, right, upper, bottom) and I can pass a variable DIR to pass which key is pressed. DIR can be a String class ("Left") or a letter (1, 2, 3, 4). This is not important.
Test priority: I created a new Flash file and created a spaceship_mc (MC) to connect it to the SHIP_MC of the library and put the "SpaceESHIP" into the connection (LINK) AS2 Class option. Then I wrote the following code in the interactive panel: // Connect SpaceShip_MC AttachMovie ("Ship_MC", "Myship", getNexThigHestDepth ()); // Declared Variable Var EnaRray: Array; // Now is empty VAR BulletArray: Array; // empty var mygame = new game (myship, entorage, bulletarray); // Game cycle _root.onterframe = function () {mygame.moveship ("right");} The I wrote minimal code so what the the the Classes Would Just Compile The minimum amount of code is to enable it to make it just compile the class game {// game class, control the entire game and contain an instance of other classes. // Declare Var Ship: Spaceship; Var EnemyArray: Array; Var LaseraRray: Array; (Translator Note: It is best to use private to indicate, otherwise there is no package "Function Game (_ship: spacesosshi)) // Constructor Function Game _naarray: array, _bullarray: array) {ship = _ship; enemyArray = _naarray; laserarray = _bullarray;} (Translator Note: This function is best in front of the head plus PUBLIC): VoID, this is quormed. )} Then write in the spaceship.as file: Class Spaceeship Extends movieclip {
} Then compile, but there is no error. But there should be some errors because the MoveShiP method has not yet appeared? Ok? ? (Translator Note: I'm fell) I didn't appear in debugging. Quite annoying error check. . . Oh alright. . . Now I have to write the Game.MoveShip method. . . I will write it and test it. . . I wrote the Moveship method and realized that I have to write a CHEKCKEY method that can detect which key is pressed and transmitted back to a SRING class. // Game class // detection button method: function checkkey (): String {if (key.right)) {dir = "right";} if (key.Ift) {DIR = "Left";} if (key.isdown (key.up)) {DIR = "UP";} if (key.down (key.down)) {DIR = "down";} returnide;} // MOVESHIP Method // 1. Check button // 2. Allocate variable DIR is transferring DIR to spaceship (dir: string) {ship.move (dir); Got mistakes. . . "Row 38: There is no 'move' method." Great, start to get the wrong, (the translator's note, I will fall back), it is what the thing you want to happen. Now write the Ship.Move method to clear the error. . . That is the idea of "test priority" (the author writes this: Mainly, it is a small Dongdong, and the main purpose is to try AS2 programming. When you do the project yourself, what model is used, please take care of yourself. ) Implement the method in a range of tests until they have problems, then write methods until it meets the next method. I make the spaceship, this is what I did. . . Class game {// Declared Variable Var Ship: SpaceShip; Var EnemyArray: Array; Var LaseraRray: Array; var Dir: string; // ==== Constructor ======================= Function Game (_ship: spaceship, _naarray: array) {ship = _ship; enemyArray = _Narray; laseraray = _bullarray;} // ==== Check button method ======= function checkKey (): String {if (key.right)) {dir = "right";} else if (key.LEFT)) {DIR = "left";} else if (key.isdown Up) {DIR = "Up";} else if (key.down)) {DIR = "down";} else dir = "stop"; // This role in this role (DIR) "- in game.checkey");
Return Dir;} // === Method of mobile spacecraft ===== // 1. Check button // 2. Assignment to the variable DIR corresponding to the corresponding direction // 3 Transfer variable DIR in the spaceship.move method Function Moveship (Dir: String) {Ship.move ());}} // Game class ends in the spaceship class:
Class spaceship extends movieclip {var dir: string; function move (_dir: string) {DIR = _dir; if (dir == "left") _X - = 20; if (DIR == "Right") _ x = 20; IF (DIR == "UP") _y - = 20; if (DIR == "Down") _y = 20; if (DIR == "STOP") {_X = 0; _Y = 0;}} } // spaceship class ends I didn't encounter any trouble when I passed the variable DIR into the ship.move method, and then I tried to use the CheckKey method directly because it always returned DIR. Of course, this is getting. Then I had to add a stop in it ... Whenever the button is pressed, there are some comic times, so the state has been holding, until I will make the movement gentle .. But I know that my design works. I just want to pass the instance of the spaceship (SpaceShip) to the constructor of the game class. Then, haha, PRESTO (the class is on the sesame and other words) ... What is the next? Well, maybe it is a laser bullet. . . Ok, let us go.
I launched a thundering bullet that I spent two days, I almost could hardly believe it. I have continued to launch the wrong bullet. . . Here is the first attempt I transmit laser bullet method. // In the GAME class: / / ====== transmit laser bullet ======== function FireLaser_old () {if (key.isdown (key.space)) {// set start PT of Laser Laserarray [bulletnum] ._ y = ship.gety (); laserarray [bulletnum] ._ x = ship.getx (); // if space bar press set flag Hasfired = true; bulletnum ; if (bulletnum> = 5) {bulletnum = 0;}}} The above code is quite close to the state of my last end, the most practical problem is that the MoveLaser method. Ohhaha, I launched the laser bullet in my ENTERFRAME loop in my FLA ... this must be placed in the ONKEYDOWN event of the FLA file. This is the situation so far:
// AttachMovie ("Ship_MC", "Myship", getNexThigHestDepth ()) in the Actions panel of FLA file; var enaRray = new array (5); // Connect Bullet array for (VAR) i = 0; i <5; i ) {attachmovie ("laser_mc", "laser" i, 100 i); bulletArray [i] = _root ["Laser" i];} // declaration and initialize the game class Entity // Put the spacecraft, enemy array and bullet array into var mygame = new game (myship, entorage, bulletArray); / / ======= Get key value ========== == // somelistener = new object (); somelistener.onkeydown = function () {mygame.fireLaser ();}; key.addlistener (somelistener);
// Game cycle _root.onterframe = function () {mygame.checkKey (); mygame.moveship (dir); mygame.movelaser ();} This is better, but still does not get the correct bullet emission. . . It is delayed by two keys than the space bar. Well, ok, this is my MoveLaser method I used this time: / / ========= move laser =========== // function moveraser_old () {if (Hasfired == True) {laserarray [bulletnum] ._ x = 30;} if (laserarray [bulletnum] ._ x> stage.width) {laserarray [bulletnum] ._ x = -10; HASFIRED = false;}} // ==== ========== //
Below is the laser bullet emission method used in ultimate (now). It works very simple to get the position of the aircraft and pass to the bullet. Then the number of bullets began to increase, when the number is greater than 5, it is reinited to 0. / / ========= Fire laser ====== // function fiber () {if (key.sdown (key.space)) {// set start point laseray [bulletnum] ._ y = Ship.gety (); laserarray [bulletnum] ._ x = ship.getx (); // increment the bullet number bulletnum; // if more Than 5 Bullets, Start Again At 0 IF (Bulletnum> 5) {BulletNum = 0;}}} The following is the method of the final moving bullet, which is also very simple. This process traverses all bullets, if there is a bullet, you can move it. It is still not very perfect, but now you can use it. / / ======= move laser ======= // function moveser () {var Bulleti = 0; While (Bulleti <6) {laserarray [Bulleti] ._ x = 30; Bulleti ;} } // ============== // The following is the game class created so far:
Class game {// ========= Declared Variable VAR Ship: Spaceship; Var EnemyArray: Array; var Dir: string; var bulletnum: Number = 0; // ==== Construction Function =================================================================================================================================================== Method of detection key is pressed ======= function checkkey (): string {ife (key.Ig (key.right)) {dir = "right";} else if (key.Ift " )) {Dir = "left";} else if (key.undown (key.up)) {dir = "UP";} else if (key.down) {dir = "down"; Else Dir = "stop"; return dir;}
/ / === Method of mobile spacecraft ===== // 1. Check button // 2. Assignment to variable DIR The corresponding direction // 3 Pipe the value of the variable DIR into the spaceship.move method Function MoveShip (DIR) : String) {ship.move (CheckKey ());} // ========= Launch bullet ======= // function fireland () {i (key.isdown (key.space) ) {// Set the start position laserarray [BulletNum] ._ y = ship.gety (); laserarray [bulletnum] ._ x = ship.getx (); // increase the number of bullets BulletNum; // If it is more than 5, re-re- Put the number at 0 if (Bulletnum> 5) {bulletnum = 0;}}} // ======= Mobile bullet ======= // function moveiler () {var Bulleti = 0; while (Bulleti <6) {laserarray [Bulleti] ._ x = 30; Bulleti ;}} // ====================== //} // ------------------ ----- / / below is the spaceship class and the getx and gety methods it use:
Class spaceship extends movieclip {var dir: string; // ===== Mobile ========= function move (_dir: string) {dir = _dir; if (DIR == "left") _x - = 20; if (DIR == "Right") _ x = 20; if (DIR == "UP") _y - = 20; if (DIR == "DOWN") _y = 20; if (DIR == "stop") {_x = 0; _y = 0;}} // ====== Get x value ==== function getX (): Number {return_x;} // ===== == Get y value ===== function getY (): Number {return_y;}} // ==== Spaceeship class end
Here is the Laser class:
Class Laser Extends MovieClip {// ======= Constructor ========== // Function Laser (x: Number, Y: Number) {_x = x; _y = y;}} Source code from now:
Let us enter the second part of this article.