PDA

View Full Version : Call an Array From a hitTest?



Yeldarb
August 10th, 2003, 07:54 PM
can this be done? if so, how?
i'm trying to make two duplicated movie clips check for any of the others
ie bullet239 hitting badguy92
thanks

ahmed
August 10th, 2003, 07:58 PM
myarray = [_root.baddy1, _root.baddy2, _root.baddy3, _root.baddy4, _root.baddy5]
for (i=0; i<myarray.length; i++) if (bullet.hitTest(myarray[i])) { break; trace(myarray[i]+" is hit!") }

Yeldarb
August 10th, 2003, 08:36 PM
is there a way to not type _root.baddy, just have the numbers, like baddy1-9999999

ahmed
August 10th, 2003, 08:48 PM
for (i=1; i<=99999999999999999999999999; i++) {
if(_root["baddy"+i].hitTest(bulletMC)) {
trace("hit!");
break;
}
}
OR

for (i=1; i<=99999999999999999999999999; i++) {
if(eval("baddy"+i).hitTest(bulletMC)) {
trace("hit!");
break;
}
}the second example uses eval, it's decprecated (The flash 5 way), and the first uses the flash mx way. The second example, though, is faster..

thoriphes
August 10th, 2003, 09:21 PM
whoa, whoa, whoa....i hope you're not serious, ahmed. this thing's gonna run forever.

Do i remember you asking this type of question before, yeildarb?

As much as possible, try to stay away from long for loops. Nobody's gonna want to watch a flash movie that freezes their computer.

Yeldarb
August 10th, 2003, 11:23 PM
this would run slow? is there a way to do it w/o it running slow?

Scootman
August 18th, 2003, 03:51 AM
set ranges for your mcs... like set your bullets to go 1-100 and your badguys to go 101-200 and if bullets get to 100 reset the counter to 1 and if your badguy counter gets to 200 reset it to 101 and then in your for loop you can do

for(i=1;i<=100;i++)
{
for(j=101;j<=200;j++)
{
//if i hits j then do stuff
}
}

and to go even faster then make even smaller ranges like 1-50 and 51-100 or whatever you want... fastest i could see is setting the ranges so its just enough for stuff to always be on the screen... because when you reset your variables say when it gets to 100 you set it back to 1... if 1 is still on the screen its going to get written over and the old #1 will disappear

anyway hope it helps

Eric Jr.
August 18th, 2003, 05:58 AM
Shame on you Ahmed !!:bad: :bad: :bad:

Ofcourse there is only one method of doing this correctly:
MovieClip.prototype.explode = function()
{
trace("POW! (" + this + " exploded)");
}

for (var i in _root)
{
obj = _root[i];
if (typeof(obj) == "movieclip" && obj._name.substr(0,5) == "baddy")
{
hit = obj.hitTest(bulletMC);
//if (hit)
obj.explode();
}
}

If loops through all the object in the _root.
Then it checks if its a MovieClip, and if the name starts with "baddy".

If a match has been found it checks if there is a collision with the bullet, and if so, it "explodes" the movieclip.
(MovieClip.prototype.explode() currently only traces a confirm)

Scootman
August 18th, 2003, 07:08 PM
nice... i didnt even think of that... thats a lot faster than my method... instead of checking every one against every other one... the game would run a lot faster..

rysolag
August 18th, 2003, 08:53 PM
but there is more than 1 bullet...

rysolag
August 18th, 2003, 08:57 PM
that AS is in the bullet right? with bulletMC = this?

Scootman
August 19th, 2003, 03:48 AM
yeah you put it in the main bullet that you will be duplicating and then each duped one will check itself against the enemies

Eric Jr.
August 19th, 2003, 06:09 AM
Very good ... check my explaination here:

http://www.kirupaforum.com/forums/showthread.php?s=&threadid=31973