2014-11-02 46 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"); 
    } 
}