我試圖讓迷宮在用戶使用鼠標繪製時,如果他們撞到牆上,它會擦除他們剛繪製的線條。我有一個用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;
}
}
}
是的,你需要某種像素完美的碰撞檢測在這裏。謝謝SO類似的問題,我發現這個http://stackoverflow.com/questions/16846470/efficient-collision-detection-in-as3雖然它可能不是你的情況。有更明顯的。並檢查PPCD庫。 – Vesper
邁克錢伯斯的這篇文章也可以幫助:http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/ – fsbmain