View Full Version : [AS2; Flash 8] Space Invaders Style Game - The Aliens
Olaf Forkbeard
May 16th, 2009, 11:11 PM
I am working on a Space Invaders style game, where the aliens (in formation) move left and right. Every time they hit one side or the other they drop down about half their height. (I'll just call it a step). So they move down a step and then repeat till they "land". So my problem is that whenever they hit a side not all of the aliens reverse direction as soon as they hit.
My work is here for your reference.
http://lawstudios.bravehost.com/nrd/spaceInvadersBeta.fla/
(http://lawstudios.bravehost.com/nrd/spaceInvadersBeta.fla/)
Thanks!
-Forkbeard
EgoAnt
May 18th, 2009, 10:16 AM
I figured, if all the aliens basically move as one, why not just attach them to one object and move it?
main();
function main() {
setupBullet();
setupShip();
setupAliens();
}
function setupBullet() {
this.attachMovie('bulletMC', 'bullet', this.getNextHighestDepth(), {_x:-1});
bullet.dy = 0;
bullet.speed = 16;
bullet.onEnterFrame = function() {
bullet._y -= bullet.dy;
if (Key.isDown(32)) {
bullet._x = ship._x;
bullet._y = ship._y;
bullet.dy = bullet.speed;
}
};
}
function setupShip() {
this.attachMovie('shipMC', 'ship', this.getNextHighestDepth(), {_x:256, _y:480});
ship.speed = 6;
ship.onEnterFrame = function() {
if (Key.isDown(37)) {
ship._x -= ship.speed;
}
if (Key.isDown(39)) {
ship._x += ship.speed;
}
};
}
function setupAliens() {
aCol = 8;
aRow = 2;
this.createEmptyMovieClip("alienHolder",2);
this.alienHolder._x = 16;
this.alienHolder._y = 0;
this.alienHolder.aSpeed = 4;
this.alienHolder.colTracker = new Array();
alienHolder.onEnterFrame = function()
{
////////////////////////////////////////////////////////////////
this._x += this.aSpeed;
if (this._x < leftEdge() || this._x > rightEdge()) {
this.aSpeed *= -1;
}
this._x += aSpeed;
////////////////////////////////////////////////////////////////
}
for (var a = 1; a<=aCol; a++) {
this.alienHolder.colTracker.push(aRow);
for (var b = 1; b<=aRow; b++) {
alienLNK = 'alien_'+a+'_'+b;
newAlien = alienHolder.attachMovie('alienMC', alienLNK,alienHolder.getNextHighestDepth(), {_x:(a-1)*36, _y:b*36});
newAlien.myColumn = a;
newAlien.onEnterFrame = function() {
if (this.hitTest(bullet)) {
bullet.dy = 0;
bullet._x = -1;
trace(this.myColumn-1);
trace(_root.alienHolder.colTracker[this.myColumn-1]);
_root.alienHolder.colTracker[this.myColumn-1] -= 1;
removeMovieClip(this);
}
};
}
}
}
function leftEdge()
{
leftNum = 0;
for(i = 0; i < alienHolder.colTracker.length; i++)
{
if(alienHolder.colTracker[i] > 0)
{
leftNum = i;
break;
}
}
return 16 - (leftNum * 36);
}
function rightEdge()
{
rightNum = 0;
for(i = alienHolder.colTracker.length - 1; i >= 0 ; i--)
{
if(alienHolder.colTracker[i] > 0)
{
rightNum = 7-i;
break;
}
}
return 244 + (rightNum * 36);
}
Olaf Forkbeard
May 18th, 2009, 08:37 PM
That did the trick thanks so much, I thought about that at the back of my mind but for some reason never implemented it.
Thanks alot!
EgoAnt
May 18th, 2009, 08:48 PM
You're welcome! Glad I could help.
Charleh
May 19th, 2009, 05:01 AM
You can also keep them separate and broadcast events between invaders, this means that they don't need a container object.
I create a space invaders game in my tutorial here
http://www.kirupa.com/forum/showthread.php?t=281507
Get the latest attachment on the thread for the code
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.