如何增加跳跃系统的限制?

时间:2019-03-18 17:02:59

标签: javascript html css

我正在尝试为我的学校项目重新创建“我想成为男人”。 我想对重力系统添加限制,因此角色只能在空中停留几秒钟,并且仅当它到达地面时才会再次跳跃。我找到了许多教程,但我的仍然无法正常工作。 (问题是当您按住W时,字符会升高到框架的顶部,而在释放键时会下降)。

var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
var img = document.getElementById("kid");
var playerW = 30;
var playerH = 40;
var playerX = 5 + 40;
var playerY = canvas.height - 40 - playerH + 5;
var right = false;
var left = false;
var up = false;
var rightup = false;
var leftup = false;
var onGround = true;
var dy = 10;
var userchoice = false;
var velocity = 10;

document.addEventListener("keydown", press);
document.addEventListener("keyup", release);

function press(event) {
  if (event.keyCode == 68) {
    right = true
  }
  if (event.keyCode == 65) {
    left = true
  }
  if (event.keyCode == 87) {
    up = true;
  }
  if (event.keyCode == 68 && event.keyCode == 87) {
    rightup = true;
  }
  if (event.keyCode == 65 && event.keyCode == 87) {
    leftup = true;
  }

}

function release(event) {
  if (event.keyCode == 68) {
    right = false
  }
  if (event.keyCode == 65) {
    left = false
  }
  if (event.keyCode == 87) {
    up = false
  }
  if (event.keyCode == 68 && event.keyCode == 87) {
    rightup = false
  }
  if (event.keyCode == 65 && event.keyCode == 87) {
    leftup = false
  }
}

function Gravity() {
  if (playerY <= canvas.height - 80) {
    playerY += dy;
    playerY += velocity;
    velocity *= 0.9
  }

}


function background() {
  var backgroundX = 40;
  var backgroundY = 40;
  for (var a = 0; a < canvas.width / 40; a++) {
    c.moveTo(backgroundX, backgroundY);
    c.lineTo(backgroundX, 0); //verticle
    c.moveTo(backgroundX, backgroundY);
    backgroundX += 40;
    c.lineTo(backgroundX, backgroundY);
    c.stroke();
  }
  backgroundX = 40;
  for (var a = 0; a < canvas.height / 40; a++) {
    c.moveTo(backgroundX, backgroundY);
    c.lineTo(0, backgroundY);
    c.moveTo(backgroundX, backgroundY);
    backgroundY += 40;
    c.lineTo(backgroundX, backgroundY);
    c.stroke();
  }
  backgroundY = canvas.height - 40;
  for (var a = 0; a < canvas.width / 40; a++) {
    c.moveTo(backgroundX, backgroundY);
    c.lineTo(backgroundX, canvas.height);
    c.moveTo(backgroundX, backgroundY);
    backgroundX += 40;
    c.lineTo(backgroundX, backgroundY);
    c.stroke();
  }
  backgroundX = canvas.width - 40;
  for (var a = 0; a < canvas.height / 40; a++) {
    c.moveTo(backgroundX, backgroundY);
    c.lineTo(canvas.width, backgroundY);
    c.moveTo(backgroundX, backgroundY);
    backgroundY -= 40;
    c.lineTo(backgroundX, backgroundY);
    c.stroke();
  }
}


function Player() {
  c.beginPath();
  c.drawImage(img, playerX, playerY, playerW, playerH);
  c.closePath();
}

function Sprites() {
  c.beginPath();
  c.moveTo();

  c.closePath();
}

function draw() {
  c.clearRect(0, 0, canvas.width, canvas.height);
  Player();
  background();
  if (right && playerX < canvas.width - 40 - playerW) {
    playerX += 10
  }
  if (left && playerX > 40) {
    playerX -= 5
  }
  if (up && onGround === false) {
    onGround === true
    if (up && playerY < canvas.height - 40) {
      playerY -= 20
    }
    if (rightup && playerX < canvas.width - 40 - playerW && playerY < canvas.height - 40 - playerH) {
      playerX += 5;
      playerY -= 20
    }
    if (leftup && playerX > 40 && playerY < canvas.height - 40 - playerH) {
      playerX += 5;
      playerY -= 20
    }
  }
  playerY += dy;
  playerY += velocity;
  velocity *= 0.9;
  if (playerY >= canvas.height - 80) {
    onGround = false;
    playerY = canvas.height - 80;
    velocity = 0;

  }
}

var interval = setInterval(draw, 30);
body {
  background: #2b2b2b;
  height: 100vh;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-family: Helvetica neue, roboto;
}

h1 {
  color: #bdbdbd;
  font-weight: 300;
}
<canvas id="canvas" width="800" height="600" style="border:1px solid black;"></canvas>
<div style="display:none;">
  <img id="kid" src="https://vignette.wikia.nocookie.net/smash-bros-lelle/images/7/79/The_kid.png/revision/latest?cb=20180424233943">
</div>

1 个答案:

答案 0 :(得分:1)

此代码看起来不正确

  if(up && onGround===false){
    onGround===true
    if(up && playerY<canvas.height-40){playerY-=20}
    if(rightup && playerX<canvas.width-40-playerW && playerY<canvas.height-40-playerH){playerX+=5; playerY-=20}
    if(leftup && playerX>40 && playerY<canvas.height-40-playerH){playerX+=5; playerY-=20}
  }

对于初学者来说,onGround === true是一个比较,而不是赋值,因此具有无操作的作用。

您在其他地方

  if(event.keyCode== 68 && event.keyCode==87){rightup=true; }
  if(event.keyCode== 65 && event.keyCode==87){leftup=true;}

event.keyCode可以是68、65或87。但是永远不能是68和87。它是一个值:密钥的keyCode值

尝试这些更正,看看它是否使您更接近所追求的目标