我有2个盒子,1个是播放器,另一个是墙壁。如果玩家继续前进的方向有一堵墙,我希望玩家停止移动。
在这个问题的底部提供了一个plunker链接来显示2个盒子的功能。检测碰撞JavaScript
使用移动盒子时,我需要知道玩家如何检测墙壁并在接触墙壁时停止移动?这意味着玩家和墙不能位于同一个位置,玩家必须绕墙而不是穿过墙。
今天我需要这个,我会整晚整夜,所以请不要犹豫,评论或回答问题,谢谢。
function Player(row, col) {
this.isUpKey = false;
this.isRightKey = false;
this.isDownKey = false;
this.isLeftKey = false;
this.row = row;
this.col = col;
this.color = "#f00";
}
function drawWalls() {
var walls = new Array();
function Wall (row,col) {
this.row = row;
this.col = col;
this.color = "#000";
}
walls[walls.length] = new Wall(5,5);
for (var b = 0; b < walls.length; b++) {
ctxWall.fillStyle = walls[b].color;
ctxWall.fillRect(walls[b].row*25, walls[b].col*25, 25, 25);
}
}
var players = [];
var ctxPlayer;
var ctxWall;
var currentPlayer;
window.onload = function() {
ctxPlayer = document.getElementById('c').getContext('2d');
ctxWall = document.getElementById('walls').getContext('2d');
currentPlayer = players[players.length] = new Player(2, 2);
setInterval(render, 25);
drawWalls();
}
window.onkeypress = doKeyDown;
function render() {
ClearPlayer();
drawPlayer();
}
function drawPlayer() {
for (var p = 0; p < players.length; p++) {
ctxPlayer.fillStyle = players[p].color;
ctxPlayer.fillRect(players[p].row * 25, players[p].col * 25, 25, 25);
}
}
function doKeyDown(e) {
console.log(e);
if (e.keyCode == 97) {
currentPlayer.isUpKey = true;
--currentPlayer.row;
}
if (e.keyCode == 100) {
currentPlayer.isDownKey = true;
++currentPlayer.row;
}
if (e.keyCode == 119) {
currentPlayer.isLeftKey = true;
--currentPlayer.col;
}
if (e.keyCode == 115) {
currentPlayer.isRightKey = true;
++currentPlayer.col;
}
}
function ClearPlayer() {
ctxPlayer.clearRect(0, 0, 600, 400);
}
http://plnkr.co/edit/27URhP?p=preview
我已尝试添加该功能checkIfPlayerMovingIntoWall(),以玩家和墙对象,看到波纹管,却又什么都没有发生。
function Wall (row,col) {
this.row = row;
this.col = col;
this.color = "#000";
this.width= 25
this.height= 25
this.leftX = this.row;
this.rightX = this.row + this.width;
this.topY = this.col;
this.bottomY = this.col + this.height;
}
function Player(row, col) {
this.isUpKey = false;
this.isRightKey = false;
this.isDownKey = false;
this.isLeftKey = false;
this.row = row;
this.col = col;
this.color = "#f00";
this.width= 25
this.height= 25
this.leftX = this.row;
this.rightX = this.row + this.width;
this.topY = this.col;
this.bottomY = this.col + this.height;
}
function checkIfPlayerMovingIntoWall() {
if (currentPlayer.topY < wall.bottomY &&
currentPlayer.bottomY > wall.topY &&
currentPlayer.leftX < wall.rightX &&
currentPlayer.rightX > wall.leftX) {
alert("collision detected");
}
}
,请不要犹豫,回答或基于你有什么到目前为止
任何帮助,请评论,如果您有任何建议 – Dalomo 2015-04-01 02:39:06
我不知道你有没有看过这些已经:HTML5画布游戏:2D碰撞检测(http://blog.sklambert.com/HTML5帆布游戏-2D碰撞检测/)。还有:[2D碰撞检测](https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection)。或者:[如何在HTML5游戏中进行碰撞检测](http://www.gaminglogy.com/tutorial/collision-detection/) – 2015-04-01 03:16:29
@RyanVincent yah我已经尝试了其中2个,问题是我的对象是玩家是在一个数组内部创建的,所以墙对象也是这样,所以对于这两个对象如何检测对方变得非常困惑,你会如何建议对象会检测对方,以及如果你有任何想法,您请向我展示如何编写代码的提示 – Dalomo 2015-04-01 03:22:48