|
||||||||||||||
. . . . Elena Tresh Foundation Florida . |
Space Invaders Flash MX Game Tutorial - Part 2
Go back to Part 1 if you haven't done that yet.
So there is a little bit of work in that. whew! Where does one start. Ummm I think I will go and have a coffee and think about it. Be back soon... Back again. What is the first thing on our list?
Lessen the number of bulletsOk lets do it. Select your defender mc and open its actions window. At the line where it says
// inside mouseDown method
if(bulletNum>50){
bulletNum = 0;
}
Change the 50 to a 6 (or whatever number you choose) and here it is :
if(bulletNum > 6){
bulletNum = 0;
}
Try that now. It should be a lot less sluggish. I hope.
Bombs.
On a new layer draw some sort of oval bomb shape. Select it and press F8 ,
give it the name of Bomb and give it the instance name of bomb.
onClipEvent (enterFrame) {
this._y += 5;
if (this._y>Stage.height) {
this.removeMovieClip();
}
}
The above code will drop the bomb and remove it when it hits the bottom of the screen. We now need to create a number of bombs. How about 10? That should do us. And we will just drop them randomly from any horizontal position and within a range in the vertical position.
So lets select our controller_mc and in the actions window , write this stuff after the line:
// controller mc enterFrame
// .....
dropdown = false;
// drop bombs
if(Math.random() < 0.1)
{
// duplicate the bomb mc
_root.bomb.duplicateMovieClip("bomb" + bombNum,200+ bombNum);
// set the coords to a random position
eval("_root.bomb" + bombNum)._x =random(700);
eval("_root.bomb" + bombNum)._y = 300*Math.random();
// increment the bomb number
bombNum++;
// if more than 10 bombs , start again at 0
if(bombNum>10){
bombNum = 0;
}
}
All we have done is recycled our bullet code. We have randomly selected a time(whenever the random function generates
a number between 0 and 0.1). You can try whatever number you like there, try 0.5, 0.8.Duplicated the bomb mc , remembering to put it on our 200+ depths. Set the x value to be between o and 700, our Stage width Set the y value to be between 300* 0.01 and 300* 1.0. And then increment the bomb Number, blah blah..Try it , there should be bombs dropping all over the place. We should have dropped the bombs from the lowest row of aliens , but I couldnt work out how to do that. If anyone can suss out how to do that , drop me a line. CoversCreate another layer and draw some sort of cover or barrier. Select it , press F8, call the mc Cover and call the instance "cover". Select the frame in the actions layer and put this code after the duplicate aliens code:
for(var j=0;j<6;j++)
{
duplicateMovieClip("cover", "cover"+j, 300 + j);
_root["cover"+j]._x = j*100 + 80;
_root["cover"+j]._y = 350;
}
It is basically the same code for creating the aliens. Pretty simple . They dont have to move so thats it for them,
except for collision detection later. Notice the depths were put at 300+. Just keep going up in hundreds each lot of
mc's that you duplicate. Keep It Simple Stupid. (KISS).
Collision Detection - Bullets Hit Aliens
So lets start the process of Collision Detection. We have got a lot of processing to do
so lets get stuck into it. First create a new layer and call it "textfields". Create a dynamic TextField.
In the "variable" field in the Properties window call the variable "score".
Create another Dynamic Text field and call its variable "lives". Place these textfields up the top somewhere.
Our scores and lives will be displayed here.
onClipEvent (enterFrame) {
this._y -= 30;
if (_x<0 || _x>Stage.width || _y<0) {
this.removeMovieClip();
}
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
if (_root.bullet, hittest(_root["alien"+i+"_"+j])) {
_root.score +=1;
_root["alien"+i+"_"+j].removeMovieClip();
this.removeMovieClip();
}
}
}
}
The top bit we already had. The bottom for loops just cycle through all the aliens testing if
a bullet has hit an alien. If there is a hit, + the score is incremented by 1, (you can use 10, 100), + and the hit alien, and the bullet,is removed from the screen. + The score will be displayed in the first TextField we created above, with the variable "score". The variable "lives" will be decremented(-1) when a bomb hits our defender.
Before we go any further, we need to make a another frame in our root movie so that we have somewhere to go after
our level ends. So select frame 2 on each layer and hit F6, (except for our actions layer, hit f5 for that) to make
new keyframes on every layer. Delete the contents of all the frame 1's , except for the actions layer. stop();Also make sure to put a stop(); action at the top of the code for layer 2, actions layer. Otherwise it will just loop back to the front page. Now everything that was on frame 1 is now on frame 2, the only thing on frame 1 is the code (stop();) in the actions layer. Now make a button (only on frame 1) on a new layer called "start" and put this code on the button to send it to frame 2:
on(release){
_root.gotoAndStop(2);
}
You can also add some graphics, some instructions, whatever to the front page.
Bombs Hit Defender, Bullets and CoversPut this code into our Bomb mc.
onClipEvent (enterFrame) {
this._y += 5;
if (this._y>Stage.height) {
this.removeMovieClip();
}
// bombs hit defender
if (this, hittest(_root.defender)) {
_root.lives -= 1;
if(_root.lives <=0)
{
// change to go to page 1
Mouse.show();
_root.gotoAndStop(1);
}
this.removeMovieClip();
}
// bombs hit bullets
for(var k=0;k<6;k++){
if(this, hittest(_root["bullet"+k]))
{
this.removieMovieClip();
}
}
// bombs hit covers
for(var l=0;l<6;l++)
{
if(this, hittest(_root["cover"+l]))
{
_root["cover"+l].removeMovieClip();
this.removeMovieClip();
}
}
}
It is pretty self-explanatory. If a bomb hits a defender, a life will be lost and if lives < 0 , then the game is
over and the player gets sent to frame 1 to restart. And I show the mouse so that punters can find the start buttonIf a bullet hits a bomb, it is simply removed. Same for the covers. Finishing Level 1
Add an empty keyframe on frame 3. Later we will put the new level there..
onClipEvent (load) {
dropdown = false;
speed = 10;
}
onClipEvent (enterFrame) {
_root.deadcount=0;
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
// if alien hits defender
if(_root["alien"+i+"_"+j].hittest(_root.defender)){
// change to go to page 1
// remove all the mc's - aliens
for(var i=0;i<3;i++){
for(var j=0;j<10;j++){
_root["alien"+i+"_"+j].removeMovieClip();
}
}
// remove covers
for(var i=0;i<6;i++){
_root["cover"+i].removeMovieClip();
}
Mouse.show();
_root.gotoAndStop(1);
}
// move horizontal
_root["alien"+i+"_"+j]._x += speed;
// check if any aliens left alive
if(_root["alien"+i+"_"+j]!=null){
++_root.deadcount;
}
if (_root["alien"+i+"_"+j]._x<0) {
// set direction to right
speed = 10;
// drop down
dropdown = true;
break;
}
if (_root["alien"+i+"_"+j]._x>Stage.width) {
// set direction to left
speed = -10;
// drop down
dropdown = true;
break;
}
}
}// end for loops
// drop down
if (dropdown) {
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
_root["alien"+i+"_"+j]._y += 20;
}
}
}
dropdown = false;
// drop bombs
if(Math.random() < _root.0.1)
{
// drop a bomb
// duplicate the bomb mc
_root.bomb.duplicateMovieClip("bomb" + bombNum,200+ bombNum);
// set the coords to the bomb
eval("_root.bomb" + bombNum)._x =random(700);
eval("_root.bomb" + bombNum)._y = 300*Math.random();
// increment the bomb number
bombNum++;
// if more than 5 bombs , start again at 0
if(bombNum>6)
bombNum = 0;
}
if(_root.deadcount==0)
{
// remove covers
for(var i=0;i<6;i++){
_root["cover"+i].removeMovieClip();
}
// go to next level
_root.gotoAndStop(3);
}
}
Whew! This is a bit hard-core but I will try to explain it.. In the enterframe method, i declare a variable which
will count the number of undead aliens or !null aliens , ie. still alive. Whenever the deadcount(misnomer) is 0, that means that
all the aliens are killed. And then i can go to the next level (frame 3). Before that I clean up any mc's left on stage.
Make a New LevelGo to frame 3 and hit F6 for every layer. Make a new baddie and call it "bug3". Wherever there is a reference to "alien" replace it with "bug". For example on the Control layer, frame 3 the code for initialising the baddie formation would be:
for(var i=0;i<3;i++){
for(var j=0;j<10;j++){
this.attachMovie("bug3","bug3"+i+"_"+j,depth);
_root["bug3"+i+"_"+j]._x = j*40;
_root["bug3"+i+"_"+j]._y = i*40;
depth++;
}
}
You can increase your baddie speed, the rate they dropdown, and increase the frequency of bombs on each level.
Continue on and make your own levels after that. That should do us for now. Once you have done all that and understood it , go back and redo it. I have learnt heaps doing this tute and it has benefitted me as much as my readers. So thank you very much. I have done a bad job of optimising my code, so it should run heaps faster. Here are some code optimisation tips:
Have a look at the game so far. |
|
||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||