简体   繁体   中英

hitTestPoint() shapeFlag not working on movieClip containing loaded swf

I have a .swf file containing a circle that I load from a URL, make into a movieClip and add it to the stage.

I would like to be able to detect when the user places the cursor over the circle. I'd like it to be accurate in a sense that it doesn't simply use the bounding box of the circle, but the actual circle itself to detect a collision. This is a fragment of my current code:

    //
    var myCircle:MyCircle = new MyCircle(swf);
    myCircle.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    myContainer.addChild(myCircle);
    //

    private function onMouseMove(event:MouseEvent):void
    {
        var glowFilter:GlowFilter = new GlowFilter();
        glowFilter.color = 0xffffff;
        if (event.currentTarget is MyCircle) {
            var object:MyCircle = event.currentTarget as MyCircle;
            if(object.hitTestPoint(event.stageX, event.stageY, true))
                event.currentTarget.filters = [glowFilter]; 
            else
                event.currentTarget.filters = []; 
        }
    }

I was led to believe that the shapeFlag parameter to the hitTestPoint() method would do this, but setting it either true or false makes no difference. Any ideas why this might be?

I wrote a very short blog post about this earlier so I had it for future times...

It seems like you are looking at stageX which AFAIK is the global x-coordinate of the object.

http://messer1024.blogspot.se/2012/06/proper-hittest-in-as3.html

Short version of a short post:

After many ugly attempts at handling hit tests in AS3 I finally managed to solve what I was looking for. The thing I forgot to consider was to use the global mouse-coordinates.

public function hitTestMouse(hitarea:DisplayObject):Boolean {
    return hitarea.hitTestPoint(hitarea.stage.mouseX, hitarea.stage.mouseY, true);
}

The last parameter (shapeFlag) would decide if the hit test are checked any actual shapes inside the hitarea that the mouse is hovering above.


So in order for your thing to work, you should probably make it a bit easier and add my function and then run "hitTestMouse(myCircle)" when you feel like testing it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM