矩形与形状碰撞

const KEY_A:uint=65;
const KEY_D:uint=68;
const KEY_S:uint=83;
const KEY_W:uint=87;
var key:Object=new Object
var speed:Number=2
stage.frameRate=30
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown)
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp)
this.addEventListener(Event.ENTER_FRAME,test)
function isDown(keyCode:uint):Boolean
{
return key[keyCode]?true:false
}
//按下事件侦听器
function keyDown(e:KeyboardEvent):void
{
key[e.keyCode]=true
}
//释放事件侦听器
function keyUp(e:KeyboardEvent):void
{
delete key[e.keyCode]
}
function test(e:Event):void
{
if(key[KEY_A])
{
mc.x-=speed
}
if(key[KEY_D])
{
mc.x+=speed
}
if(key[KEY_W])
{
mc.y-=speed
}
if(key[KEY_S])
{
mc.y+=speed
}
checkhit()
}
function checkhit():void
{
var rect:Rectangle=mc.getBounds(this)
var leftAndTop:Boolean=map.hitTestPoint(rect.x,rect.y,true)
var leftAndBottom:Boolean=map.hitTestPoint(rect.x,rect.bottom,true)
var rightAndTop:Boolean=map.hitTestPoint(rect.right,rect.y,true)
var rightAndBottom:Boolean=map.hitTestPoint(rect.right,rect.bottom,true)
if(leftAndTop||leftAndBottom)
{
mc.x+=speed
}
if(rightAndTop||rightAndBottom)
{
mc.x-=speed
}
if(leftAndTop||rightAndTop)
{
mc.y+=speed
}
if(leftAndBottom||rightAndBottom)
{
mc.y-=speed
}
}
浙公网安备 33010602011771号