|
-
multiple HitTest without using iteration
A little bit long here, but very understandable.
Hi, currently I'm creating a shooting game and having problem with hitTest checking.
The game is about controlling one hero (a single Tank) and the enemies are ships which fly from left-screen to right-screen or vice versa.
I've limited the number of HOSTILE object in the screen to 50 at a time. The HOSTILE object means that object is destroyed when my Tank Gun hits it.
OK, now the problem.
In my projectile_mc (which is shot when I press mouse button), I use FOR syntax. So, for 1 to 50, I check whether the projectile hit HOSTILE object or not.
Right, now the main problem is, the SWF is running slowly when multiple projectiles are launched via multiple mouse-clicks. I understand this because each projectile makes 50 iterations which consumes a lot of processor power.
So, are there any techniques which eliminate using FOR or iteration ?
Thanks in advance.
-
Style Through Simplicity
Well I don't believe there is any way to achieve what you want without using a loop. The slowdown is likely due to the graphics rendering as much as it is the loops anyway. If you are using a later version of flash and don't have a stupid amount of bullets then the loops shouldn’t really be a problem. So ways you could boost performance:
- use bitmap graphics rather than vectors. If you are using flash 8 or 9, use the BitmapData functions to draw the sprites to the screen. This is a big speed booster.
- Split the screen up into sectors. Monitor what enemies are in what sector, then the bullets only need to loop through the enemies in the same sector as themselves.
- make sure bullets and enemies are being removed correctly. The slowdown may be caused by that rather than the code.
- If all else fails, reduce the number of enemies/bullets on screen at any time. 50 enemies should be no problem though if you do things right.
Ali
-
Wait- what now?
Is that actually right? In the game in making at the moment there are at least 1000 loops in total which run almost every frame and the game doesn't seem to lag at all, my computer isn't exactly top notch either...
"I'd only told them the truth. Was that so selfish? Our integrity sells for so little, but it is all we really have. It is the very last inch of us, but within that inch, we are free."
-
alillm: many thanks for the information!!
tidenburg: well, I tested it by watching CPU usage in Windows Task Manager. Whe I remove the iteration check, my SWF uses 17% of CPU, when I enabled it, 37-40% CPU is used. Beside, I don't use onEnterFrame, I use setInterval instead.
-
Senior Member
May I suggest grid-based system. Basically divide your stage into tiles with the size of maximum speed any moving object can achieve. Now whenever enemy or bullet moves, it should register itself into tile it is over.
To check if bullet has hit any enemies, you only have to check enemies that are on same tile as the bullet. It effectively avoids checking all the enemies that are far from the bullet and usually means only couple of enemies are checked for each bullet.
-
Another good idea. Btw, your tutorial about tile based games is awesome! (still reading it)
-
SaphuA
SetInterval is teribly slow, better use onEnterFrame instead.
Last edited by SaphuA; 12-03-2007 at 10:01 AM.
-
hippie hater
You can draw all enemies in the same Bitmap, them on enterFrame each bullet only needs to see if his point is not an empty pixel on that bitmap, if its not, them you check all the baddies agains that bullet
Still have to loop on all of them, but not all the time
-
 Originally Posted by SaphuA
SetInterval is teribly slow, better use onEnterFrame instead.
Can you give me a reason why it's slower? thx!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|