I want to be able to spawn little alien ships and have them fly down the screen, then I want to see if they either a) hit a laser b) hit my ship or c) go below the bottom of the screen.
For some reason I can’t get more then one ship spawned with this code. (P.S I’m using Flash MX 2004)
Here is my current code:
shipSpeed = 10;
laserSpeed = 20;
enemySpeed = 5;
enemySpeedx = 3;
i = 0;
n = 0;
score = 0;
shooting = false;
lives = 3;
_root.live = lives.toString();
_root.score = score.toString();
function shoot(laser){
laser._y -= laserSpeed;
if(laser._y < 0-laser._height){
shooting = false;
removeMovieClip(laser);
}
}
function attack(enemy){
enemy._y += enemySpeed;
dir = caculateDir(findDir());
if(dir == "left" and enemy._x - enemy._width*.5 > 0){
enemy._x += enemySpeedx * -1;
}
if(dir=="left" and enemy._x-enemy._width *.5 <= 0){
dir = "right";
enemy._x += enemySpeedx;
}
if(dir == "right" and enemy._x + enemy._width*.5 < 350){
enemy._x += enemySpeedx;
}
if(dir == "right" and enemy._x + enemy._width*.5 >= 350){
enemy._x += enemySpeedx * -1;
}
if(enemy.hitTest(_root.ship)){
enemy.play();
lives--;
_root.live = lives.toString();
}
if(enemy.hitTest(_root.laser)){
iScore = int(_root.score)+5;
_root.score = iScore.toString()
removeMovieClip(laser);
shooting = false;
enemy.play();
}
}
function spawnEnemy(){
n = _root.getNextHighestDepth();
trace(n);
_root.attachMovie("enemy","enemy"+n,n);
enemy = eval("_root.enemy"+n);
trace(enemy);
enemy._y = 0 - enemy._height;
enemy._x = random(100)+enemy._width;
enemy.onEnterFrame = function(){
attack(enemy);
}
}
ship.onEnterFrame = function(){
if(Key.isDown(Key.LEFT) and ship._x > 0 + ship._width*.5){
ship._x -= shipSpeed;
}
if(Key.isDown(Key.RIGHT) and ship._x + (ship._width*.5) < 350 ){
ship._x += shipSpeed;
}
if(Key.isDown(Key.SPACE) and !shooting){
shooting = true;
i = _root.getNextHighestDepth();
attachMovie("laser","laser"+i,i);
laser = eval("laser"+i);
laser._x = ship._x;
laser._y = ship._y + laser._height;
}
laser.onEnterFrame=function(){
shoot(laser);
}
}
function findDir(){
return random(1);
}
function caculateDir(dir){
if(dir=0){
return "left";
}
if(dir=1){
return "right";
}
trace(dir+" is not an acceptable direction");
}
setInterval(spawnEnemy(),100);