這是我第一次嘗試JavaScript遊戲。 我正在試圖做一個黃色塊的JavaScript遊戲,你可以使用w
a
s
d
鍵來控制。藍色方塊應該是一個敵人,如果你擊中了它,就會讓你鬆動。稍後我會添加更多功能。問題:遊戲工作正常,直到你釋放其中一個按鈕。如何在釋放控制器按鈕時防止播放器飛走?Javascript遊戲控件
我也不能讓按鈕工作時,我讓w
a
s
d
按鍵控制器,但我不認爲,因爲在這一點上的一個重大問題。即便如此,如果有人知道如何解決這個問題,我會很開心。
var myGamePiece;
var myFi;
function startGame() {
myGameArea.start();
myGamePiece = new component(30, 30, "rgba(200,200,0,0.5)", 10, 175);
//blueGamePiece = new component(20, 20, "blue", 300, 110);
myFi = new component(15, 15, "blue", 300, 120);
}
var myGameArea = {
canvas: document.createElement("canvas"),
start: function() {
this.canvas.width = 680;
this.canvas.height = 420;
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");
})
}, // I don't get why the comma is so important and what it does!
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
stop: function() {
clearInterval(this.interval);
}
}
function component(width, height, color, x, y) {
this.gamearea = myGameArea;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function() {
this.x += this.speedX;
this.y += this.speedY;
},
this.crashWith = function(enemy) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var fileft = enemy.x;
var firight = enemy.x + (enemy.width);
var fitop = enemy.y;
var fibottom = enemy.y + (enemy.height);
var crash = true;
if ((mybottom < fitop) || (mytop > fibottom) || (myright < fileft) || (myleft > firight)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
if (myGamePiece.crashWith(myFi)) {
myGameArea.stop();
} else {
myGameArea.clear();
myGamePiece.speedX += myGamePiece.speedX;
myGamePiece.speedY += myGamePiece.speedY;
if (myGameArea.keys && myGameArea.keys[65]) {
myGamePiece.speedX = -1;
}
if (myGameArea.keys && myGameArea.keys[68]) {
myGamePiece.speedX = 1;
}
if (myGameArea.keys && myGameArea.keys[87]) {
myGamePiece.speedY = -1;
}
if (myGameArea.keys && myGameArea.keys[83]) {
myGamePiece.speedY = 1;
}
myGamePiece.newPos();
//blueGamePiece.x -=1;
//blueGamePiece.y +=2;
//blueGamePiece.update();
myGamePiece.update();
myFi.update();
}
}
function moveup() {
myGamePiece.speedY = 1;
}
function movedown() {
myGamePiece.speedY = 1;
}
function moveleft() {
myGamePiece.speedX = 1;
}
function moveright() {
myGamePiece.speedX = 1;
}
function clearmove() {
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
}
body {
text-align: center;
}
canvas {
border: 1px solid #d3d3d3;
background-color: #f4f4f4;
}
div {
text-align: center;
width: 300px;
height: 100px;
}
button {
background-color: black;
color: yellow;
}
<div>
<button onmousedown="moveup()" onmouseup="clearmove()">UP</button>
<br>
<br>
<button onmousedown="moveleft()" onmouseup="clearmove()">LEFT</button>
<button onmousedown="moveright()" onmouseup="clearmove()">RIGHT</button>
<br>
<br>
<button onmousedown="movedown()" onmouseup="clearmove()">DOWN</button>
</div>
<p>I have created a game area!</p>
<p>Then I created a component to my game! I is a yellow square!</p>
<p>I continued to add frames and movement</p>
<p>After I had added movement, I could finally add controllers</p>
<p>First I added the buttons that was controlled by the mouse,
<br>then I added the keystroke controllers for the w,s,a,d keys.</p>