|
||||||||||||||
. . . . . |
Documenting a Flash AS2 OOP Game Development Part 2
These Flash Tutorial notes follow on from Part 1 , which is here
Make your enemy/baddie movieclip in flash mx2004, and in the linkages dialog box, write "baddie" in the Identifier
textfield and in the class textfield write "Enemy".
// ------ ATTACH 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;
}
Here is what we have got in our Enemy class so far:
class Enemy extends MovieClip{
// put implementation in here
} // ------- end enemy class
Move Class Enemy
Nothin'. So what do we need ? How about some variables. Lets go back to our class diagrams and design docs and see what
we need. Well, we need its speed , so lets put that in, and we will need a move method
class Enemy extends MovieClip{
// declare attributes
private var speed:Number;
// ======= CONSTRUCTOR
function Enemy(){
speed = 20;
}
// ===== MOVE
function moveEnemy(){
// leaves stage left
if(_x<=-30){
reset();
}
else{
_x -= speed;
}
}
// ====== RESET
function reset()
{
_x = Stage.width+50;
_y = Math.random()*380 + 10;
speed = Math.random()* 10 + 10;
}
} // ----- end enemy class
And test it by compiling.(i.e. Go to flash and hit CTRL/ENTER , or go to menu item - Control - TestMovie).
// in Game class
// move enemies
function moveBaddies(){
for(var i=0;i<3;i++){
enemyArray[i].moveEnemy();
}
}
and double check that you have the movebaddies method in the enterFrame loop in our flash file. This keeps our action panel code very neat and tidy :
// loop
_root.onEnterFrame = function(){
myGame.checkKey();
myGame.moveShip(dir);
myGame.moveLaser();
myGame.moveBaddies();
}
Collision Detection
I'm not quite sure how I am going to do this. Probably in the Game class. And do a couple of loops ...
// in 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 this line inside the enterFrame method of our .fla :
// the loop
_root.onEnterFrame = function(){
myGame.checkKey();
myGame.moveShip(dir);
myGame.moveLaser();
myGame.moveBaddies();
myGame.collision();
}
Make the Explosion
// in game classs
// Collision Detection
function collision(){
for(var j = 0; j<6;j++){
for(var k=0;k<3;k++){
if(laserArray[j].hitTest(enemyArray[k])){
// explode the mofo
_root.attachMovie("splode", "splode", 301);
// set explosion to baddy coords
_root.splode._x = enemyArray[k]._x;
_root.splode._y = enemyArray[k]._y;
// reset the baddy to stage right
enemyArray[k].reset();
break;
}
}
}
}
Collision Detection - Baddies hit ShipNow lets check for a collision between a baddy and our spaceship. I am recycling the splode_mc, by changing its color and changing its x and y-scale. We could also put a sound effect in each of the hitTest checks. But I will leave that up to you.
// Collision Detection
function collision(){
for(var j = 0; j<6;j++){
for(var k=0;k<3;k++){
// check bullets hit baddies
if(laserArray[j].hitTest(enemyArray[k])){
// explode the mofo
_root.attachMovie("splode", "splode", 301);
// set explosion to baddy coords
_root.splode._x = enemyArray[k]._x;
_root.splode._y = enemyArray[k]._y;
// reset the baddy to stage right
enemyArray[k].reset();
}// end if
// check baddies hit ship
if(enemyArray[k].hitTest(ship)){
_root.attachMovie("splode", "splode", 301);
// set explosion to ship coords
_root.splode._x = ship._x;
_root.splode._y = ship._y;
// change color
var my_color:Color = new Color(_root.splode);
my_color.setRGB(0xff0000);
// change size
_root.splode._xscale = _root.splode._yscale = 400;
}
}
}
}
Score and livesWhat is left ? Scoring and lives. I might make a couple of textfields and set the text property on them when a change occurs. So make 2 textfields in our Flash MX2004 file and in the instance names call them score_txt and lives_txt. Then give the Game class 2 more variables, score and lives and initialise them in our constructor:
// in 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;
// initialise score and lives
score = 0;
lives = 10;
}
Now lets set the textfields to the score and lives variables:
// in Game class
// Collision Detection
function collision(){
for(var j = 0; j<6;j++){
for(var k=0;k<3;k++){
// check bullets hit baddies
if(laserArray[j].hitTest(enemyArray[k])){
// increment score and display it
score += 10;
_root.score_txt.text = " Score: " + score;
// explode the mofo
_root.attachMovie("splode", "splode", 301);
// set explosion to baddy coords
_root.splode._x = enemyArray[k]._x;
_root.splode._y = enemyArray[k]._y;
// reset the baddy to stage right
enemyArray[k].reset();
}// end if
// check baddies hit ship
if(enemyArray[k].hitTest(ship)){
// reset baddy, decrement lives
// display lives count
enemyArray[k].reset();
lives -= 1 ;
_root.lives_txt.text = " Lives : " + lives;
_root.attachMovie("splode", "splode", 301);
// set explosion to ship coords
_root.splode._x = ship._x;
_root.splode._y = ship._y;
// change color
var my_color:Color = new Color(_root.splode);
my_color.setRGB(0xff0000);
// change size
_root.splode._xscale = _root.splode._yscale = 400;
}
}
}
}
During the above coding i had to reset the baddies when they hit the ship because the lives were still rolling
over because the collision lasted a few frames.. try it with the enemyArray[k].reset() line commented out and
watch the lives keep on ticking over. It is a fairly simple procedure. |
|
||||||||||||
|
. | 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 | . . |
||||||||||||||