【问题标题】:Only detecting collision with the visible part of a transparent image (AS3)仅检测与透明图像可见部分的碰撞 (AS3)
【发布时间】:2013-04-07 22:03:59
【问题描述】:

我有一个透明图像和一个正方形。我想检测正方形何时与图像发生碰撞。然而,由于图像是透明的,它仍然会检测到它与透明像素的碰撞。所以,经过一番阅读,我尝试使用以前没有使用过的 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");
        }
    }
}

任何人都知道我怎样才能让它仅在正方形与图像的可见部分碰撞时检测到? 提前非常感谢。

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    除非您出于个人原因想要自己实现此功能,否则我建议您使用 Corey O'Neil 的碰撞检测套件:

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

    【讨论】:

    • 谢谢,拉胡尔。该套件将满足我的需求。 :)
    猜你喜欢
    • 2014-11-20
    • 2014-04-16
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多