html - 我遇到了碰撞检测问题
问题描述
我第一次使用 html,虽然 mygamepiece 停在地上,但它穿过画布的侧面和顶部。有谁知道如何解决这一问题?这是我正在使用的代码。
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
border:10px solid skyblue;
background-color: powderblue;
}
</style>
</head>
<body onload="startGame()">
<script>
var myGamePiece;
function startGame() {
myGamePiece = new component(30, 30, "purple", 80, 75);
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 1550;
this.canvas.height = 745;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
window.addEventListener('keydown', function (e) {
myGameArea.keys = (myGameArea.keys || []);
myGameArea.keys[e.keyCode] = (e.type == "keydown");
})
window.addEventListener('keyup', function (e) {
myGameArea.keys[e.keyCode] = (e.type == "keydown");
})
},
clear : function(){
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.speedX = 0;
this.speedY = 0;
this.gravity = 0.1;
this.gravitySpeed = 0;
this.bounce = 0.5;
this.update = function() {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = -(this.gravitySpeed * this.bounce);
}
}
}
function updateGameArea() {
myGameArea.clear();
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -2; }
if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 2; }
if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -3; }
if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 4; }
myGamePiece.newPos();
myGamePiece.update();
}
</script>
</body>
</html>
希望这将使您对我正在使用的内容有一个很好的了解,希望任何人都能够提供一些建议。
解决方案
与阻止框退出屏幕底部的方式相同,您可以相应地更改x
和speedX
值以防止离开屏幕的左侧和右侧。
检查左右边界
// hit left
if (this.x < 0) {
this.x = 0;
this.speedX = 0;
}
// hit right
var right = myGameArea.canvas.width - this.width;
if (this.x > right) {
this.x = right;
this.speedX = 0;
}
如果x
小于0
,我们将其重置为0
并将 重置speedX
为0
。右手边的逻辑相同。如果x
大于画布宽度减去字符宽度,则设置x
为该值并将 设置speedX
为0
。
这是一个工作片段:
var myGamePiece;
function startGame() {
myGamePiece = new component(30, 30, "purple", 80, 75);
myGameArea.start();
}
var myGameArea = {
canvas: document.createElement("canvas"),
start: function() {
this.canvas.width = 300;
this.canvas.height = 200;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
window.addEventListener('keydown', function(e) {
myGameArea.keys = (myGameArea.keys || []);
myGameArea.keys[e.keyCode] = (e.type == "keydown");
})
window.addEventListener('keyup', function(e) {
myGameArea.keys[e.keyCode] = (e.type == "keydown");
})
},
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.speedX = 0;
this.speedY = 0;
this.gravity = 0.1;
this.gravitySpeed = 0;
this.bounce = 0.5;
this.update = function() {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
// hit left
if (this.x < 0) {
this.x = 0;
this.speedX = 0;
}
// hit right
var right = myGameArea.canvas.width - this.width;
if (this.x > right) {
this.x = right;
this.speedX = 0;
}
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = -(this.gravitySpeed * this.bounce);
}
}
}
function updateGameArea() {
myGameArea.clear();
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
if (myGameArea.keys && myGameArea.keys[37]) {
myGamePiece.speedX = -2;
}
if (myGameArea.keys && myGameArea.keys[39]) {
myGamePiece.speedX = 2;
}
if (myGameArea.keys && myGameArea.keys[38]) {
myGamePiece.speedY = -3;
}
if (myGameArea.keys && myGameArea.keys[40]) {
myGamePiece.speedY = 4;
}
myGamePiece.newPos();
myGamePiece.update();
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
canvas {
border: 10px solid skyblue;
background-color: powderblue;
}
</style>
</head>
<body onload="startGame()">
</body>
</html>
对于顶墙,您需要检查是否y
小于0
,然后相应地更改y
和speedY
。
推荐阅读
- javascript - 带预览的 HTML5 视频
- vue.js - How to unit test standalone Vue composition
- matlab - 如何在 MATLAB 上生成 Nakagami–m 衰落通道?
- javascript - 如何获取 httpOnly(安全)cookie TestCafe?
- permutation - 生成所有无序组合
- go - JWT 不验证收到的令牌
- regex - IF - ELSE 语句 Google Sheets 各种值
- java - 在百里香中动态填充列表
- swift - 在 SceneKit 中捕获没有背景的节点的图像
- ms-access - 缺少打印对象