我一直在尝试使用 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;
}
}
}