【问题标题】:as3 hitTestPoint detecting alphaas3 hitTestPoint 检测 alpha
【发布时间】: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;
        }



    }
}

【问题讨论】:

标签: actionscript-3 alpha hittest


【解决方案1】:

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

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

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

【讨论】:

  • 有趣。在我的代码中会是什么样子,我会把它放在哪里?在输入框上?
  • 如果您的迷宫是在舞台上以与位图相同的比例“创建”的,您可以用我上面提供的代码替换您的 hitTest 代码(在“draw”中),其中 _myBitmapData 是 BitmapData地图位图的对象和 xPosition/yPosition 是 mouseX/mouseY。如果舞台贴图相对于贴图位图进行缩放,您可以按该比例缩放 xPosition 和 yPosition(即;如果绘制的贴图比位图大 2 倍,则将鼠标线除以 2 以检查相应的像素)。一旦你有了像素颜色,检查它是否是墙壁的颜色。
  • 请注意,如果您必须除以鼠标位置值,则应使用 Math.floor(mouseX / stageMapScale) 忽略任何小数值(.getPixel 需要 int 值进行像素检查)。另一个想法:如果您的舞台地图不在舞台上的 0, 0 处,您将不得不减去这些坐标(这样当鼠标位于舞台地图的左上角时,您正在检查像素 0, 0位图数据)。我希望这一切都清楚了!
  • 非常感谢。我更多地在想,比如我应该在哪个部分添加我的代码。例如,我是在声明值的地方添加它,还是将它放在一个函数中并且它被调用多次或每秒多次进入帧或 if语句如... if(绘图)。我确实让它工作了。我将发布我的工作代码。我必须添加一个我拖动的影片剪辑,而不是鼠标本身和我正在绘制的线条。要是知道怎么做鼠标就好了。
【解决方案2】:

我找到了一个可行的解决方案:

感谢fsbmain,我找到了一个可行的解决方案。谢谢你。我正在发布我的完整代码,希望这可以帮助其他人。

最后我不得不使用另一个电影剪辑而不是鼠标,但这对我有用。现在,他们不必在迷宫中的任何地方绘制,而是必须拖动一个名为 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;

                }
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多