使用Graphics.lineTo / curveTo时HitTest不能正常工作 [英] HitTest not working correctly when using Graphics.lineTo/curveTo

查看:219
本文介绍了使用Graphics.lineTo / curveTo时HitTest不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个名为 hookLine 的影片剪辑,这些剪辑从我的 mainEngine 类添加到舞台上。这个空的movieClip连接到我的 fisherman 影片剪辑并曲线到我的 playerHook 影片剪辑。它添加并连接到舞台,如下所示:



在我的 mainEngine 函数循环中:

  playerHookLine(); 

然后函数:

  private function playerHookLine():void 
{

//将钩子线添加到fisherman和playerhook
hookLine.graphics.clear();
hookLine.graphics.lineStyle(1);
hookLine.graphics.moveTo(fisherman.x,fisherman.y);
hookLine.graphics.curveTo(playerHook.x,playerHook.y,mouseX,mouseY);


$ / code>

现在我遇到的问题是每当我尝试hitTest带有Move Clip的 hookLine 命名为 currentShark hitTest可以工作,并且我得到一个跟踪,但它根本不准确我将我的钩线弯曲到两侧,并且 currentShark 出现在舞台上,它会自动击中测试并给出跟踪。所以基本上鲨鱼甚至不必接触实际的线条图形。正当鲨鱼加入舞台时,它只是登记。

有人知道这是为什么吗?



以下是hitTest函数的用法:

  private function checkPlayerHitShark ():void 
{
//遍历所有鲨鱼
for(var i:int = 0; i< aSharkArray.length; i ++)
{
//获取当前鲨鱼的循环
var currentShark:mcShark = aSharkArray [i];

//检查鲨鱼是否被Hook
击中if(currentShark.hitTestObject(playerHook)|| currentShark.hitTestObject(hookLine))
{
trace(钩击鲨鱼);
trace(hit LINE);
removePlayerLive();

//销毁播放器
playerHook.destroyPlayerHook();
hookLine.destroyHookLine();

//从阵列中删除鲨鱼
aSharkArray.splice(i,1);

//添加新的Hook到stage
stage.addChild(playerHook);
stage.addChild(hookLine);
}


}

}


解决方案

很可能您的鲨鱼和钓鱼线的边界框发生碰撞。当您的弧形钓鱼线向左或向右移动时,您的边界框将与钓鱼线本身的宽度和高度相同。打开您的项目并发布为SWF,然后在Flash播放器中打开SWF,然后按Control + E或点击窗口顶部的视图并选择显示重绘区域。你应该看到边框被重新绘制到舞台上。



你正在寻找的是你的鲨鱼和钓鱼线位图像素级命中检测。 BitmapData有一个名为hitTest的方法,它需要几个参数。

您可以从链接中的Mike Chambers撰写的文章中找到对像素级命中检测的出色帮助:
http://www.mikechambers.com/blog/2009/06 / 24 / using-bitmapdata-hittest-for-collision-detection /

BitmapData.hitTest的文档可以在这里找到:
< a href =http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html =nofollow> http://help.adobe.com/en_US/ FlashPlatform / reference / actionscript / 3 / flash / display / BitmapData.html



只需查找公共方法列表。


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);
            }


        }

    }

解决方案

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.

这篇关于使用Graphics.lineTo / curveTo时HitTest不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆