时间轴上的MovieClip与actionscript中绘制的一个之间的碰撞检测

时间:2012-01-19 11:38:21

标签: flash actionscript-3 actionscript collision-detection

我正在制作一个让用户用鼠标在屏幕上绘制形状的程序。当他们完成后,按一个按钮完成。用户只能在舞台的某个区域上绘制,为了在时间轴上控制它,我有一个影片剪辑,占用了用户不能画的所有空间。

当用户按下按钮时,我希望程序检查用户绘制的形状是否接触到moiveclip。我希望它能够在不考虑movieclip边框的情况下执行此操作,因此我无法使用hitTestObject()

到目前为止我所拥有的是什么:

//------------------------------DRAW SHAPE------------------------------
    private var shape:MovieClip = new MovieClip();

    public function startDraw(e:MouseEvent):void {
        shape.graphics.moveTo(mouseX, mouseY);
        shape.graphics.lineStyle(4, 0x000000, 0.8);

        stage.addEventListener(MouseEvent.MOUSE_MOVE, beginDraw);
    }

    public function beginDraw(e:MouseEvent):void {
        shape.graphics.lineTo(mouseX, mouseY);
    }

    public function stopDraw(e:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, beginDraw);
    }
//--------------------------CHECK FOR COLLISION---------------------------
    public function bclick(e:MouseEvent):void {
        if(cd(mcOnTimeline, shape)){
            trace("HIT IT");
        }else{
            trace("DIDNT HIT");
        }
    }
    public function cd(mc1:MovieClip, mc2:MovieClip):Boolean{
        var mc1Bounds:Object = mc1.getBounds(mc1);
        var mc2Bounds:Object = mc2.getBounds(mc1);
        //return(mc1Bounds.intersects(mc2Bounds));

        var mc1BmpD = new BitmapData(mc1Bounds.width, mc1Bounds.height, true, 0);
        var mc2BmpD = new BitmapData(mc2Bounds.width, mc2Bounds.height, true, 0);
        //mc1BmpD.draw(mc1Bounds);
        //mc2BmpD.draw(mc2Bounds);
        //return(mc1BmpD.intersects(mc2BmpD));

        if(mc1BmpD.hitTest(new Point(mc1Bounds.x, mc1Bounds.y), 255, mc2BmpD, new Point(mc2Bounds.x, mc2Bounds.y),255)){
            return true;
        }
        else{
            return false;
        }

注释掉的东西是我试图让这个工作不同的东西

2 个答案:

答案 0 :(得分:1)

我会在画画的同时进行测试;当用户正在绘制(鼠标按钮)时,测试鼠标x,y是否正在击中动画片段。如果是这样,请相应地设置一个标志。

答案 1 :(得分:1)

根据hitTest形状的复杂程度,您可能需要考虑使用BitmapData.hitTest(),这应该更有效。查看Mike Chamber's blog post on the subject