BitmapData碰撞

时间:2012-04-25 05:48:22

标签: actionscript-3 flash

我一直在尝试使用BitmapData进行碰撞检测,但到目前为止都失败了,我不知道为什么。代码编译,但没有做任何事情(当它应该打印“命中”)。任何人都可以帮助我吗?

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

1 个答案:

答案 0 :(得分: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 );