需要一些帮助来完成一个碰撞选择工具

时间:2010-08-29 05:29:37

标签: actionscript-3 flash bumptop

我正在创建一个Bumptop风格的选择工具。现在我开始创建工具本身(实际上工作得很好)并在舞台上传播一些随机的方形项目。这是创建选择工具的类:

package com.reyco1.medusa.selectiontool
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    public class SelectionBase extends Sprite
    {
        private var points:Array = [];

        public function SelectionBase()
        {
            super();
            addEventListener(Event.ADDED_TO_STAGE, initialize);
        }

        private function initialize(e:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, initialize);

            points.push(new Point(mouseX, mouseY));          stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
        }

        private function handleMouseMove(e:MouseEvent):void
        {           
            graphics.clear();

            graphics.beginFill(0x33CCFF, .5);
            graphics.drawCircle(0, 0, 20);
            graphics.endFill();

            graphics.moveTo(0, 0);
            graphics.lineStyle(1.5, 0x33CCFF, .5);
            graphics.lineTo(mouseX, mouseY);

            points.push(new Point(mouseX, mouseY));

            graphics.beginFill(0x33CCFF, .1);
            graphics.moveTo(points[0].x, points[0].y);

            for (var i:uint = 1; i < points.length; i++)
            {
                graphics.lineTo(points[i].x, points[i].y);
            }

            graphics.lineTo(points[0].x, points[0].y);
            graphics.endFill();

            dispatchEvent(new Event("UPDATE"));
        }

        public function clear():void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
            graphics.clear();
        }
    }
}

这是实现它的文档类:

package
{
    import com.reyco1.medusa.selectiontool.SelectionBase;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.StageAlign;
    import flash.display.StageQuality;
    import flash.display.StageScaleMode;

    [SWF(width = '1024', height = '768', backgroundColor = '0x000000')]
    public class SelectionToolPrototype extends Sprite
    {
        private var selectionTool:SelectionBase;

        public function SelectionToolPrototype()
        {                         
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.quality = StageQuality.MEDIUM;
                    stage.addEventListener(MouseEvent.MOUSE_DOWN, handleDown);
            stage.addEventListener(MouseEvent.MOUSE_UP,     handleUp);  

            placeShapesRandomly();
        }

        private function placeShapesRandomly():void
        {
            for(var a:Number = 0; a<25; a++)
            {
                var s:Sprite = new Sprite();
                s.graphics.beginFill(Math.random() * 0xCCCCCC);
                s.graphics.drawRect(0, 0, 50, 50);
                s.graphics.endFill();
                s.x = Math.floor(Math.random() * 900 - 40) + 40;
                s.y = Math.floor(Math.random() * 700 - 40) + 40;
                s.rotation =  Math.floor(Math.random() * 360 - 40) + 40;
                s.buttonMode = true;
                addChild(s);
            }
        }

        private function handleUp(e:MouseEvent):void
        {
            selectionTool.removeEventListener("UPDATE", handleToolUpdate);
            removeChild(selectionTool);
            selectionTool = null;
        }

        private function handleDown(e:MouseEvent):void
        {
            selectionTool = new SelectionBase();
            selectionTool.addEventListener("UPDATE", handleToolUpdate);
            selectionTool.x = mouseX;
            selectionTool.y = mouseY;
            addChild(selectionTool);
        }

        private function handleToolUpdate(e:Event):void
        {
            // logic to determin if items are within selection goes here        
        }
    }
}

我尝试过使用BitmapData进行碰撞检测,甚至使用像CDK这样的碰撞库,但是我无法使用任何东西。任何人都知道我应该在handleToolUpdate(e:MouseEvent);中使用什么?谢谢!

更新

我会把它分解。基本上我正在尝试创建BumpTop套索或选择工具的原型。

我需要帮助找出哪些物体碰撞或在绘制的套索范围内有一个点。

我已将目前为止的内容上传到我的服务器:http://labs.reyco1.com/bumptop/SelectionToolPrototype.html。您可以通过右键单击并选择“查看源”来查看源。

就像我在之前的帖子中所说,我尝试使用Bitmapdata碰撞测试,甚至尝试使用碰撞检测套件无济于事。提前谢谢。

1 个答案:

答案 0 :(得分:0)

循环显示您将随机精灵附加到的显示对象,并为每个精灵使用,根据selectionTool实例检查它们的hitTestObject值。

以下是hitTestObject()的Adobe文档:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#hitTestObject%28%29