Multiple collision detection

I’m having a problem with my multiple collision detection code for a platformer game…
It checks for collisions on all objects separately and it checks only one object per frame.
I need to make somekind of a loop so that it would check every object in one frame.
Anyone got an idea? :q:
It’s coded like this:

if (i>0) {
i--
} else {
i=icount
}

and after that, there’s the collision and result code.

you need a “for in” loop for this. (the most underrated (probebly because it’s the least understood) loop)

the syntax is something like this


for(obj in target){
    if(this.hitTest(obj)){
        //doThis
    }
}

Now this would be placed in an onClipEvent, on an object that you wanted to test against, like a character.

The variable called “obj” is abitrary. It is mearly the variable that will be assigned the name of each object as it is found (see below).

the “for in” loops works because as soon as the flash player creates an object, it has at it’s disposal an array of all objects within that object… variables and movie clips alike. When it searches using the for in loop it mearly looks at each object in turn. If the object is a variable, it cannot perform a hitTest against it, so it skips to the next object, until it reaches a movie clip which is vailid for the hitTest method.

You might want to take a look at

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

if you’re going to use a for in() loop

Umm… Here’s a quick question…

Is there a possibility for alot of objects on the screen at one time?

If so… I might suggest a slightly larger bit of code… BUT… It’ll only check for collission detections closest to the object you are testing the hit with…

See… When designing for games in mind… You have to keep in contact with the fact that speed is a big issue…

David’s thought on the for… in loop is most excellent… But… If you get 20 objects on the screen… It’ll go through each and every one of the objects even if the main object isn’t close to them…

You might wnana toy around with this idea but say… When you load up an area or level in your game… You might wanna say:



i = 1;

for (object in target)
{
     objxvalues* = _root.obj._x;
     i++;
}


You may ask… Why would I want to bother with this… Well when you look at the for in statement… it’ll look at every single object that is on that screen and some that may be off of it… And it does tests to see if your guy or an attack actually hits all those objects… Well hitTest’s are slower than first testing for the object’s closest to your guy or attack and then testing for hitTest’s…



i = 1;

for (object in target)
{
     if(math.abs(objxvalues*-mainobjxvalue) <= 50)
     {
          if(mainobj.hitTest(object))
          {
               //do this
          }
     }
     i++;
}


Egads… I’ve gotta start testing these more though… I’m actually not sure which way would be faster now that I take a look at it… But I know for a fact that the hitTest code is far greater in size than these couple of lines…

Just giving you an idea that if you get the objects working with david’s for…in loop… But it slows down… Give this a try… The first php code sequence should be in where the level loading thing should be… And the next should be added into your frame loops.

Peace Man

Oh MAN did the For loop DO IT! Thanks David!
Thanks playamarz, optimizing is a good idea but I’ll try to get this working first. :slight_smile: