2014-11-02 133 views
1

我已经对框架采取了操作,我正在尝试执行的操作有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 
    } 
} 

回答

1

你可以先使用copyFrom方法在功能lineDraw,因为alreadyDrawn它的测试之前,必须先抽!

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

这工作,但不正确,因为则hitTest认为rectanglealreadyDrawn。您必须考虑要测试的point是您的mouse

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