Actionscript 3 as3命中测试点检测alpha
我试图在用户用鼠标绘制的地方制作一个迷宫,如果他们撞到墙上,它会擦除他们刚才画的线。我有一个带有alpha的png文件,可以创建迷宫的墙 我需要用户在alpha上绘制,但当用户点击非alpha时,它将触发一个动作并擦除线条。 以下是我遇到的问题:Actionscript 3 as3命中测试点检测alpha,actionscript-3,alpha,hittest,Actionscript 3,Alpha,Hittest,我试图在用户用鼠标绘制的地方制作一个迷宫,如果他们撞到墙上,它会擦除他们刚才画的线。我有一个带有alpha的png文件,可以创建迷宫的墙 我需要用户在alpha上绘制,但当用户点击非alpha时,它将触发一个动作并擦除线条。 以下是我遇到的问题: if (myshape.hitTestPoint(theBall.x,theBall.y, true)) 以下是完整的代码: package { import flash.display.Sprite; import flash.e
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;
}
}
}
另一个选项是测试对应于“玩家位置”(在本例中为绘制线的末端)的地图位图数据像素的颜色:
或者您可以使用.getPixel32,它在返回的uint中包含alpha。我找到了一个有效的解决方案: 多亏了你,我找到了一个有效的解决方案。非常感谢。我张贴我的完整代码,希望这可以帮助别人 最后,我不得不使用另一个电影唇而不是鼠标,但这对我来说很有效。现在,他们不用在迷宫中的任何地方画画,而是要把一个名叫博的小角色拖过迷宫。这对我更有效 如果知道如何检测鼠标或我正在绘制的实际线条是否也碰到墙,那就太好了。也许有人可以提个建议。不过,谢谢你帮我走到这一步。 下面可以找到使我的movieClip检测alpha的简短而重要的代码。与我发现的其他复杂选项相比,它确实非常短。以下是链接:
包
{
导入flash.display.Sprite;
导入flash.events.MouseEvent;
导入flash.display.DisplayObject;
导入flash.display.Graphics;
导入flash.display.JointStyle;
导入flash.display.LineScaleMode;
导入flash.display.Shape;
导入flash.display.Sprite;
导入flash.events.*;
导入flash.events.KeyboardEvent;
导入flash.ui.Keyboard;
导入flash.events.TouchEvent;
导入flash.ui.multi-touch;
导入flash.ui.multi-touchinputmode;
导入flash.desktop.NativeApplication;
导入flash.system.Capabilities;
导入flash.display.*;
导入flash.geom.*;
导入flash.display.BitmapData;
导入flash.display.Bitmap;
导入flash.geom.Matrix;
公共类MazeClass扩展了Sprite
{
//我们是不是在画画?
私有变量绘制:布尔;
公共形状:形状;
公共变量已绘制:形状;
公共var主题地图:*;
公共变量swiper:swiper=新swiper();
public var Beau:littleBeau=新littleBeau();
//alreadyDrawn=新形状();
公共变量currentGalleryItem:Number=1;
公共var totalGalleryItems:Number=4;
公共变量redClipBmpData:BitmapData;
公共var blueClipBmpData:BitmapData;
公共变量redRect:矩形;
公共变量blueRect:矩形;
公共函数MazeClass()
{
如果(阶段)
{
theMap=newmapone();
myshape=新形状();
myshape.graphics.lineStyle(33,0x0aa6df);
addChild(myshape);
drawing=false;//以
stage.addEventListener(MouseEvent.MOUSE_向下,开始绘制);
stage.addEventListener(MouseEvent.MOUSE_MOVE,draw);
stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing);
//stage.addEventListener(Event.ENTER_FRAME,checkIt);
stage.addEventListener(KeyboardEvent.KEY_UP,fl_Options MenuHandler);
Multitouch.inputMode=MultitouchInputMode.手势;
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.手势\u滑动,fl\u滑动到ExtPreviousFrame);
阿迪奇尔德(博);
Beau.x=theMap.theStart.x;
Beau.y=地图开始点y-游泳高度;
Beau.addEventListener(MouseEvent.MOUSE_DOWN,BeauStartDrag);
Beau.addEventListener(MouseEvent.MOUSE_UP,BeautoDrag);
redRect=theMap.getBounds(这个);
redClipBmpData=新的位图数据(redRect.width,redRect.height,true,0);
redClipBmpData.draw(theMap);
blueRect=beu.getBounds(this);
blueClipBmpData=新位图数据(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\u FRAME,enterFrame);
}
}
公共功能enterFrame(e:事件):无效
{
//Beau.x=鼠标;
//Beau.y=老鼠;
如果(redClipBmpData.hitTest)(新点(地图x,地图y),
255,
blueClipBmpData,
新点(Beau.x,Beau.y),
255
))
{
跟踪(“命中”);
clearAll();
//redClip.filters=[new GlowFilter()];
}
其他的
{
跟踪(“无命中”);
}
}
公共功能BeautStartDrag(事件:MouseeEvent):无效
{
Beau.startDrag();
绘图=真;
}
公共函数BeauStopDrag(事件:MouseeEvent):无效
{
图纸=假;
Beau.stopDrag();
}
公共函数开始绘图(事件:MouseEvent):无效
{
myshape.graphics.moveTo(mouseX,mouseY);
}
//绘图=真;
var testPixel:uint = _myBitmapData.getPixel(xPosition, yPosition);
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;
}
}
}
}
}