【发布时间】:2014-01-23 04:59:50
【问题描述】:
我正在尝试在用户用鼠标绘制的地方制作迷宫,如果他们撞到墙上,它会擦除他们刚刚绘制的线。我有一个带有 alpha 的 png 文件,可以创建迷宫的墙壁。
我需要用户在 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;
}
}
}
【问题讨论】:
-
是的,您需要在这里进行某种像素完美碰撞检测。 Chech SO 对于类似的问题,我发现了这个stackoverflow.com/questions/16846470/… 虽然它可能不完全是你的情况。还有更明显的。并检查 PPCD 库。
-
Mike Chambers 的这篇文章也有帮助:mikechambers.com/blog/2009/06/24/…
标签: actionscript-3 alpha hittest