微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

防止玩家“飞”

如何解决防止玩家“飞”

我目前正在尝试制作一款无尽的跑步游戏,我刚刚完成了跳跃机制的制作。但是,如果玩家要按住向上箭头键,或者在他们接触地面之前按下它,他们就能够模仿“飞行”的能力。如果它们还没有接触地面,我不确定如何防止它们“飞行”。如果有人有任何想法,请告诉我。我的代码如下:

let ctx = document.querySelector("canvas").getContext("2d");

// Screen
ctx.canvas.height = 512;
ctx.canvas.width = 512;

// Images
let bg = new Image;
bg.src = "./Background.png";

let fl = new Image;
fl.src = "./Floor.png";

// Player
let y = 256;
let speed = 2.5;

let pl = new Image;
pl.src = "./Idle.png";
pl.onload = function() {
  ctx.drawImage(pl,y);
};

// Jumping
let UP = false;

// Ducking
let DOWN = false;

document.onkeydown = function(e) {
  if (e.keyCode == 38) UP = true;
  if (e.keyCode == 40) DOWN = true;
};

document.onkeyup = function(e) {
  if (e.keyCode == 38) UP = false;
  if (e.keyCode == 40) DOWN = false;
};

// Frames
function update() {

  // Clear
  ctx.clearRect(0,512,512);

  // Background
  ctx.drawImage(bg,0);

  // Floor
  ctx.drawImage(fl,384);
  ctx.drawImage(fl,128,256,384,384);

  // UP
  if (UP) {
    if (y > 100) {
      ctx.drawImage(pl,y -= speed);
    } else {
      UP = false;
    };
  } else if (!UP) {
    if (y < 256) {
      ctx.drawImage(pl,y += speed);
    } else {
      ctx.drawImage(pl,y);
    };
  };

  // DOWN
  if (DOWN) {
    pl.src = "./Duck.png";
  } else if (!DOWN) {
    pl.src = "./Idle.png";
  };
};

setInterval(update,10);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。