Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-16-2003, 01:00 PM   #1
zantafio
Registered User
 
Join Date: Apr 2003
Posts: 29
Unhappy Ye Olde hitTest Problem - revisited....

Ok, here's the deal:

I'm working on a space shooter (flyover style).

the ship encounters a lot of different objects on its way that can either do harm, or supply shields etc.

Now to trigger hitTest as exactly as possible I tried out the method in which you define 8 - 10 small movieclips around the ships outline and loop through them to check if they hit the obstacles shape:
To save CPU power I let the obstacle first check the hitTest for the ships complete area (i.e. a square) and if that resolves to true let it loop through the ships 10 shape clips.

this worked pretty good in the beginning, but towards the end of the game when everything gets faster and more objects are spawned it was a total failure.
( i guess the looping took too much time...)

Now I could of course say that according to the games speed the more complicated hitTest function could be dropped in favor of the simple one but this would be hard since on a slow computer it might not work.

The other option could be to attach a little square movieclip inside each object to hitTest against, so the hitTest would not trigger prematurely (especially in round shapes).

But I am also thinking that it would be best to let only the ship do a hitTest and not all of the objects ?
but how could that be done ?

Lets say I have 20 objects on the stage which are called "object0" to "object 19".
How could I tell the ship:
If you encounter an object which has the substring "object" in it's name check for the objects shape, and if you hit it do this and that...

AAARghh ...

Any advice greatly appreciated...
thanks
zantafio is offline   Reply With Quote
Old 05-16-2003, 11:12 PM   #2
emergency_pants
Today I smell of toast
 
Join Date: Dec 2002
Location: London, UK
Posts: 1,053
Default

It is my humble opinion that HitTests need to be developed further for the next version of Macromedia Flash.

Here is a sample loop script that will test for 20 numbered MCs:

ActionScript Code:
for (i=0; i<20; i++) { if (_root["object"+i].hitTest(_root.ship)) { _root.ship.hSpeed *= -1; _root.ship.vSpeed *= -1; } }

Hope that helps.
__________________
Cheers,

Simon.
emergency_pants is offline   Reply With Quote
Old 05-16-2003, 11:25 PM   #3
zantafio
Registered User
 
Join Date: Apr 2003
Posts: 29
Default

I second your humble opinion !

thanks for the script, I came up with something similar, but isn't this very CPU intensive since I have to call it on every frame?
zantafio is offline   Reply With Quote
Old 05-16-2003, 11:26 PM   #4
emergency_pants
Today I smell of toast
 
Join Date: Dec 2002
Location: London, UK
Posts: 1,053
Default

I have a sneaky feeling that you could use functions to force the ship to hitTest all of the objects, rather than attach hitTest scripts to all of your objects!

ActionScript Code:
onClipEvent(enterFrame){ HitTestFunction(walls); HitTestFunction(floor); HitTestFunction(enemyShapes); HitTestFunction(enemyBullets); //etc etc etc }

Your HitTesFunction() would be something like:

ActionScript Code:
function HitTestFunction(target){ _root.ship.hitTest(target._x,target._y, true); }

You might need to alter the syntax and stuff, but I hope you get the general idea.
__________________
Cheers,

Simon.
emergency_pants is offline   Reply With Quote
Old 05-16-2003, 11:32 PM   #5
emergency_pants
Today I smell of toast
 
Join Date: Dec 2002
Location: London, UK
Posts: 1,053
Default

Yeah... they're both probably quite cpu intensive, but I've never been able to find anything more efficient.

Altohugh I have found a neat trick. I find that sometimes, enterFrame hitTests are rather sketchy when testing for small objects... they can run WAAAAY into a shape before the hit registers. When objects are moving fast, they can miss a hitTest completely!

So I put all my hitTests into functions and then use
ActionScript Code:
setInterval(hitTestFunction,5);

or something similar to run my hitTests.

You need to make sure that you use an updateAfterEvent() command at the end of your hitTest functions, to take advantage of the increased hitTest rate.

yep... more work for the cpu, but much nicer hitTests... more reliable.

And if you use it for movements too, you'll find beautifully smooth ship movememnts, etc. Yum.
__________________
Cheers,

Simon.
emergency_pants is offline   Reply With Quote
Old 05-16-2003, 11:46 PM   #6
zantafio
Registered User
 
Join Date: Apr 2003
Posts: 29
Default

fast as chat

Actually I always use setInterval since flash mx is out, this way I have more control over the timing of certain instances.

My solution was the old "little square box inside the target movieclip" to save all that cpu.
Hittest are not very accurate but it works.oh well..
zantafio is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:31 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.