as3 hitTestPoint检测alpha

时间:2014-01-23 04:59:50

标签: actionscript-3 alpha hittest

我正在尝试制作用户使用鼠标绘制的迷宫,如果他们撞到墙壁,它会删除他们刚绘制的线条。我有一个带有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;
        }



    }
}

2 个答案:

答案 0 :(得分:0)

另一种选择是测试对应于“玩家位置”的地图位图数据像素的颜色(在这种情况下,绘制线的末尾):

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

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

答案 1 :(得分: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;

                }
            }
        }

    }
}