0

我一直在尝试使用 BitmapData 进行碰撞检测,但到目前为止都失败了,我不知道为什么。代码编译,但不做任何事情(当它应该打印 "hit" 时)。谁能帮我?

package 
{
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.display.BitmapData;
import flash.geom.Point;

public class Main extends MovieClip
{
    private var spd:int = 5;
    private var pressedKeys:Object = {};
    private var bgMask:BitmapData;
    private var plMask:BitmapData;
    private var wall:Wall = new Wall();

    public function Main()
    {
        var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
        bgMask = bgBitMapData.clone();
        wall.x = bg.x;
        wall.y = bg.y;
        bgMask.draw( wall );

        var plBitMapData:BitmapData = new BitmapData(pl.width,bg.height,true,0);
        plMask = bgBitMapData.clone();
        bgMask.draw( pl );

        stage.addEventListener(KeyboardEvent.KEY_DOWN,this.keyPressHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP,this.keyPressHandler);
        stage.addEventListener(Event.ENTER_FRAME,this.gameLoop);
    }

    public function keyPressHandler( evt:KeyboardEvent ):void
    {
        if (evt.type == KeyboardEvent.KEY_DOWN)
        {
            pressedKeys[evt.keyCode] = 1;
        }
        else
        {
            delete pressedKeys[ evt.keyCode ];
        }
    }

    public function inputHandler():void
    {
        var moveXBy:int;
        var moveYBy:int;

        if (pressedKeys[Keyboard.J])
        {
            moveXBy -=  spd;
        }

        if (pressedKeys[Keyboard.K])
        {
            moveYBy +=  spd;
        }

        if (pressedKeys[Keyboard.L])
        {
            moveXBy +=  spd;
        }

        if (pressedKeys[Keyboard.I])
        {
            moveYBy -=  spd;
        }

        pl.x +=  moveXBy;
        pl.y +=  moveYBy;

    }

    public function playerWallCollision():void
    {
        if ( plMask.hitTest( new Point( wall.x, wall.y ), 1, bgMask, new Point( pl.x, pl.y ), 1))
        {
            trace(  "hit" );
        }
    }

    public function gameLoop( evt:Event ):void
    {
        wallUpdate();
        inputHandler();
        playerWallCollision();
    }

    private function wallUpdate()
    {
        wall.x = bg.x;
        wall.y = bg.y;
    }
}
}
4

1 回答 1

1

据此脚本告诉我,在以下几行之后:

var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
    // bgBitMapData is an empty BMP at this point cause its just freshly instanciated
    bgMask = bgBitMapData.clone();
    wall.x = bg.x;
    wall.y = bg.y;
    bgMask.draw( wall );

    var plBitMapData:BitmapData = new BitmapData(pl.width,bg.height,true,0);
    // no idea y you make this plBitMapData anyway
    plMask = bgBitMapData.clone();
    // now you clone the empty bgBitMapData to be plMask
    bgMask.draw( pl );

plMask 永远不会充满任何东西!所以与空的 BitmapData 相比应该总是返回 false。

所以什么时候应该输出命中的答案 - 从不;)

作为旁注,请尝试通过以下方式使其更具可读性: 1. 在这里和那里写评论您的意图是什么,以及 2. 采用直接的方式:

    var bgBitMapData:BitmapData = new BitmapData(bg.width,bg.height,true,0);
    bgMask = bgBitMapData.clone();
    // this position seems just wrong to me wall.x or wall.y do not effect the draw so keep it somewhere else... since you will copy the inside of wall
    wall.x = bg.x;
    wall.y = bg.y;
    bgMask.draw( wall );

对我来说,它应该看起来像:

    // placing a wall to new / init coordinates 
    wall.x = bg.x;
    wall.y = bg.y;

    // drawing wall on bgMask for hittesting against player or what ever you intende here
    bgMask = new BitmapData(bg.width,bg.height,true,0);
    bgMask.draw( wall );
于 2012-04-25T08:03:20.470 回答