1

我有一个透明图像和一个正方形。我想检测正方形何时与图像发生碰撞。然而,由于图像是透明的,它仍然会检测到它与透明像素的碰撞。所以,经过一番阅读,我尝试使用以前没有使用过的 BitmapData。所以,它不起作用。老实说,我没想到下面的代码起作用。我只是写信让你知道我想做什么以及我想怎么做。

这是我的代码:

package 
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;

/**
 * Testing Transparency
 * @author Craig Jackson
 */

public class Main extends Sprite 
{
    public var square:Sprite;

    [Embed(source="../lib/TestTransparency.png")]
    public var TestTrans:Class;

    public var testTransBitmapData:BitmapData = new BitmapData(300, 30, true, 0);

    public var testTransBitmap:Bitmap = new TestTrans();

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        startUp();
    }

    public function  startUp():void 
    {
        square = new Sprite();
        square.graphics.beginFill(0x666666);
        square.graphics.drawRect(0, 0, 50, 50);
        square.graphics.endFill();
        addChild(square);

        testTransBitmapData.draw(testTransBitmap);
        addChild(testTransBitmap);

        addEventListener(Event.ENTER_FRAME, enterFrame);
    }

    public function enterFrame(e:Event):void
    {
        square.x = mouseX;
        square.y = mouseY;

        if (square.hitTestObject(testTransBitmap))
        {
            trace("Touching");
        }
    }
}

任何人都知道我如何才能使其仅在正方形与图像的可见部分碰撞时检测到?提前致谢。

4

1 回答 1

1

Unless you have a personal reason for wanting to implement this yourself, I'd recommend using Corey O'Neil's collision detection kit:

https://code.google.com/p/collisiondetectionkit/

于 2013-04-07T22:33:28.180 回答