AS3 - hitTestObject Collision Not Registering Correctly

前端 未结 2 1641
鱼传尺愫
鱼传尺愫 2020-12-22 03:41

I had a problem with my hitTestObject collision detection a couple of days ago which has since been fixed (How can I solve my hitTestObject Collision Null Object Ref Error)

相关标签:
2条回答
  • 2020-12-22 04:12

    I long abandoned the builtin flash functions for collision detection in favor of my own system that uses the separating axis theorem for AABBs, or using something like Box2D to handle the physics for me.

    I've never had success with either hitTestPoint or hitTestObject, but you might find that the bitmapData.hitTest will work for you (never used it), or you could go with a rectangle-based grid system for everything, which is what I do.

    0 讨论(0)
  • 2020-12-22 04:14

    One thing to be wary of is that hitTestObject does AABB (axis-aligned bounding box) collision, meaning that it fits a rectangle around your object, and uses that to test for collisions. Your buildings appear to have complex, concave shapes -- hitTestObject won't take these into consideration, it simply plops a giant rectangle over the building and uses that to test. Also note that if collide is a single MovieClip with all of the building areas inside of it, then the rectangle gets wrapped over that entire area as opposed to having separate areas for the individual buildings. You'd have to split your collision areas up into separate rectangles and test against them individually. This is likely why you are seeing some false positives and bad behavior.

    hitTestPoint will take into account the object's shape and do pixel-perfect collision when the shapeFlag parameter set to true, but this will only test a single point. To test a larger shape, you can try to do multiple hitTestPoint calls iteratively. If that's not sufficient, look into more sophisticated collision methods. Check out this set of tutorials from the creators of N.

    0 讨论(0)
提交回复
热议问题