html游戏卡住了,HTML 5帆布游戏对象碰撞后卡住

请帮我试了几件事情,但每当我将小物体移动到较大的物体后,在碰撞检测后卡住了。这是我的代码很容易理解。 我也尝试检测另一个对象的各个方面的碰撞检测。HTML 5帆布游戏对象碰撞后卡住

// Setup requestAnimationFrame

requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||

window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

// Create the canvas

var canvas = document.getElementById("canvas");

var ctx = canvas.getContext("2d");

// Game objects

var player = {

width:50,

height:50,

x:50,

y:50,

speed:100,

color:'#3C1BE0'

};

var wall={

width:50,

height:150,

x:300,

y:100,

color:'#E01B5D'

};

// Handle keyboard controls

var keysDown = {};

addEventListener("keydown", function (e) {

keysDown[e.keyCode] = true;

},false);

addEventListener("keyup", function (e) {

delete keysDown[e.keyCode];

},false);

//check collisions

var collisions=function(){

}

// Update game objects

var update = function (modifier) {

//test for collisions

//player collision with wall(red cube)

if(player.x+player.width>wall.x &&

player.x

player.y

player.y+player.height>wall.y

)

{

player.speed=0;

}

//player collission with canvas

if(player.x < 0)

{

player.x=0;

}

else if(player.x+player.width> canvas.width)

{

player.x=canvas.width-player.width;

}

else if(player.y <0)

{

player.y=0;

}

else if(player.y+player.width>=canvas.height)

{

player.y=canvas.height-player.height;

}

if (38 in keysDown) { // Player holding up

player.y -= player.speed*modifier;

}

if (40 in keysDown) { // Player holding down

player.y += player.speed*modifier;

}

if (37 in keysDown) { // Player holding left

player.x -= player.speed*modifier;

}

if (39 in keysDown) { // Player holding right

player.x += player.speed*modifier;

}

};

// Draw everything

var render = function() {

ctx.clearRect(0,0,600,400);

ctx.fillStyle=wall.color;

ctx.fillRect(wall.x,wall.y,wall.width,wall.height);

ctx.fillStyle=player.color;

ctx.fillRect(player.x,player.y,player.width,player.height);

};

// The main game loop

var main = function() {

var now = Date.now();

var delta = now - then;

update(delta/1000);

render();

then = now;

requestAnimationFrame(main);

};

// Let's play this game!

var then = Date.now();

main();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值