Quick tip: always use global position for hitTestPoint()

Just for the record and myself (I should read all the documentation, also the long sentences 🙂 … Make sure you always check hitTestPoint() against global coordinates of some point.

// we are somewhere deep in myObject.myObject2.myInnerObject
var point:Point;
var someMovingObject:DisplayObject;
var hitShape:DisplayObject;

point = localToGlobal(new Point(someMovingObject.x, someMovingObject.y)); 
point = someMovingObject.localToGlobal(new Point());
// both give us same results, 
// 2nd method is faster in loops while it can use predefined zero point

hitShape.hitTestPoint(point.x, point.y, true);

Even hitShape object is not directly on a stage (but on myInnerObject), you have to use global points for testing hits.

6 comments so far

  1. MrKishi June 10, 2010 19:02

    The first method may be slower — but even more importantly, it’s not generic.
    I mean, if someMovingObject is inside somewhere else, you’d need to do something such as ‘someMovingObject.parent.local..’. Ugly stuff.

  2. Jozef Chúťka June 10, 2010 22:54

    @MrKishi, sure thing, thnx for noticing that 😉

  3. rtoprani July 27, 2010 03:36

    Hey Jozef,
    would it be possible to show how it would be done when ‘hitShape’ (from your example above) is nested a few movieClips below (my_mc1.my_mc.2.hitShape)?

    Thanks!

  4. Jozef Chúťka July 27, 2010 09:37

    @rtoprani Hi, what exatcly is function hitShape()? It seems this one does not exist in AS3, anyway you can use hitTestObject() and this one should work no matter where the objects are parented

  5. Leo September 19, 2011 02:04

    First of all, Amazing stuff you have. Awesome website. Congratz… I am way beyond impressed.

    Now, I was having a lot of trouble with this issue, and this thread saved me. Here’s what I think rtoprani wanted to say:

    What if on the example of code at the top you have the someMovingObject inside somewhere else, and you’d need to do something such as ‘someMovingObject.parent.local.. the way MrKishi is implying.

    Say the display list is something like this:

    currentLevel.foreGround.someMovingObject

    and you want to use the hitTestPoint against an object located at:

    currentLevel.foreGround.rocks

    How would that look like?

    Would this be correct?
    //************
    var point:Point;
    point = currentLevel.foreGround.someMovingObject.localToGlobal(new Point());
    currentLevel.foreGround.rocks.hitTestPoint(point.x, point.y, true);
    //***********

    Is always confusing because I know the hitTestPoint works with the stage coordinates, and this is all done well within a display list with childs… so… just confusing…

    Your help would be awesome. Thanks and cheers!!

  6. Jozef Chúťka September 19, 2011 09:43

    hi Leo, yes I believe your example is ok for hittesting point(0, 0) in someMovingObject against rocks

Leave a comment

Please be polite and on topic. Your e-mail will never be published.