ForumsProgramming Forum[LAG] 10 Enemies Collisions with 33 Blocks

6 6031
Solenix
offline
Solenix
6 posts
Nomad

I'm making a room with 10 enemies. The room consists of 33 blocks. I'm checking every block for every enemy on a single enterframe. That's 10 x 33 = 330 collisions being checked in one enterframe.

This causes a lot of frame lag!

What are some good alternate methods to handle this and make it not laggy?

  • 6 Replies
weirdlike
offline
weirdlike
1,299 posts
Prince

I am assuming you have other elements in your enterFrame as well, such as enemy behavior. I suggest reducing the amount of checks, or create a restrictive set of rules before checking. For example, why should an enemy check for collision if it is not even moving, or is not even on screen. If you eliminate a single enemy, that would reduce the number of checks to 297.

Solenix
offline
Solenix
6 posts
Nomad

It seems to run fast without the collision detection codes while other AI are working.

I would originally ask if the collisions are happening while the enemy is moving, but the problem is that the game has knockback in it. So the enemy doesn't always move of it's own will, and I have no clue how to check if the position of the enemy is "moving", because that is only "x" numbers changing.

weirdlike
offline
weirdlike
1,299 posts
Prince

There are a few other tricks I can think of, tho I don't know the full structure of your code. The best route would be to reduce the numbers.

More rules to think about:

- It might be faster to check the distance and if it is within range then check the collision, especially if you're using CDK or some other 3rd party code.

- if there is a collision, should the enemy keep checking for other collisions?

- if the enemy is confined to a certain area it doesn't necessarily need to check collisions with all the blocks only the one's it comes in contact with.

Solenix
offline
Solenix
6 posts
Nomad

Thanks very much. I will try some of the things you told me. If it doesn't work, I'll come back here and post some of my code.

I'll post some here now actually.

if (_root.Block_0.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_1.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_2.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_3.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_4.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_5.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_6.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_7.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_8.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_9.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_10.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_11.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_12.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_13.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_14.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_15.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_16.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_17.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_18.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_19.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_20.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_21.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_22.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_23.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_24.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_25.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_26.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_27.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_28.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_29.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_30.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_31.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
if (_root.Block_32.hitTest(_root.Slime_9.C_Right)){_root.Slime_9._x -= 1}
weirdlike
offline
weirdlike
1,299 posts
Prince

AS2? Are you coding on the timeline? Hmm.... might need to start a new project and play around with array's, then come back to this project.

With array's you can push blocks into one array and your enemies into another, then test the collision by means of a for loop. The end result is a much smaller piece of code.

Introduction to ActionScript 2.0/Arrays - review the portion on multidimensional arrays

Solenix
offline
Solenix
6 posts
Nomad

Okay, thanks. I will check out arrays.

Showing 1-6 of 6