Flash makes a wonderful air combat game

Source: Internet
Author: User
Tags exit cos count explode insert reset variable

More and more flash enthusiasts have not satisfied just to appreciate other people's production of Flash, but hope that they can also be put into the ranks of flash producers, become a flash master. Today, we use a game example to let the vast number of flash enthusiasts to understand the flash in the production of the game's basic ideas and methods, the end of the article provided. fla original file download.

Today we are going to do is a fighter attack enemy aircraft game, start the game, click the "Start" button, the game begins. Keyboard key to control the fighter jets, press the space bar fired bullets to destroy enemy aircraft, each destroyed a fighter will be added to the appropriate scores, if the score is greater than 500, will win the game completed. In the game to control fighter aircraft can not be fired by the artillery shells hit, nor with the flying enemy collision, otherwise each collision or hit will reduce the life of the fighter, life value is less than 0 o'clock, the game failed, back to the original screen.

Below we separately from the idea, the component makes, the code control several aspects introduced this small game the production process.

  Functions and methods used

The idea of this game is derived from the HitTest method in Flash, HitTest method is mainly used to detect the overlap of "space", it has two methods:

Mymovieclip.hittest (X,y,shapeflag);
Mymovieclip.hittest (target);

  mymovieclip.hittest (X,y,shapeflag): Compares the x and Y coordinates with the Mymovieclip entity, Shapeflag can take two values true and false, then X, A function returns true if the Y coordinate overlaps any point of the Mymovieclip entity (excluding the boundary), or false. If the Shapeflag value is False, the function returns True if the x,y coordinates overlap any point of the Mymovieclip entity (including the boundary), otherwise it returns false.

  mymovieclip.hittest (target): compares the entity represented by the target and the mymovieclip entity to overlap, and the intersection function returns True, otherwise it returns false. This method will be used in the game.

There are several more important statements in the game: the Duplicatemovieclip method and the Duplicatemovieclip method and the Duplicatemovieclip method.

Duplicatemovieclip method
Duplicatemovieclip method
Duplicatemovieclip method

  The Duplicatemovieclip method is to make a copy of a movie clip entity in the home scene (or in another movie clip), which will be placed on its source entity with a depth value (an object with a lower depth value).

  The Removemovieclip method is to delete the copy of the movie clip entity, and you cannot delete the entity that was placed manually on the stage during the clip.

  The Attachmovie method is almost exactly the same as the Duplicatemovieclip method, but it does not need to place an entity on the stage altogether, and you can remove a movie clip entity directly from the library. In this case, the bullet from the fighter is achieved by this method, and the bullets emitted by the enemy aircraft are placed in advance on the stage a clip, that is, with Duplicatemovieclip to copy.

  second, the production of components

1. Create a new scene "Scenario 1", select the "document" option on the "Modify" menu, and change the background color to black in the pop-up Document properties dialog box, with the default 550*400 and frame frequency fps.

2. Create a film clip (hereinafter referred to as MC), named "Plane", which is used as a control fighter. Inserts a keyframe in the first frame of the movie clip, draws an airplane shape in the editing area, or inserts an airplane vector map. The frame action for the first frame is "Stop ()". Inserts a blank keyframe at the second frame, drawing a rectangle that is much smaller than a fighter. Then insert a blank keyframe in frame sixth, draw an exploded graphic, and set the tween of the second frame to the sixth frame in the property panel as "shape," the frame action for frame six is "gotoAndPlay (1)".

3. Create a new MC element "Fire", the first frame is a blank key frame, the second frame inserts a key frame, draws an ellipse as "plane" to fire the projectile, the frame action is "stop ()".

4. Create a new MC component "Bullet", drag the "fire" clip into the center of the edit area, the clip name is "Fire", and the Frame Action "stop ()". When the clip is edited, its options appear in the movie library, right-click the "Bullet" option, select "Link" in the pop-up shortcut menu, the identifier is "laser", and tick "output for action script."

This is the key step, because to use Attachmovie call library clips for replication, without this step, Attachmovie can not copy clips in the library, the game run "plane" will not be able to emit artillery fire. Then set the action code for "Fire" in the bullet clip:

Onclipevent (load) {//action occurs during load
N=_root.enemynumber; The number of enemy aircraft in the picture is assigned to n
speed=10//speed Initial value is 0
}
Onclipevent (enterframe) {//occurs when entering frames
Array ()
for (i=0;i<n;i++) {
if (_root["new" +i].ship1._currentframe==1) {//The enemy plane in the screen is not in its first frame
if (This.hittest (_root["new" +i))//Determine if the clip is overlapping with the enemy aircraft copied from the screen
{_root.score+=10//if overlap, life value plus 10
_root["New" +i].ship1.gotoandplay (2); Enemy bombing
Removemovieclip (_parent);//delete clips from scene
}
}
}
if (_parent._x<=550) {//If the X coordinate is less than the speed, the x coordinates plus the value of the variable
_parent._x + = speed;
else {//If the x-coordinate is greater than 550, delete the clip
Removemovieclip (_parent);
}

}

6. New MC named "Explode". Draw a square in the first frame, then insert a blank keyframe in frame fifth, draw an exploded figure with the pencil tool in the frame, then go back to the first frame and set the tween in the property panel to frame fifth as the shape. Finally, insert a blank keyframe in frame sixth, which sets the command "Stop ()". As shown in the following figure

7. Establish MC Element "Enemyplane". Draw an enemy plane in the first frame, set the command "Stop ()" On the timeline, insert a blank keyframe in the second frame, drag the explode clip into the Enemyplane clip's original position, and set the Stop () in the second frame of the timeline.

8. New MC symbol "enemy", drag the Enemyplane component into the first frame, the clip name is SHIP1, and the frame action is stop ().

9. Next to do the enemy's artillery shells, the new MC element "Ball", in the editing area to draw an ellipse, the size of the appropriate, because this is used as a game in the shells emitted.

10. Finally, create a new two button symbol, one "start" and the other "exit".

   three , code control

1. Return to the main scene, rename the first layer to "start", insert the keyframe in the first frame, and the frame action:

function Clean () {
For (i in _root) {
_root[i].removemovieclip ();
}
}
_root.clean ();
Stop ();

The clean function is used to remove all unnecessary movie clips, which have no function at the first run, and when the game is run, the "Plane" life is zero to return the frame, and if there is no clean function, there will be countless copied movie clips.

2. Drag the "Start" button and the "Exit" button into the scene, the "Start" button action script is

On (release) {//Free mouse to enter the third frame, start the game
gotoAndPlay (3);
}

The action script for the exit button is

On (release) {//Free mouse, exit game
Fscommand ("Quit", "1");
}

3. Add a second layer, named "Victor", in the second frame of the layer insert keyframes, in the scene with the text tool to write "You win", set the font size of 84, color yellow. Also insert the "Start" and "Exit" buttons, the action script ibid.

4. Add a third layer, named "Action," insert the keyframe at the third frame of the layer and write the code as follows:

Enemynumber=5//Every time there are five enemies in the picture
for (i=0;i<enemynumber;i++) {////If the number of enemies in the screen is less than five, copy the enemy movie clips
Duplicatemovieclip ("Enemy", "new" +i,30+i)
}
LIFE=100//Life value is 100
score=0; Start with 0.
Stop ();

5. Add two static text boxes and two dynamic text boxes in the frame scene to represent life values and scores. The contents of the Static text box are "life" and "score", the first dynamic text box life variable value, the second dynamic text box variable value score.

6. Add two more layers, one named "Plane", one named "Enemy", and insert keyframes in the third frame respectively. Select the third frame of the "plane" layer and drag "plane" into the clip name "ship". Select the third frame of the enemy layer and drag the "enemy" and "ball" clips into the clip name "enemy" and "Enemylaser" respectively.

Right-click the "Plane" clip and write the code in the Action panel:

Onclipevent (load) {//load is initialized
speed=6;
sy=_y;
ang=0;
}

Onclipevent (enterframe) {
_y = sy + 4 * math.cos (ang+=0.092); A COS letter can be an airplane with the effect of flapping in the air
C=_root.        Score Assign a value to a C variable with the initial status of "0"
if (c>=500) {//If the value of C (score) is greater than 500, then victory over the game
_root.gotoandstop (2);
}
if (_root.life<=0) {//If the life value is less than 0, restart the game or exit
_root.gotoandstop (1);

}

When the player presses the top, bottom, left, RIGHT arrow keys, the plane can move

if (Key.isdown (Key.down) and _y<400) {_y = = speed; Sy+=speed}
if (Key.isdown (key.up) and _y>0) {_y-= speed; Sy-=speed}
if (Key.isdown (Key. right) and _x<550) {_x + = speed;}
if (Key.isdown (key.left) and _x>0) {_x-= speed;}
if (Key.isdown (key.space)) {//When the space bar is pressed, the aircraft opened fire, using a variable A, so that every click of the space issued a cannonball, if not set this variable, then when the space bar is pressed, the shells will continue to fly out.
if (!a) {//If A is false
shotcount++;
Depth= (shotcount%100) +100
_root.attachmovie ("Laser", "Sparo" +shotcount, depth); The Attachmovie method is to invoke the copy movie clip "Fire" directly from the library, and its identifier is "laser".
_root["Sparo" +shotcount]._x = this._x+ (_width-45)
_root["Sparo" +shotcount]._y = this._y;         The initial position of the copied "fire" clip. A = true;

}
else {a = false;}
}

7. Right-click the "enemy" clip and set the code in the Actions panel

Onclipevent (load) {
Sy=random (400) +10; Take random numbers
function Reset () {
Ship1.gotoandstop (1);
Speed=random (10) +2; The flight speed of an enemy aircraft is a random number
_y= random (+_height); The initial y-coordinates of the enemy aircraft take random numbers
_x= 550+_y;
_root.firepower=100;

}
Reset ()
}

Onclipevent (enterframe) {
if (this.ship1._currentframe==1) {//fighter clips in the first frame
_y = sy + speed * Math.Cos (speed+=0.032); There is a floating effect when the enemy aircraft fly
if (This.hittest (_root.ship)) {//If the clip collides with a fighter
Ship1.gotoandstop (2); Enemy bombing
_root.ship.play ();
_root.life-=10; Life value minus 10
}
if (random (_root.firepower) +1==10) {
count++;
Depth= (count%100) +200;//artillery shells emitted when flying aircraft
Duplicatemovieclip (_root.enemylaser, "num" +count, depth);
The initial coordinates of the projectile are the coordinates of the enemy's current position.
_root["num" +count]._x=this._x-40
_root["num" +count]._y=this._y
}
}
_x-=speed
if (_x<-10) {Reset ()}

}

8. Right-click the "Ball" clip and set the code in the Actions panel

  

Onclipevent (load) {
speed=15;
}
Onclipevent (enterframe) {
if (!_root.shipdead) {//If the fighter has a life value greater than 0
if (This.hittest (_root.ship)) {//artillery shells from enemy planes hit fighter jets
_root.ship.play ();
_root.life-=5; Life value minus 5
Removemovieclip (This)
}
_x-=speed;
if (_x<-10) {Removemovieclip (This)}
}
}

  Iv. Summary

At this point, the whole game is complete. In the process above, in order to explain as concise as possible, I did not talk about the process of inserting sound, in fact, the insertion of the method is very simple, as long as the appropriate sound in the appropriate frame can be.

In fact, the use of flash to make such a simple game is not difficult, as long as we can flexibly use the method or function in Flash, you will be able to write more outstanding works. Click here to download the source file



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.