我有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