PDA

View Full Version : Enemy depths???


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

Barn
04-30-2005, 12:49 AM
Too hard to read unformatted. Try enclosing in AS tags.

pitto
04-30-2005, 12:55 AM
here it is again
// 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--;
}
}

And
onClipEvent (load) {
_root.buildMap();
}
onClipEvent (enterFrame) {
_root.detectKeys();
}
onClipEvent (mouseUp) {
_root.shoot();
}

Pitto

Barn
04-30-2005, 01:06 AM
Not sure why they'd be appearing in front. Maybe put a trace in there to see in what scope the enemies are being created. You could also make the createEnemy function run faster by creating a reference, instead of using a string variable:

// create enemies
function createEnemy() {
trace("scope: "+this);
// check if there are more enemies to come
if (noenemya>0) {
// put in screen
currEnemy = this.attachMovie("enemya", "enemya"+noenemya, 100+noenemya);
// put at x 600
currEnemy._x = 600;
// put at a random y(100to200)
currEnemy._y = Math.random()*100+100;
// create a speed var
currEnemy.speed = Math.random()*3;
// create a health var
currEnemy.a = 2;
// create a max health var
currEnemy.maxa = 2;
noenemya--;
}
}

pitto
04-30-2005, 01:37 AM
I think i need to give a visual example of what is happening
First Scenario

1 (look at 1.gif) first inserted (1) is going slower than second inserted (2)
2 (look at 2.gif) the second inserted has a depth of 102, first 101, so the second is above
3 (look at 3.gif) this is what it should look like

The code may need to do something with swapping depths, but i'm not really sure what to do
Pitto

Barn
04-30-2005, 02:10 AM
Well, maybe try changing that attachMovie line to this:

currEnemy = this.attachMovie("enemya", "enemya"+noenemya, 200-noenemya);

pitto
04-30-2005, 02:49 AM
that works for the example, but then if the second is lower than the first that doesnt work
Pitto

Barn
04-30-2005, 03:15 AM
Yes, that puts the second one lower than the first, and the third lower than the second.

pitto
04-30-2005, 06:36 AM
I found out a way to do it myself

Of course you can use the y properties to do the depths, here is my new createEnemy function

function createEnemy() {
// check if there are more enemies to come
if (noenemya>0) {
// set var current
current = "enemya"+noenemya;
// y value used for placing and depth
currenty = Math.round((Math.random()*100+100));
currentspeed = Math.round(Math.random()*3+1)
// put in screen
attachMovie("enemya", current, 100+currenty);
// put at x 600
this[current]._x = 600;
// put at a random y(100to200)
this[current]._y = currenty;
// create a speed var
this[current].speed = currentspeed;
// create a health var
this[current].a = 2;
// create a max health var
this[current].maxa = 2;
noenemya--;
}
}

Thankyou for all your help