Yet Another hitTest Question

4 posts

Flag Post

I am kind of new to Flash 8, AS2.0, but not that new to programming so I understand somethings are not possible, I think this is one of them but figured I would ask.

Scenario Setup:
There are a hundred squares on the stage and one circle.
If the circle runs into a square a hit should be registered.
Now lets say all the squares are named “Square#” where # is just some number between 1 and 100.

Is there a better way to register the hit then:
//this is the circle
onClipEvent(enterFrame){
for(i=1;i<=100;i++){
if(this.hitTest(_root["Square"+i])){
//Do something with the hit
}
}
}

The reason I ask is that if I have a bunch of circles and square the thing slows down so much its impossible to do anything.

If there is a way to register a hit without checking every single name that would be a huge plus, I highly doubt that its possible, but any other tips to speed up the hitTest would be helpful, other then checking the hitTest once every like 6 frames or so.

 
Flag Post

Of course it’s possible, you just have to design your system so that you know what squares are in the immediate area of the circle, so you don’t have to test all at once. If you have four different areas of the screen, and four arrays full of squares, you can test only against the squares occupying the area that your circle is in.

There’s already a thread on square-circle collision, so just look through the forum and check it out… it’s already been talked about exhaustively. But, you don’t really need the MovieClip.hitTest() method here.

Like with any programming language, in ActionScript you can make things easy or hard on yourself. Honestly, I think anything is possible if you put your mind to it… but this particular problem really isn’t too difficult to manage; you just need to be a little more creative. :)

 
Flag Post

Also, in the method I described, you will need to manage the square arrays somehow… and make sure that when squares pass into a new zone, they get put in the corresponding array.

 
Flag Post

The quadrant thing is not a bad idea, I may try something along those lines.

I did check out the squares and circle thread, along with all the other hitTest threads I could find here, before I posted but that thread is more about concerned accuracy of the hit which is something that does not matter in my program, they all could be squares as far as I’m concerned I just said circle because it was a different shape so it would be easier to read.

Thanks for the feed back, if you have any other ideas please let me know