pitto
04-30-2005, 12:29 AM
I am making a shoot em up kind of game, where the enemies travel from right to left on the screen. My idea was to have them at different y values, and random speeds, otherwise it is just a pointless game. Anyway, i have come across a problem with the create enemy function. The enemies that should be further back are infront instead. Here is my code
// varibles
reload = false;
health = 100;
maxhealth = 100;
noenemya = 3;
totalenemya = 4;
guny = 300;
gunx = 50;
// create main things
function buildMap() {
attachMovie("crosshair", "crosshair", 99999);
attachMovie("gun", "gun", 99998);
attachMovie("healthBar", "healthBar", 99997);
attachMovie("shotBar", "shotBar", 99996);
// set guns y position
gun._y = guny;
// make crosshair follow mouse pointer
crosshair.startDrag(true, 0, 0, 600, 300);
// hide mouse pointer
Mouse.hide();
}
// move gun according to crosshair
function moveGun() {
gun._x = crosshair._x-gunx;
createEnemy();
moveEnemy();
}
// check keys
function detectKeys() {
// reload only if totaly finished clip
if (Key.isDown(Key.SPACE) and reload == true) {
shotBar.gotoAndPlay(1);
reload = false;
}
// calls the function that moves the gun movieclip
moveGun();
}
// shoot gun
// on mouse up
function shoot() {
// check if bullets = 0
if (reload == false) {
// make the shot bar show one less bullet
shotBar.nextFrame();
// gun animation
gun.play();
// check if an enemy was shot
for (var i = 0; i<totalenemya; ++i) {
// set var current
shoota = "enemya"+i;
// check if crosshair middle point is on the enemy
if (this[shoota].hitTest(crosshair._x, crosshair._y, crosshair)) {
// if a hit enemy health minus one
this[shoota].a--;
}
}
}
}
// create enemies
function createEnemy() {
// check if there are more enemies to come
if (noenemya>0) {
// set var current
current = "enemya"+noenemya;
// put in screen
attachMovie("enemya", current, 100+noenemya);
// put at x 600
this[current]._x = 600;
// put at a random y(100to200)
this[current]._y = Math.random()*100+100;
// create a speed var
this[current].speed = Math.random()*3;
// create a health var
this[current].a = 2;
// create a max health var
this[current].maxa = 2;
noenemya--;
}
}
// move enemies or let them die
function moveEnemy() {
for (var i = 0; i<totalenemya; ++i) {
movea = "enemya"+i;
this[movea]._x -= this[movea].speed;
if (!this[movea].a>0) {
this[movea].gotoAndPlay("dieing");
this[movea].a = -1;
}
this[movea].health._xscale = this[movea].a/this[movea].maxa*100;
}
}
In addition to this i have a empty movieclip calling the functions, the actionscript on that is:
onClipEvent (load) {
_root.buildMap();
}
onClipEvent (enterFrame) {
_root.detectKeys();
}
onClipEvent (mouseUp) {
_root.shoot();
}
I would really apreciate it if you could help me out with this tricky problem.
Pitto
// varibles
reload = false;
health = 100;
maxhealth = 100;
noenemya = 3;
totalenemya = 4;
guny = 300;
gunx = 50;
// create main things
function buildMap() {
attachMovie("crosshair", "crosshair", 99999);
attachMovie("gun", "gun", 99998);
attachMovie("healthBar", "healthBar", 99997);
attachMovie("shotBar", "shotBar", 99996);
// set guns y position
gun._y = guny;
// make crosshair follow mouse pointer
crosshair.startDrag(true, 0, 0, 600, 300);
// hide mouse pointer
Mouse.hide();
}
// move gun according to crosshair
function moveGun() {
gun._x = crosshair._x-gunx;
createEnemy();
moveEnemy();
}
// check keys
function detectKeys() {
// reload only if totaly finished clip
if (Key.isDown(Key.SPACE) and reload == true) {
shotBar.gotoAndPlay(1);
reload = false;
}
// calls the function that moves the gun movieclip
moveGun();
}
// shoot gun
// on mouse up
function shoot() {
// check if bullets = 0
if (reload == false) {
// make the shot bar show one less bullet
shotBar.nextFrame();
// gun animation
gun.play();
// check if an enemy was shot
for (var i = 0; i<totalenemya; ++i) {
// set var current
shoota = "enemya"+i;
// check if crosshair middle point is on the enemy
if (this[shoota].hitTest(crosshair._x, crosshair._y, crosshair)) {
// if a hit enemy health minus one
this[shoota].a--;
}
}
}
}
// create enemies
function createEnemy() {
// check if there are more enemies to come
if (noenemya>0) {
// set var current
current = "enemya"+noenemya;
// put in screen
attachMovie("enemya", current, 100+noenemya);
// put at x 600
this[current]._x = 600;
// put at a random y(100to200)
this[current]._y = Math.random()*100+100;
// create a speed var
this[current].speed = Math.random()*3;
// create a health var
this[current].a = 2;
// create a max health var
this[current].maxa = 2;
noenemya--;
}
}
// move enemies or let them die
function moveEnemy() {
for (var i = 0; i<totalenemya; ++i) {
movea = "enemya"+i;
this[movea]._x -= this[movea].speed;
if (!this[movea].a>0) {
this[movea].gotoAndPlay("dieing");
this[movea].a = -1;
}
this[movea].health._xscale = this[movea].a/this[movea].maxa*100;
}
}
In addition to this i have a empty movieclip calling the functions, the actionscript on that is:
onClipEvent (load) {
_root.buildMap();
}
onClipEvent (enterFrame) {
_root.detectKeys();
}
onClipEvent (mouseUp) {
_root.shoot();
}
I would really apreciate it if you could help me out with this tricky problem.
Pitto