|
||||||||||||||||
. . . . . |
Space Invaders Inspired Tutorial Version 2.0I have redone the
original Flash Game tutorials and made an upgraded version of my Space Devourers game. First off , Make the movie 700 x 400 and set the frame rate at 25 frames/per/second. Then make 3 layers and call them : actions, text, defender. Frame 1 - Intro
In frame 1 , layer "text" Put some text describing your gameplay("use the mouse"), a title and a start button.
You can put some graphics etc as well in here
on(release){
_root.gotoAndStop(2);
}
This is very basic. All it does, of course, is take us to frame 2. Make your sprites
Create 3 alien sprites. Make them 30x30. I made them as bitmaps and saved them as transparent png's.
Also make a defender about 40X25. Call the mc "Defender". Call the Alien mc's "Alien", "Bug" and "Skull". Go to the
Library window(F11) and right click on Alien. Select "Linkage" and then
tick "Export for Actionscript" and "Export in first frame". In the identifier box , name it "alien". Do the same for
Bug and Skull. Call them "bug" and "skull" in the identifier box. That will be their actionscript mc name.
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Write your FunctionsSelect your "actions" layer and put this code in : stop(); // declare and initialise variables bombNum = 0; lives = 3; score = 0; speed=10;This code stops the playhead going to frame 2 until the user has pressed the start button, and initialises our variables that we will use later. Initialise the AliensWe will write a global function which will allow use to initialise the various types of baddies
and to call it in one line of code. We can reuse it by simply passing the name of the baddie to the function.
like so : initAliens("bug") or initAliens("skull"). Notice that we pass the linkage name and not the MC name.
// --------- INITIALISE ALIENS ------------- //
_global.initAliens = function(mc) {
depth = 0;
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
attachMovie(mc, mc+i+"_"+j, 100+depth);
_root[mc+i+"_"+j]._x = j*40;
_root[mc+i+"_"+j]._y = i*40-80;
depth++;
}
}
};
It is pretty self-explanatory. I use a 2 Dimensional Array, 3 by 10 , and use 2 for loops to go through and name and attach the aliens according to their rows and columns. Then their x and y positions are determined by their row and column numbers. I have given them depths of 100+. That is because the bullets will be given number from 0+. It is good to give each sprite and their duplicates the same hundreds number. I will be using 200+ for the bombs. So if I had more ranges of duplicated or attached clips i would use 300+, 400+, etc... Each mc must be on its own depth, otherwise it will be overwritten. Move Aliens FunctionWe will now write the global function that will allow us to move the aliens and allows us to test for any collisions with the defender's bullets. We have jammed a lot in here because I want to have as few loops as possible so that the game runs as fast as can be. 3 arguments will be passed to the function: the mc name(alien, bug, or skull), the next frame number, and the baddies' speed. Write it on frame 1, actions layer after the above code. Here it is:
// ------- MOVE ALIENS ----------------- //
_global.moveAliens = function(mc, frame,alspeed) {
// count the dead baddies
_root.deadcount = 0;
// loop through baddies
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
// move horizontal
_root[mc+i+"_"+j]._x += speed;
// check if aliens hit defender
if(_root[mc+i+"_"+j].hitTest(_root.defender)){
cleanup(mc);
_root.gotoAndStop(1);
}
// check if any aliens left alive
if (_root[mc+i+"_"+j] != null) {
++_root.deadcount;
}
// -------test bullet hit
bulleti = 6;
while (--bulleti>0) {
if (_root[mc+i+"_"+j].hitTest(eval("_root.bullet"+bulleti))) {
_root[mc+i+"_"+j].removeMovieClip();
eval("_root.bullet"+bulleti).removeMovieClip();
_root.score += 1;
}
}
// hits left wall
if (_root[mc+i+"_"+j]._x<0) {
// set direction to right
speed = alspeed;
// drop down
dropdown = true;
break;
}
// hit right wall
if (_root[mc+i+"_"+j]._x>Stage.width) {
// set direction to left
speed = -alspeed;
// drop down
dropdown = true;
break;
}
}
}
// drop down all the rows
if (dropdown) {
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
_root[mc+i+"_"+j]._y += 20;
}
}
}
// reset flag
dropdown = false;
// if all aliens are dead
if (_root.deadcount == 0) {
// go to next level
_root.bombspeed = 0.0;
_root.gotoAndStop(frame);
}
};
So what does this do. We set up 2 loops to go through each alien. We :
Initialise the Bombs FunctionThis function will randomly drop bombs at a random time set by bombspeed, the argument passed to this function. I have used the deprecated random() function as well as the newer Math.random() which returns a random number between 0 and 1. I have also set the bombs to have a random starting x and y position. If the bombNumber is greater than 10 , it simply starts again at 0. Add this code in frame 1 , actions layer, after the above moveAliens function:
// ----------- INIT BOMBS --------------- //
_global.initBombs = function(bombspeed) {
// drop bombs
if (Math.random()<bombspeed) {
// duplicate the bomb mc
attachMovie("bomb", "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;
}
}
};
Move Bombs Function
This function will move the bombs and do collision detection on the defender.
// -------------- MOVE BOMBS ---------------- //
_global.moveBombs = function(mc) {
var bombi = 0;
while (bombi<11) {
_root["bomb"+bombi]._y += 5;
// hitest defender
if (_root["bomb"+bombi].hittest(_root.defender)) {
_root["bomb"+bombi].removeMovieClip();
_root.lives -= 1;
// lives are all gone
if (_root.lives<=0) {
// go to start
Mouse.show();
_root.bombspeed = 0.0;
_root.gotoAndStop(1);
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
_root[mc+i+"_"+j]._visible = false;
}
}
break;
}
}
// test if bomb goes offscreen
if (_root["bomb"+bombi]._y>Stage.height) {
_root["bomb"+bombi].removeMovieClip();
}
bombi++;
}
};
Clean Up Function
This function cleans up before the user is returned to frame 1. It sets the bombspeed to 0 ,
and makes all the baddies invisible. If not done the baddies continue in the foreground when
a user is taken back to frame 1.
// ---------- CLEAN UP -------- //
_global.cleanup = function(mc) {
_root.bombspeed = 0.0;
for (var i = 0; i<3; i++) {
for (var j = 0; j<10; j++) {
// move horizontal
_root[mc+i+"_"+j]._visible = false;
}
}
};
That finishes off frame 1. To recap,
Frame 2 - Level 1
In each layer hit F6 to make new keyframes.
In frame 2, "text" layer, delete all text and the button and create 2 dynamic textFields.
Write "score" and "lives" in their variable fields
in their respective properties windows. I have put them up the top left and right. Make some static text fields next to
each and write something like "Score: " and " Lives :".
Select Frame 2, "defender" layer. Drag the Defender mc from the Library to the bottom of the stage. In the "properties" window, type "defender" in the instance name box. Frame 2 - Actions LayerSelect Frame 2, "actions" layer and type this in the Action window:
stop();
// initialise variables
bulletNum = 0;
dropdown = false;
speed = 10;
_root.bombspeed = 0.1;
initAliens("alien");
This code stops the playhead at this frame, initialises the variables for this frame, and calls the global function initAliens("alien") that we declared in frame 1. We will reuse this line in each level to initialise our baddies. After that we will put in our code for our our onLoad method. All it does it hide the mouse. So, type in this after the above code : >
// ----------- ONLOAD ----------- //
_root.onLoad = function() {
Mouse.hide();
};
Now we need to write our code for our enterFrame method. I have made everything fit into this one loop. It is our timer and every 25 times per second(our framerate), this code is executed. Enter this code in frame 2, "actions" layer after the onload method:
// ------------- ENTERFRAME ---------- //
_root.onEnterFrame = function() {
// move the defender with the mouse.
_root.defender._x = _root._xmouse;
_root.defender._y = 385;
// bullet move
var y = 0;
while (y<6) {
eval("_root.bullet"+y)._y -= 30;
y++;
}
moveBombs("alien");
moveAliens("alien", 3,10);
initBombs(_root.bombspeed);
};
In the enterframe method we mainly use the functions we wrote earlier and we will reuse them on every level. We will:
Write the method for when the mouse clicks. Put this code in frame 2, actions layer after the enterframe method :
// ------------- MOUSE CLICK ---------- //
_root.onMouseDown = function() {
// duplicate the bullet mc
attachMovie("bullet", "bullet"+bulletNum, bulletNum);
// set the coords to the mouse clik
eval("_root.bullet"+bulletNum)._x = _root.defender._x;
eval("_root.bullet"+bulletNum)._y = _root.defender._y;
// increment the bullet number
++bulletNum;
// if more than 5 bullets , start again at 0
if (bulletNum>5) {
bulletNum = 0;
}
};
That finishes frame 2. In our Defender layer we should have our Defender mc with the instance name of "defender".
Frame 3 - Level 2
Simple. We have done all the hard yards and it is downhill from here. Frame 4 - Level 3
Copy and paste the code from frame 3, "actions" layer to frame 4. As before , you will only have to make a
few changes, similar to frame 3. Use the replace dialog box to replace "bug" with "skull".
That is all for Frame 4. Hit F6 to make keyframes for frame 5 on all layers. Frame 5 - Game Over On frame 5, "defender" layer , delete the defender mc. stop();
In the "text" layer, frame 5, you can delete the lives textfields and leave the score there. Write some sort of
text stating that "You have won etc" .
on (release) {
_root.lives = 3;
_root.score = 0;
_root.gotoAndStop(2);
}
Where to now ?This is only the start. This was my second rewrite. You can do lots more to this to refine and perfect. Here are some suggestions.
That is it for now. I have made extensive use of global functions and this has made the creation of levels
a whole lot easier. |
|
||||||||||||||
|
| 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 | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
||||||||||||||||