Complete your enemy MC in Flash mx2004, and then write "baddie" in the logo text box in the Link dialog box, and write "enemy" in the as2 text box ".
Now write the following script in your FLA interaction panel, connect the enemy MC to the scene, and put them in the enemyarray array:
(Translator's note: It seems that the array of as2 is the same as that of AS1 (and JavaScript). It can be used for anything. It is quite helpful here .)
// ------ Connect to the enemy array ---------//
For (var j = 0; j <3; j ++ ){
Attachmovie ("baddie", "baddie" + J, 200 + J );
Enarray [J] = _ root ["baddie" + J];
Enarray [J]. _ x = 50 * J;
Enarray [J]. _ y = 100;
}
This is what we have written so far in our enemy class:
Class enemy extends movieclip {
// Write the implementation code here
} // ------- End of the enemy class
Mobile enemy class
No, so what do we need? How about some variables.
Let's go back to the previous class table to see what we can design in the document.
Okay, we need speed, so I put it in, and we need a moving method.
Oh, there are some things I forgot in the Design. A reset () method is used to remove the enemy planes from the left of the scene or return them back to the right of the scene when they are stabbed.
We can set the initial speed in the constructor. This is what I have done so far:
Class enemy extends movieclip {
// Declare attributes
Private var speed: number;
// ======= Constructor
Function enemy (){
Speed = 20;
}
// ===== Move
Function moveenemy (){
If (_ x <=-30) {// when the scene is removed from the left
Reset ();
}
Else {
_ X-= speed;
}
}
// ======= Resetting
Function reset ()
{
_ X = stage. Width + 50;
_ Y = math. Random () * 380 + 10;
Speed = math. Random () * 10 + 10;
}
} // ----- Enemy class ends
Test it by compiling.
Aha! Make up for it. The enemy plane starts to act at a speed of 10-20 pixels/frame in the X direction.
Oh, then we will put our machine into the game class, so we will put the following code into the game class.
// In the game class
// Move the enemy
Function movebaddies (){
For (VAR I = 0; I <3; I ++ ){
Enemyarray [I]. moveenemy ();
}
}
And confirm that you have the movebaddies method in the enterframe loop in your flash file, which makes the code in your action Panel very neat and clean.
// Loop
_ Root. onenterframe = function (){
Mygame. checkkey ();
Mygame. moveship (DIR );
Mygame. movelaser ();
Mygame. movebaddies ();
}
Collision Detection
I am not very clear about how I will implement it, maybe in the game class.
And do some loop...
// In the game class
// Collision Detection
Function collision (){
For (var j = 0; j <6; j ++ ){
For (var k = 0; k <3; k ++ ){
If (laserarray [J]. hittest (enemyarray [k]) {
Trace ("enemy hit, no." + k );
}
}
}
}
And put the method here into the enterframe event in our FLA file:
// Loop:
_ Root. onenterframe = function (){
Mygame. checkkey ();
Mygame. moveship (DIR );
Mygame. movelaser ();
Mygame. movebaddies ();
Mygame. Collision ();
}
Create an explosion. When a bullet hits an enemy plane, we want to see the explosion (of course ).
Create an explosive MC in your Flash MX 2004 file.
I just gathered smoke and fog images in each frame, added a blank frame at the end, and a stop ().
I named this MC "splode_mc" and called it "splode" in the Link dialog box ".
So in the magnetic collision method, I should connect to the MC splode, reset the enemy machine, and then add the score.
// In the game class
// Collision Detection
Function collision (){
For (var j = 0; j <6; j ++ ){
For (var k = 0; k <3; k ++ ){
If (laserarray [J]. hittest (enemyarray [k]) {
// Explosive effect
_ Root. attachmovie ("splode", "splode", 301 );
// Place the explosion on the enemy's plane
_ Root. splode. _ x = enemyarray [K]. _ x;
_ Root. splode. _ y = enemyarray [K]. _ y;
// Re-set the enemy plane to the right of the scenario
Enemyarray [K]. Reset ();
Break;
}
}
}
}
Collision Detection: collision detection by an enemy plane.
Now let's take a look at the collision between the enemy and me and our spacecraft.
I reuse sploe_mc and changed its color and stretch value in the X and Y directions,
We can also add the effect of a sound to the determination of each hit.
But I will give it to you.
// Collision Detection
Function collision (){
For (var j = 0; j <6; j ++ ){
For (var k = 0; k <3; k ++ ){
// Check whether a bullet hits the enemy
If (laserarray [J]. hittest (enemyarray [k]) {
//// Explosive effect
_ Root. attachmovie ("splode", "splode", 301 );
// Place the explosion on the enemy's plane
_ Root. splode. _ x = enemyarray [K]. _ x;
_ Root. splode. _ y = enemyarray [K]. _ y;
// Re-set the enemy plane to the right of the scenario
Enemyarray [K]. Reset ();
} // End if
// Check whether the enemy has hit the ship
If (enemyarray [K]. hittest (SHIP )){
_ Root. attachmovie ("splode", "splode", 301 );
// Match the plane coordinates for the explosion
_ Root. splode. _ x = ship. _ x;
_ Root. splode. _ y = ship. _ y;
// Change the color
VaR my_color: color = new color (_ root. splode );
My_color.setrgb (0xff0000 );
// Change the size
_ Root. splode. _ XScale = _ root. splode. _ yscale = 400;
}
}
}
}
Score and destiny value:
What is the rest?
Scores are taken as life values. I may need to make some text boxes to change their text attributes when changes occur.
Therefore, make two text boxes in the Flash MX 2004 file and name them score_txt and lives_txt respectively.
Then add two variables to the game class, score and lives, and initialize them in our constructor.
// In the game class
// Declare Variables
VaR score: number;
VaR lives: number;
// ===== Constructor ======================
Function game (_ ship: Spaceship, _ enarray: array,
_ Bullarray: array ){
Ship = _ ship;
Enemyarray = _ enarray;
Laserarray = _ bullarray;
// Initialize the bullet and Life Value
Score = 0;
Lives = 10;
}
Now let's set the score and lives variables to textfields.
// In the game class
// Collision Detection
Function collision (){
For (var j = 0; j <6; j ++ ){
For (var k = 0; k <3; k ++ ){
// Check whether a bullet hits the enemy
If (laserarray [J]. hittest (enemyarray [k]) {
// Add a score value and present it
Score + = 10;
_ Root. score_txt.text = "score:" + score;
// Explosive effect
_ Root. attachmovie ("splode", "splode", 301 );
// Place the explosion on the enemy's plane
_ Root. splode. _ x = enemyarray [K]. _ x;
_ Root. splode. _ y = enemyarray [K]. _ y;
// Re-set the enemy plane to the right of the scenario
Enemyarray [K]. Reset ();
} // End if
// Check whether the enemy has hit the ship
If (enemyarray [K]. hittest (SHIP )){
// Reset the enemy plane to reduce the life value
// Display the lifecycle
Enemyarray [K]. Reset ();
Lives-= 1;
_ Root. lives_txt.text = "Lives:" + lives;
_ Root. attachmovie ("splode", "splode", 301 );
// Match the plane coordinates for the explosion
_ Root. splode. _ x = ship. _ x;
_ Root. splode. _ y = ship. _ y;
// Change the color
VaR my_color: color = new color (_ root. splode );
My_color.setrgb (0xff0000 );
// Change the size
_ Root. splode. _ XScale = _ root. splode. _ yscale = 400;
}
}
}
}
In the above Code, when an enemy plane hits a ship, I have to reset the enemy plane. As the collision lasts several frames, the number of lives will be continuously reduced.
Comment out one line of enemyarray [K]. Reset () and you will see a continuous reduction in the number of lives.
The above is a fairly simple process:
1. Collision Detection.
2. Increase or decrease the variable value.
3. The variable value is changed.
In terms of technology, the speech is a little heavy jump. I think I will put it there for a while for us to digest it.
Next, we will do a way to check the scores and determine whether the scores and lifecycles are out of bounds. If so, we will end the game.
But here we are. The following considerations are the same as the first part...
Next we will go to the third part, where we will organize a way to present scores and create a background for horizontal movement.
To be continued...