An AS2 aircraft tutorial in foreign countries (3)

xiaoxiao2021-03-06  41

Development documentation of a Flash AS2 OOP game --- Part III

Now we have to check our number of life and certainly do it.

Troubles the number of life:

Create two new frames in your FLA document and put the code of the first frame into the second frame F. In Your Fla, Make 2 New Frames and Put The Code That Was On Frame 1 ONTO FRAME 2. Add a STOP (); writing at the beginning of the code, adding in the first frame and third frame, respectively. In the first frame, add a title, some instructions and a button. Write down the following code: On (Release) {_root.gotoAndStop (2);} Add a dynamic text box in the third frame to present the results in the game, and name the instance name "Result_txt" Add a restart button and press the code as follows in New Years, so that it returns the second frame. On (Release) {_Root.gotoAndStop (2);

Return to our code in our game class and add this method to check how much life remaining and processing:

// In the GAME class / / ============ Check the life value ===================================================================================================================================================================== 0) {// Wipe screen // Re-move MC object Ship orremoveMovieClip (); for (var i = 0; i

Class Background {// ----- Initialization Background --- // Function InitBack () {_Root.attachmovie ("Sky", "Sky", 1); _Root.attachmovie ("FAR", "FAR", 2 _Root.attachmovie ("MID", "MID", 3); _Root.attachmovie ("Fore", "Fore", 4);} // ========== Mobile background === = // function moveback () {// SKY mobile IF (_root.sky._x <= -stage.width) {_root.sky._x = 0;} _root.sky._x - = 2; // FAR foreground mobile IF (_root.far._x <= -stage.far._x = 0;} _root.far._x - = 4; // MID mobile IF (_root.mid._x <= -stage.width ) {_Root.mid._x = 0;} _root.mid._x - = 6; // fore mobile IF (_root.Fore._x <= -stage.width) {_root.Fore._x = 0;} _root. FORE._X - = 8;} // // === Erase background === // function cleanback () {_root.sky.removemovieClip (); _root.far.removemovieClip (); _root.mid.removemovieClip (_root.mid.removemovieClip) ); _Root.Fore.RemovemovieClip ();}} Note I have three ways: initback () - here I load the background clip to moveback () - mobile background object Cleanback () with different speeds () - When the number of lives is 0, remove all background objects. Increase the Back variable in the Game class and change the constructor into the background object entity.

// In Game // Declare Variable Var Back: Background; // ... Constructor Function Game (_ship: spaceship, _naarray: array, _bullarray: array, _back: background) {ship = _ship; enemyArray = _narray; laseray = _narray; laseray = _bullarray; back = _BACK; score = 0; lives = 10;} Now we need to initialize our background. In the GAME class, in the initGame method, initialization background: // In Game // --- Initialization --------- // Function initGame () {back.initback ();} then Move it:

// In Game // == Mobile background ===== // function movebackground () {back.moveback ();

// In our FLA // game time _root.onterframe = function () {mygame.checkKey (); mygame.moveship (dir); mygame.movelaser (); mygame.movebaddies (); mygame.collision ); Mygame.checklives (); mygame.movebackground ();} fixed our score and rendered. Everything is doing very well, but we don't present the scores and life pulse numbers. That's because our text box is in the Root layer, while the background is at 1-4 layers, so the root layer is overwritten. So we have to do a dynamic detection now and put them in a higher level. Put this in the game class where we declare the variables: var myformat: textFormat; this will make us get our text box behind our text, and add dynamic text boxes in the initgame (initial game): // In the GAME class // --- Initialization ----------- // function initGame () {back.initback (); // Create a text box and set the format _root.createtextfield "score_txt", 501, 20, 10, 100, 50); myformat = new textFormat (); myformat.color = 0xff0000; myFormat.Size = 20; _root.score_txt.text = "score:" score; _root.score_txt.setTextFormat (MyFormat); // Initialize text boxes showing health _root.createtextfield ("lives_txt", 502, 600, 10, 100, 30); _root.lives_txt.text = "Lives:" lives; _root.lives_txt.setTextFormat (MyFormat);} Delete the Guangxi text box we put in the scene and perform bullets and enemy machines and collision detection between enemy machines and hosts. // In the GAME class // collision detection ... // bullet hit the enemy machine score = 10; _root.score_txt.text = "score:" score; _root.score_txt.setTextFormat (MyFormat); //. ..// enemy machine hits the spaceship lives - = 1; _root.lives_txt.text = "Lives:" lives; _root.lives_txt.setTextFormat (MyFormat); // ...

Wipe When the game is over, we still need to remove the scene from the game scene. So I did a new way to do this:

// In the background class / / === erase background === // function cleanback () {_root.sky.removemovieclip (); _root.far.removemovieClip (); _root.mid.removemovieClip (); _root. Fore.removemovieClip ();} Now add this line checklives () method to the GAME class: // In the Game class // In the checklives method IF (lives <= 0) {// ... back.cleanback (); // ...}

There is still something left: This game is far from finishing. For me, this is a programming exercise and I ignore a lot of game sexual factors. Visual trial also has the effect of the beautiful angle and something else. This is something that can be added: the sound - bomb shooting, collision. More off, larger and worse enemies (Translated note: The author is "worse" here is Badder ~~~~) a maximum transcript form writes the code to the game in the game. Some other things? Just think about your imagination. Please download the following source file, I only add the second frame of code in FLA, and the entire file is too big. Also continue? ? ? (Translator Note: Ok, finally is finished, ready to review English, goal: CET-4 more ...): The author's original text and the internal part of my translation, is there something to throw? I flash ~~~

转载请注明原文地址:https://www.9cbs.com/read-68993.html

New Post(0)