1

我正在创建一个 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 碰撞测试,甚至尝试使用 Collision Detection Kit 均无济于事。提前致谢。

4

1 回答 1

0

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

以下是 hitTestObject() 的 Adob​​e 文档:

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

于 2011-07-14T14:33:43.913 回答