我希望有人能够查看我一直在为一个简单的老式马里奥克隆工作的这个 javascript 代码。我从几个教程中拼凑了我对画布的了解,但我无法让与块的碰撞或跳跃正常工作。
跳跃似乎让马里奥陷入了一遍又一遍弹跳的无限循环,这看起来很有趣,但不太适合玩游戏!
function Player() {
this.srcX = 0;
this.srcY = 0;
this.drawX = gameWidth /2;
this.drawY = 0;
this.scaleWidth = 38;
this.scaleHeight = 50;
this.width = 48;
this.height = 60;
this.speed = 10;
this.maxJump = 50;
this.velY = 0;
this.velX = 0;
this.isJumpKey = false;
this.isRightKey = false;
this.isCrouchKey = false;
this.isLeftKey = false;
this.jumping = false;
this.grounded = false;
}
Player.prototype.draw = function(){
clearPlayer();
this.checkKeys();
ctxPlayer.drawImage(
player,
this.srcX,
this.srcY,
this.width,
this.height,
this.drawX,
this.drawY,
this.scaleWidth,
this.scaleHeight);
};
Player.prototype.checkKeys = function () {
if(this.isJumpKey){
if (!this.jumping && this.grounded ) {
this.jumping = true;
this.grounded = false;
this.velY = -this.speed * 2;
}
}
if(this.isRightKey){
if (this.velX < this.speed) {
this.velX++;
}
}
if(this.isLeftKey){
if (this.velX < this.speed) {
this.velX--;
}
}
if(this.isCrouchKey){
player1.grounded = true;
player1.jumping = false;
}
};
这是我现在所在的代码笔:http: //codepen.io/AlexBezuska/pen/ysJcI
我真的很感谢任何帮助,我将在此期间继续搜索和解决这个问题,但是您可以提供的任何指示,甚至是格式化、原型创建等方面的建议都非常感谢(我对画布和原型都很陌生)