长时间浏览互联网后,我终于想出了如何使角色跳起来的方法。但是我仍然在跳跃方面遇到问题。当我按下跳转按钮时,角色/球基本上会传送到所需位置,然后由于我设置的重力而缓慢下落。我一无所知。我是一名学生,也是编程语言的学习者,所以请多多包涵。
如果你们对我来说,可以使跳跃的上升部分更加平滑,将不胜感激。这是我的代码。
图片简介:
function startGame() {
"use strict";
myGameArea.start();
myGamePiece = new component(30, 30, "GamePiece.png", 10, 389, "image");
/* Javascript that controls the movment of the character and the
background/when the game loads*/
定义游戏区和键码:
var myGameArea = {
canvas : document.getElementById("myCanvas"),
start : function() {
"use strict";
this.canvas.width = 1300;
this.canvas.height = 600;
this.canvas.style.position = "absolute";
this.canvas.style.top = "267px";
this.canvas.style.left = "303px";
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");
/* Loading th Canvas and style it to stay at the exact same position*/
});
window.addEventListener('keyup', function (e) {
myGameArea.keys[e.keyCode] = (e.type === "keydown");
});
},
每次定义位置:
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
};
}
按键:
function updateGameArea() {
if (myGamePiece.jumping <= 0 && myGameArea.keys && myGameArea.keys[38])
{myGamePiece.speedY = -40; myGamePiece.jumping = 1;}
if (myGamePiece.y >= 389) {myGamePiece.jumping = 0; }
myBackground.newPos();
myBackground.update();
myGamePiece.newPos();
myGamePiece.update();
}
如果有人可以帮助我单独缩进所有这些代码行,那就太难了。