Javascript 画布和#x27之间的冲突;s

Javascript 画布和#x27之间的冲突;s,javascript,html,canvas,html5-canvas,collision-detection,Javascript,Html,Canvas,Html5 Canvas,Collision Detection,是的,我知道这上面有帖子,我已经读过了,试过了所有的方法。。。我正在制作一个2人游戏,其中一人使用箭头键绘制轨迹,另一人使用W和S键避开轨迹的边缘。但无论我怎么做,当玩家与赛道边缘碰撞时,我都无法获得输出。提前谢谢你,贾斯汀 alert('Player One Ready?按i键获取指令!') var c=document.getElementById(“myCanvas”); var ctx=c.getContext(“2d”); setInterval(函数(){ window.TempX

是的,我知道这上面有帖子,我已经读过了,试过了所有的方法。。。我正在制作一个2人游戏,其中一人使用箭头键绘制轨迹,另一人使用W和S键避开轨迹的边缘。但无论我怎么做,当玩家与赛道边缘碰撞时,我都无法获得输出。提前谢谢你,贾斯汀

alert('Player One Ready?按i键获取指令!')
var c=document.getElementById(“myCanvas”);
var ctx=c.getContext(“2d”);
setInterval(函数(){
window.TempX=window.TempX-1
window.TempBottomY=window.PlayerTwoYBottomLength
window.testopy=window.PlayerTwoYTopLength
}, 15);
//P2底部和顶部变量
window.PlayerTwoX=1277
window.PlayerTwoYTopLength=310
window.PlayerTwoYBottomLength=310
window.TempX=1277
window.etropy=310
window.TempBottomY=310
window.fourten=410
window.player=1240
window.playerney=360
//声明的画布
ctx.fillStyle=“#c2c4c2”//背景矩形
ctx.fillRect(20,20,1280,720);
setInterval(函数(){
ctx.fillStyle=“#000000”//播放器2底部
ctx.fillRect(1277410,3,window.PlayerTwoYBottomLength);
ctx.fillStyle=“#000000”//播放器2顶部
ctx.fillRect(1277、20、3、window.PlayerTwoYTopLength);
ctx.fillStyle=“白色”//播放器2底部
ctx.fillRect(1277410,3,window.PlayerTwoYBottomLength);
ctx.fillStyle=“白色”//播放器2顶部
ctx.fillRect(1277、20、3、window.PlayerTwoYTopLength);
ctx.fillStyle=“黑色”//播放器2底部
ctx.fillRect(window.TempX,window.fourten,3,window.TempBottomY);
ctx.fillStyle=“Black”//播放器2顶部
ctx.fillRect(window.TempX,20,3,window.testopy);
}, 15);
setTimeout(函数测试(){
setInterval(函数Joe(){
window.playerNex=window.playerNex-0.8
}, 15);
警报(“玩家2准备好了吗”)
}, (20000));
setTimeout(函数Testz(){
setInterval(函数Jeff(){
ctx.fillStyle=“#42F4”//播放器1
ctx.fillRect(window.playernex,window.playerney,20,20);
}, 3);
}, (20000));
//Y轴播放器2
函数被按下(){
window.PlayerTwoYBottomLength=window.PlayerTwoYBottomLength-2
window.PlayerTwoYTopLength=window.PlayerTwoYTopLength+2
window.fourten=window.fourten+2
}
函数UpPressed(){
window.PlayerTwoYBottomLength=window.PlayerTwoYBottomLength+2
window.PlayerTwoYTopLength=window.PlayerTwoYTopLength-2
window.fourten=window.fourten-2
}
函数WPressed(){
window.playerney=window.playerney-3
}
函数SPressed(){
window.playerney=window.playerney+3
}
函数ippressed(){
window.open(“instructions.html”);
location.reload();
}
//检查钥匙
document.onkeydown=checkKey;
功能检查键(e){
e=e | | window.event;
如果(e.keyCode=='38'){
//向上箭头
console.log('Up')
加强的()
}否则如果(e.keyCode=='40'){
//向下箭头
console.log('Down')
下压()
}否则如果(e.keyCode=='37'){
//左箭头
console.log('Left'))
}否则如果(e.keyCode=='39'){
//右箭头
console.log('Right')
}
如果(e.keyCode=='87'){
//W键
console.log('W')
WPressed()
}
如果(e.keyCode=='83'){
//
console.log('S')
SPressed()
}
如果(e.keyCode=='73'){
//我
console.log('i')
iPressed()
}
}

我在代码中没有看到任何冲突检测,因此我假设您正在询问如何创建这样的代码。在不修改当前代码的情况下,检测碰撞的一种方法是查看玩家2将移动到的x,y点上的颜色,如果颜色与玩家1绘制的颜色不同,那么这就是碰撞。我该怎么做?有没有办法检查黑色像素是否变为蓝色?我自己从来没有这样做过,但有一个Stackoverflow答案可以帮助您: