2013-04-07 90 views
1

我有一个透明的图像和一个正方形。我想检测方形与图像碰撞的时间。但是,由于图像是透明的,它仍然会检测到它与透明像素发生碰撞。所以,在我尝试使用BitmapData之后,我还没有使用过。所以,它不工作。说实话,我没有想到波纹管代码的工作。我只是写信给你一个想法,想要做什么以及我想做什么。只检测与透明图像的可见部分碰撞(AS3)

这里是我的代码:

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"); 
     } 
    } 
} 

任何人都知道我可以做它只能检测时方与图像的可见部分碰撞? 提前致谢。

回答