Now we will add an option for spaceship so that it can shoot and fire bullets.
Start by drawing a bullet, or just download the image below.
Open the .fla file you just saved from from Part 1 and import the bullet image to the library.
Drag it anywhere on the flash document and press F8.
Name it as "Bullet", select Movieclip and set the Registration to center box.
Click the Advance button and on the Linkage, check the Export for ActionScript and set Identifier as "bullet".
Now, select the Spaceship movieclip and press F9 and add this new function just above the onClipEvent(enterFrame) function.
onClipEvent(load){ timer = 0; i = 0; }
We use onClipEvent(load) to initialize 2 variables when the movie gets loaded: timer and i. The timer variable will be used as a delay for every bullets fired. The i variable will be used for indexing for naming the creation of each bullets fired.
Next, inside the onClipEvent(enterFrame) function, and just above the if(Key.isDown(Key.LEFT)){,
add the ff. code:
if(timer > 0){
timer--;
}
This will decrement the timer every time the frame is entered but only if timer is any number greater than zero.. Later, you'll see how it is being used.
Next, add this code just below the movement control codes:
if(Key.isDown(Key.SPACE)){ if(timer == 0){ _root.attachMovie("bullet", "bullet"+i, _root.getNextHighestDepth()); _root["bullet"+i]._x = _x; _root["bullet"+i]._y = _y-20; _root["bullet"+i].onEnterFrame = function(){ this._y -= 10; if(this._y < -30){ this.removeMovieClip(); } } i++; timer = 20; } }
if(Key.isDown(Key.SPACE)){ - this checks if the spacebar key is being pressed.
if(timer == 0){ - tells that execute only below codes if timer is equal zero.
_root.attachMovie("bullet", "bullet"+i, _root.getNextHighestDepth()); - This will attach the Bullet MovieClip to the stage, we provide it a name of “bullet”+i and provide the depth with a unique number by using the next highest depth on the stage. This is for ordering of objects on which one should be placed at the front or which one at the back.
_root["bullet"+i]._x = _x; _root["bullet"+i]._y = _y-20; - These code will just set the initial position of the bullet created. In this case, the _x position of the spaceship and 20 pixels above it, _y-20.
_root["bullet"+i].onEnterFrame = function(){
- This function will be executed every time after the bullet has been created.
this._y -= 10;
if(this._y < -30){
this.removeMovieClip();
}
- This will move the bullet for every 10 pixels vertically above the spaceship. If the bullets _y position is less than -30, it will get removed from the stage.
i++;
timer = 20;
- This will update the i variable so that the next creation of bullet will have a new index. Then, the timer variable will be reset to 20, so that the player should wait about 20 enter frame events before it can fire another bullet.
if(timer == 0){ - tells that execute only below codes if timer is equal zero.
_root.attachMovie("bullet", "bullet"+i, _root.getNextHighestDepth()); - This will attach the Bullet MovieClip to the stage, we provide it a name of “bullet”+i and provide the depth with a unique number by using the next highest depth on the stage. This is for ordering of objects on which one should be placed at the front or which one at the back.
_root["bullet"+i]._x = _x; _root["bullet"+i]._y = _y-20; - These code will just set the initial position of the bullet created. In this case, the _x position of the spaceship and 20 pixels above it, _y-20.
_root["bullet"+i].onEnterFrame = function(){
- This function will be executed every time after the bullet has been created.
this._y -= 10;
if(this._y < -30){
this.removeMovieClip();
}
- This will move the bullet for every 10 pixels vertically above the spaceship. If the bullets _y position is less than -30, it will get removed from the stage.
i++;
timer = 20;
- This will update the i variable so that the next creation of bullet will have a new index. Then, the timer variable will be reset to 20, so that the player should wait about 20 enter frame events before it can fire another bullet.
Below is the complete code:
onClipEvent(load){ timer = 0; i = 0; } onClipEvent(enterFrame){ if(timer > 0){ timer--; } if(Key.isDown(Key.LEFT)){ _x -= 6; }else if(Key.isDown(Key.RIGHT)){ _x += 6; } if(Key.isDown(Key.SPACE)){ if(timer == 0){ _root.attachMovie("bullet", "bullet"+i, _root.getNextHighestDepth()); _root["bullet"+i]._x = _x; _root["bullet"+i]._y = _y-20; _root["bullet"+i].onEnterFrame = function(){ this._y -= 10; if(this._y < -30){ this.removeMovieClip(); } } i++; timer = 20; } } }
Now test your flash game by pressing Ctrl+Enter. If you haven't deleted the bullet movieclip you just drag on the stage, you will still see it stick there and not moving.
Just select that instance of the bullet movieclip and delete it. The Bullet movieclip object will still be on the library.
Go back to How To Make A Simple Space Shooting Game In Flash
No comments:
Post a Comment