HitTest not working correctly when using Graphics.lineTo/curveTo

大城市里の小女人 提交于 2019-12-11 16:46:38

问题


So I have a Movie Clip called hookLine thats added to the stage from my mainEngine class. This empty movieClip is connected to my fisherman Movie Clip and curves to my playerHook Movie Clip. Its added and connected to the stage like so:

In My mainEngine function loop:

playerHookLine();

Then the Function:

private function playerHookLine():void 
    {

        //Add hook line to fisherman and playerhook
        hookLine.graphics.clear();
        hookLine.graphics.lineStyle(1);
        hookLine.graphics.moveTo(fisherman.x, fisherman.y);
        hookLine.graphics.curveTo(playerHook.x, playerHook.y, mouseX, mouseY);

    }

Now the problem I am having is whenever I try to hitTest the hookLine with a Move Clip called currentShark the hitTest works and I get a trace, but its not ACCURATE at all when I curve my hook line to the sides and the currentShark comes on stage it automatically hitTests and gives me the trace. So basically the shark doesnt even have to come in contact with the actual Line Graphic. Right when the shark is added to the stage it just registers.

Does anyone have any idea why this is?

Here is how the hitTest function is:

private function checkPlayerHitShark():void 
    {
        //Loop through all sharks
        for (var i:int = 0; i < aSharkArray.length; i++)
        {
            //Get current Shark in i loop
            var currentShark:mcShark = aSharkArray[i];

            //Check if shark is hittest with Hook
            if (currentShark.hitTestObject(playerHook) || currentShark.hitTestObject(hookLine))
            {
                trace("Hook Hit Shark");
                trace("hit LINE");
                removePlayerLive();

                //Destroy player 
                playerHook.destroyPlayerHook();
                hookLine.destroyHookLine();

                //Remove shark from array
                aSharkArray.splice(i, 1);

                //Add new Hook to stage
                stage.addChild(playerHook);
                stage.addChild(hookLine);
            }


        }

    }

回答1:


More than likely the bounding boxes of your shark and fishing line are colliding. As your curved fishing line moves to the left or right, your bounding box will be the same as the width and height of the fishing line itself. Open your project and publish as SWF, then open the SWF in Flash player and press Control+E or click on View at the top of the window and select "Show Redraw Regions". You should see the bounding boxes in red as they are redrawn to the stage.

What you are looking for is pixel level hit detection on the bitmaps of your shark and fishing line. BitmapData has a method called hitTest which will require a few parameters.

You are going to find excellent help for pixel level hit detection from an article written by Mike Chambers at the link here: http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

The documentation for BitmapData.hitTest can be found here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html

Just look for the list of public methods.



来源:https://stackoverflow.com/questions/21923615/hittest-not-working-correctly-when-using-graphics-lineto-curveto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!