2014-01-23 67 views
0

我試圖讓迷宮在用戶使用鼠標繪製時,如果他們撞到牆上,它會擦除​​他們剛繪製的線條。我有一個用alpha創建迷宮牆的png文件。as3 hitTestPoint檢測阿爾法

我需要用戶在alpha上繪圖,但是當它們碰到非alpha時,它會觸發一個動作並擦除這一行。

這裏是我有問題行:

if (myshape.hitTestPoint(theBall.x,theBall.y, true)) 

下面是完整的代碼:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    import flash.display.DisplayObject; 
    import flash.display.Graphics; 
    import flash.display.JointStyle; 
    import flash.display.LineScaleMode; 
    import flash.display.Shape; 
    import flash.display.Sprite; 
    import flash.events.*; 



    public class MazeClass extends Sprite 
    { 
     //Are we drawing or not? 
     private var drawing:Boolean; 
     public var myshape:Shape; 
     public var alreadyDrawn:Shape; 
     public var theBall:Ball = new Ball(); 
     //alreadyDrawn = new Shape(); 


     public function MazeClass() 
     { 

      if (stage) 
      { 

       myshape = new Shape(); 
       myshape.graphics.lineStyle(12,0x000000); 
       addChild(myshape); 
       drawing = false;//to start with 
       stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing); 
       stage.addEventListener(MouseEvent.MOUSE_MOVE, draw); 
       stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing); 
       //stage.addEventListener(Event.ENTER_FRAME, checkIt); 

       addChild(theBall); 
      } 
     } 


     public function startDrawing(event:MouseEvent):void 
     { 

      myshape.graphics.moveTo(mouseX, mouseY); 
      drawing = true; 
     } 


     public function draw(event:MouseEvent) 
     { 
      if (drawing) 
      { 
       //checkIt(); 
       myshape.graphics.lineTo(mouseX,mouseY); 

       if (myshape.hitTestPoint(theBall.x,theBall.y, true)) 
       { 
        trace("Hit A WALL!"); 
        myshape.graphics.clear(); 
        myshape.graphics.lineStyle(12, 0xFFFFFF); 
        myshape.graphics.moveTo(mouseX,mouseY); 
       } 
      } 
     } 


     public function stopDrawing(event:MouseEvent) 
     { 
      drawing = false; 
     } 



    } 
} 
+1

是的,你需要某種像素完美的碰撞檢測在這裏。謝謝SO類似的問題,我發現這個http://stackoverflow.com/questions/16846470/efficient-collision-detection-in-as3雖然它可能不是你的情況。有更明顯的。並檢查PPCD庫。 – Vesper

+1

邁克錢伯斯的這篇文章也可以幫助:http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/ – fsbmain

回答

0

另一種選擇是,以測試對應於地圖的BitmapData像素的顏色「玩家的位置」(本例中畫出的線的末端):

var testPixel:uint = _myBitmapData.getPixel(xPosition, yPosition); 

或者您可以在返回的uint中使用包含alpha的.getPixel32。

+0

有趣。在我的代碼中看起來像什麼,我會在哪裏放?在輸入框? –

+1

如果您的迷宮是在舞臺上以與您的位圖相同的比例「創建」的,您可以用上面提供的代碼替換您的hitTest代碼(在'draw'中),其中_myBitmapData是您的地圖的BitmapData對象和xPosition/yPosition是mouseX/mouseY。如果舞臺地圖相對於地圖位圖縮放,則可以按比例縮放xPosition和yPosition(即;如果地圖繪製比位圖大2倍,則將鼠標線分爲2以檢查相應的像素)。一旦你有像素的顏色,檢查它是否是牆的顏色。 – moosefetcher

+1

請注意,如果您必須劃分鼠標位置值,則應使用Math.floor(mouseX/stageMapScale)忽略任何小數值(.getPixel需要像素檢查的int值)。另外一個想法是:如果你的舞臺地圖沒有在舞臺上定位在0,0,你將不得不減去這些座標(這樣當鼠標在舞臺地圖的左上角時,你正在檢查像素0,0 BitmapData)。我希望這一切都清楚! – moosefetcher

0

我發現了一個有效的解決方案:

我找到了工作液感謝fsbmain。謝謝。我發佈我的完整代碼,希望這可以幫助別人。

最後,我不得不使用另一個movieclip,而不是鼠標,但這對我很有用。現在,他們不必畫迷宮中的任何地方,而必須拖曳一個名爲Beau的小角色穿過迷宮。這對我更好。

這將是很高興知道如何也檢測鼠標或我正在繪製的實際線條也擊中了牆。也許有人可以提出建議。儘管如此,感謝幫助我獲得這些。

使我的movieClip檢測alpha的簡短代碼可以在這裏找到。與我找到的其他複雜選項相比,它確實非常短。這裏是鏈接:http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

package 
{ 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    import flash.display.DisplayObject; 
    import flash.display.Graphics; 
    import flash.display.JointStyle; 
    import flash.display.LineScaleMode; 
    import flash.display.Shape; 
    import flash.display.Sprite; 
    import flash.events.*; 
    import flash.events.KeyboardEvent; 
    import flash.ui.Keyboard; 

    import flash.events.TouchEvent; 
    import flash.ui.Multitouch; 
    import flash.ui.MultitouchInputMode; 

    import flash.desktop.NativeApplication; 
    import flash.system.Capabilities; 

    import flash.display.*; 
    import flash.geom.*; 

    import flash.display.BitmapData; 

    import flash.display.Bitmap; 

    import flash.geom.Matrix; 

    public class MazeClass extends Sprite 
    { 
     //Are we drawing or not? 
     private var drawing:Boolean; 
     public var myshape:Shape; 
     public var alreadyDrawn:Shape; 
     public var theMap:*; 

     public var swiper:Swiper = new Swiper(); 

     public var Beau:littleBeau = new littleBeau(); 

     //alreadyDrawn = new Shape(); 


     public var currentGalleryItem:Number = 1; 
     public var totalGalleryItems:Number = 4; 

     public var redClipBmpData:BitmapData; 
     public var blueClipBmpData:BitmapData; 
     public var redRect:Rectangle; 
     public var blueRect:Rectangle; 

     public function MazeClass() 
     { 

      if (stage) 
      { 
       theMap = new MapOne(); 

       myshape = new Shape(); 
       myshape.graphics.lineStyle(33,0x0aa6df); 
       addChild(myshape); 
       drawing = false;//to start with 
       stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing); 
       stage.addEventListener(MouseEvent.MOUSE_MOVE, draw); 
       stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing); 
       //stage.addEventListener(Event.ENTER_FRAME, checkIt); 


       stage.addEventListener(KeyboardEvent.KEY_UP, fl_OptionsMenuHandler); 

       Multitouch.inputMode = MultitouchInputMode.GESTURE; 




       addChild(theMap); 
       //theMap.height = stage.stageHeight - 200; 
       theMap.theStart.alpha = 0; 
       theMap.theFinish.alpha = 0; 


       addChild(swiper); 
       swiper.y = stage.stageHeight - swiper.height; 
       swiper.addEventListener(TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrame); 

       addChild(Beau); 
       Beau.x = theMap.theStart.x; 
       Beau.y = theMap.theStart.y - swiper.height; 

       Beau.addEventListener(MouseEvent.MOUSE_DOWN, BeauStartDrag); 
       Beau.addEventListener(MouseEvent.MOUSE_UP, BeauStopDrag); 



       redRect = theMap.getBounds(this); 
       redClipBmpData = new BitmapData(redRect.width,redRect.height,true,0); 
       redClipBmpData.draw(theMap); 

       blueRect = Beau.getBounds(this); 
       blueClipBmpData = new BitmapData(blueRect.width,blueRect.height,true,0); 
       blueClipBmpData.draw(Beau); 

       //blueClipBmpData.x = theMap.theStart.x; 
       //blueClipBmpData.y = theMap.theStart.y - swiper.height; 

       stage.addEventListener(Event.ENTER_FRAME, enterFrame); 

      } 
     } 




     public function enterFrame(e:Event):void 
     { 
      //Beau.x = mouseX; 
      //Beau.y = mouseY; 

      if (redClipBmpData.hitTest(new Point(theMap.x, theMap.y), 
              255, 
              blueClipBmpData, 
              new Point(Beau.x, Beau.y), 
              255 

            )) 
      { 
       trace("hit"); 
       clearAll(); 
       //redClip.filters = [new GlowFilter()]; 
      } 
      else 
      { 
       trace("No Hit"); 
      } 
     } 





     public function BeauStartDrag(event:MouseEvent):void 
     { 

      Beau.startDrag(); 
      drawing = true; 
     } 
     public function BeauStopDrag(event:MouseEvent):void 
     { 
      drawing = false; 
      Beau.stopDrag(); 
     } 

     public function startDrawing(event:MouseEvent):void 
     { 

      myshape.graphics.moveTo(mouseX, mouseY); 
     } 
     //drawing = true; 


     public function draw(event:MouseEvent) 
     { 
      if (drawing) 
      { 
       //checkIt(); 
       myshape.graphics.lineTo(mouseX,mouseY); 


      } 
     } 


     public function stopDrawing(event:MouseEvent) 
     { 
      //drawing = false; 
     } 


     public function fl_OptionsMenuHandler(event:KeyboardEvent):void 
     { 
      if ((event.keyCode == 95) || (event.keyCode == Keyboard.MENU)) 
      { 
       NativeApplication.nativeApplication.exit(0); 
      } 
     } 

     public function clearAll() 
     { 
      myshape.graphics.clear(); 
      myshape.graphics.lineStyle(12, 0x0aa6df); 
      myshape.graphics.moveTo(mouseX,mouseY); 
      Beau.x = theMap.theStart.x; 
        Beau.y = theMap.theStart.y - swiper.height; 
        drawing = false; 
      Beau.stopDrag(); 
     } 





     public function fl_SwipeToGoToNextPreviousFrame(event:TransformGestureEvent):void 
     { 
      if (event.offsetX == 1) 
      { 
       if (currentGalleryItem > 1) 
       { 
        currentGalleryItem--; 
        trace("swipe Right"); 
        clearAll(); 
        removeChild(theMap); 
        theMap = new MapOne(); 
        addChild(theMap); 
        theMap.height = stage.stageHeight - 200; 

        theMap.theStart.alpha = 0; 
        theMap.theFinish.alpha = 0; 
        addChild(Beau); 
        Beau.x = theMap.theStart.x; 
        Beau.y = theMap.theStart.y - swiper.height; 
       } 
      } 
      else if (event.offsetX == -1) 
      { 
       if (currentGalleryItem < totalGalleryItems) 
       { 
        currentGalleryItem++; 
        trace("swipe Left"); 
        clearAll(); 
        removeChild(theMap); 
        theMap = new MapTwo(); 
        addChild(theMap); 
        theMap.height = stage.stageHeight - 200; 

        theMap.theStart.alpha = 0; 
        theMap.theFinish.alpha = 0; 
        addChild(Beau); 
        Beau.x = theMap.theStart.x; 
        Beau.y = theMap.theStart.y - swiper.height; 

       } 
      } 
     } 

    } 
}