【问题标题】:Drawing line collision?画线碰撞?
【发布时间】:2014-12-29 05:25:31
【问题描述】:

我已经在一个框架上执行了操作,我想要做的是有一个hitTest,当我正在绘制的形状与touchTest 碰撞时触发gotoAndStop(<lose frame>)。我遇到的唯一问题是我无法让hitTest 在线路命中时直接注册,它只会在下一次点击事件后注册。我遇到的另一个问题是touchTest 上的点击框比符号的实际图像大很多倍。

var myshape:Shape;
myshape = new Shape();
myshape.graphics.lineStyle(5, 0xC807DE);
var alreadyDrawn:Shape;
alreadyDrawn = new Shape();

stage.addEventListener(MouseEvent.MOUSE_DOWN, activateDraw);
function activateDraw(event:MouseEvent):void
{
    myshape.graphics.moveTo(mouseX,mouseY);
    addChild(myshape);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
}

function lineDraw(event:MouseEvent):void
{
    myshape.graphics.lineTo(mouseX,mouseY);
    checkIt();
}
function stopDraw(event:MouseEvent):void
{
    alreadyDrawn.graphics.copyFrom(myshape.graphics);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    stage.removeEventListener(MouseEvent.MOUSE_UP, stopDraw);
}

function checkIt()
{
    if (alreadyDrawn.hitTestObject(touchTest) == true)
    {
        trace("wall");
        myshape.graphics.clear();
        myshape.graphics.lineStyle(5, 0xC807DE);
        alreadyDrawn.graphics.clear(); // clear this too
        stopDraw(null); // stop active draw, if any
    }
}

【问题讨论】:

    标签: actionscript-3 flash actionscript flash-cs6


    【解决方案1】:

    您可以先在函数lineDraw 中使用copyFrom 方法,因为alreadyDrawn 必须在测试之前绘制!

    function lineDraw(event:MouseEvent):void
    {
        myshape.graphics.lineTo(mouseX,mouseY);
        alreadyDrawn.graphics.copyFrom(myshape.graphics);
        checkIt();
    }
    

    这可行,但不正确,因为 hitTest 认为 rectangle 包含 alreadyDrawn。您必须考虑到要测试的point 是您的mouse

    function checkIt():void
    {
        if (touchTest.hitTestPoint(mouseX, mouseY, true))
        {
            trace("wall");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2013-04-19
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多