0

目前我正在开发纯动作脚本项目中的拖放应用程序。现在我遇到的问题是,我有一些 PNG 位图,周围有一些透明背景。位图可能会相互碰撞。用户可以拖动这些位图中的任何一个。如果用户点击透明区域,用户应该不能拖动,反之亦然。这意味着用户应该能够拖动最上面的位图而忽略透明位图..

任何人都可以帮助解决这个问题。

谢谢并恭祝安康。

4

1 回答 1

2

有可能的。您需要阅读有关 AS 可能性的更多文档。

这只是一个快速而肮脏的概念证明:

package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;

/**
 * ...
 * @author Jevgenij Dmitrijev ( http://www.ifmi.lt )
 *
 */
public class DragTransparentBitmaps extends Sprite
{
    private var _dragMC:Sprite;

    public function DragTransparentBitmaps()
    {
        var sp:Sprite;
        var bmp:Bitmap;

        for (var i:uint; i < 10; ++i)
        {
            sp = new Sprite();
            sp.name = 'item_' + i.toString();
            bmp = new Bitmap(new Close()); // add your bitmapData
            sp.addChild(bmp);
            sp.x = 20*i;
            sp.y = 20*i;
            addChild(sp);
        }

        stage.addEventListener (MouseEvent.MOUSE_DOWN, handleMouseDown )
    }

    function handleMouseDown(e:MouseEvent):void
    {
        /*

        // OPTIONAL:

        // if you need the mouse to be in a specific region:            
        if ( mouseX > 400 || mouseY > 400 )
        {
            return;
        }

        // or you can just check if mouse is on your needed mc:
        if ( !someSprite.hitTestPoint(mouseX, mouseY) )
            return;
        */


        // cheking how much objects are under the mouse
        var objectsUnderMouse:Array = getObjectsUnderPoint (new Point (mouseX, mouseY));

        var length_i:uint = objectsUnderMouse.length;

        if ( length_i > 0 )
        {
            // if only one then no checking needed just parsing it to the drag function.
            if ( length_i == 1 )
            {
                drag(objectsUnderMouse[0].parent);
                return;
            }
        }
        else
            return; // if nothing under the mouse just end the function

        var sp:Sprite;
        var bmpData:BitmapData; 

        for (var i:uint; i < length_i; i++) 
        {
            // taking a sprite where bitmap is positioned
            sp = objectsUnderMouse[i].parent as Sprite;

            // taking the bitmap data
            bmpData = objectsUnderMouse[i].bitmapData;

            // taking the pixel information ( see the docu about it )
            // if the value is > 0 it means that the bitmap is not transparent and we can move it
            if ( bmpData.getPixel32(sp.mouseX, sp.mouseY ) > 0 )
            {
                drag (objectsUnderMouse[i].parent);
                return;
            }
        }
    }

    // just drag functions
    private function drag(value:DisplayObject):void 
    {
        _dragMC = value as Sprite;
        _dragMC.startDrag();

        stage.addEventListener (MouseEvent.MOUSE_UP, handleMouseUp);
    }

    private function handleMouseUp(e:MouseEvent):void 
    {
        stage.removeEventListener (MouseEvent.MOUSE_UP, handleMouseUp);
        _dragMC.stopDrag();
    }
}
}

让我知道它是否有帮助。

于 2012-05-07T13:35:37.080 回答