The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.39 / 5.00 38,635 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.09 / 5.00 15,161 ViewsHey,
In my game, the user uses the mouse to aim their gun and when they click it spawns a movie clip (from the library) onto the stage <<this all works
My problem is - my enemies also get spawned automatically and will have diff. instance names.
Eg, i can't do the basic if(this.HitTest(this(_root.bullet)); because the bullet would have a name like _root.bullet2918 and the enemy would have an instance name like _root.enemy4
Is there any workaround for this? How do other games get around this?
thanks (:
Create an array to store all of the created enemies.
Whenever one is created, push it into the array.
Use a loop to check against all the elements in the array.
If one collides, remove that element from the array.
If one goes out of bounds, remove that element from the array.
Repeat.
Perpetually looking for time to return to the arts.
i think that would be a bit too slow...
isn't there a wild card function or something?
If not, could you give me some example code please Johnny?
thanks allot
At 4/19/09 10:13 PM, Captainhams wrote: i think that would be a bit too slow...
isn't there a wild card function or something?
It's actually a fairly efficient way to do things. Been working in AS3.0 lately and forget a bunch of the AS2.0 syntax, so it might be a bit sloppy.
var enemyCounter:Number = 0; //the number of enemies
var enemiesArray:Array = new array() //creates the array
var enemy = attachMovieClip(WhateverCodeYou'reUsing)
;
enemiesArray[enemyCounter] = enemy; //put the enemy in the array
enemyCounter++; //increment the enemy counter
for(var i = 0; i < enemyCounter; i++)
{
if(enemiesArray[i].hitTest(whateEverYou'
reUsing))
{
//remove the clip at enemiesArray[i];
}
}
You'll have to create a small algorithm to stop the enemiesArray from continuously growing. In AS3.0 you can use .push and .splice, but it's been we'll over a year since I've seriously worked with AS2.0, so I'm rusty.
Perpetually looking for time to return to the arts.
my solution is not the best coding habbit but it works great. in your bullet mc, inside its own timeline code the hitTest in a loop using a for statment:
lets assume youve named ur enemys acording to: ["enemy"+i] on the main timeline
your for would look something like this:
for(var i = 0; i<_root.i; i++){
if(this.hitTest(_root["enemy"+i])){
_root["enemy"+i].gotoAndStop("die")
}
}
hey thanks for the help - ive made a montage of both of your ideas and the result is this:
[code]
bullet_mc.onEnterFrame = function() {
//shot an enemy?
c = 0;
while (c<_root.evilbox_spawnCount) {
if (this.hitTest(_root["evilbox"+c])) {
trace("HIIIIIIIIT!!");
} else {
c++;
}
trace("still looping...");
}
//move bullet
this._x += this.xSpeed;
this._y += this.ySpeed;
//
//check if bullet needs to die of old age
if (this.bulletLifeTimer>=bulletLifeTimerTo tal) {
this.removeMovieClip();
}
//update bullet timer
this.bulletLifeTimer++;
};
[/code]
thats the code on the bullet. I'm sure that should work but when I test the movie and shoot at an enemy, flash just commits seppuku and freezes for 5 mins and then crashes and then gives me the "A script is causing Flash player to run slowly...."
Any ideas on whats wrong - or get some more efficient code I could perhaps borrow :)
thx
if (this.hitTest(_root["evilbox"+c])) {
trace("HIIIIIIIIT!!");
} else {
c++;
}
i think this is where your problem is, and why you should be using for. Right now, c is adding up, but as soon as it hits, it stops. therefore your while statment becomes infinite. you could just take out the else in it, that should work, so:
if (this.hitTest(_root["evilbox"+c])) {
trace("HIIIIIIIIT!!");
}
c++; Try to shy away from while loops, and use for loops instead.
Flash is picky about 'while'
Perpetually looking for time to return to the arts.
thanks allot - works like a charm :)
c = 0;
while (c<_root.evilbox_spawnCount) {
if (this.hitTest(_root["evilbox"+c])) {
_root["evilbox"+c].unloadMovie();
this.unloadMovie();
}
c++;
}