亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js鍵盤(pán)事件實(shí)現(xiàn)人物的行走

 更新時(shí)間:2020年01月17日 15:15:21   作者:SSSkyCong  
這篇文章主要為大家詳細(xì)介紹了js鍵盤(pán)事件實(shí)現(xiàn)人物的行走,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js鍵盤(pán)事件實(shí)現(xiàn)人物行走的具體代碼,供大家參考,具體內(nèi)容如下

描述:

小時(shí)候喜歡玩的一個(gè)游戲,魔塔,實(shí)現(xiàn)了人物的行走,以及跳躍,當(dāng)然是2D的效果。

用到的圖:

效果:

代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    html
    {
      background-color: deepskyblue;
    }
    div
    {
      width: 32px;
      height: 32px;
      background-image: url("img/Actor01-Braver03.png");
      position: absolute;
    }
  </style>
</head>
<body>
  <div></div>
  <script>
    var key=0;
    var bool=false;
    var speed=2;//每次行走的距離
    var actor;//人物div
    const HEIGHT=33;//人物的高
    const WIDTH=32;//人物的寬
    var arr=[1,3,2,0];//4排圖像 代表 下 左 右 上
    var num=0;
    var jumpBool=false;
    var actorSkinSpeed=8;
    var jumpSpeed=-15;
    init();
    function init() {
      window.addEventListener("keydown",keyHandler);
      window.addEventListener("keyup",keyHandler);
      actor=document.querySelector("div");
      setInterval(animation,16);
      //按鍵驅(qū)動(dòng)不能實(shí)現(xiàn) 實(shí)現(xiàn)的是通過(guò)按鍵觸發(fā)相應(yīng)動(dòng)畫(huà) 實(shí)現(xiàn)我們的人物的幀動(dòng)畫(huà) 跳轉(zhuǎn)
    }
    
    function keyHandler(e) {
      bool=e.type==="keydown";
      key=e.keyCode;
      if(!bool){
        num=0;
        actor.style.backgroundPositionX=-num*WIDTH+"px";
      }
      if(key===32 && !jumpBool){//跳躍 空格驅(qū)動(dòng)
        jumpBool=true;
      }
    }
    
    function animation() {
      jump();
      if(!bool)return;
      walk();//單方向行走 實(shí)現(xiàn)
      changeDirection();//方向確定時(shí) 內(nèi)部行走的實(shí)現(xiàn)
    }
    
    function jump() {
      if(!jumpBool)return;
      jumpSpeed+=1;
      if(jumpSpeed===15){
        jumpBool=false;
        jumpSpeed=-15;
        return;
      }
      actor.style.top=actor.offsetTop+jumpSpeed+"px";
    }
    
    function changeDirection() {
      actorSkinSpeed--;
      if(actorSkinSpeed>0) return;
      actorSkinSpeed=8;
      num++;
      if(num>3) num=0;
      actor.style.backgroundPositionX=-num*WIDTH+"px";
    }
 
    function walk() {
      switch (key){
        case 37://左 ×1 第二排
          actor.style.left=actor.offsetLeft-speed+"px";
          actor.style.backgroundPositionY=-arr[0]*HEIGHT+"px";
          break;
        case 38://上 ×3 第四排
          actor.style.top=actor.offsetTop-speed+"px";
          actor.style.backgroundPositionY=-arr[1]*HEIGHT+"px";
          break;
        case 39://右 ×2 第三排
          actor.style.left=actor.offsetLeft+speed+"px";
          actor.style.backgroundPositionY=-arr[2]*HEIGHT+"px";
          break;
        case 40://下 ×0 第一排
          actor.style.top=actor.offsetTop+speed+"px";
          actor.style.backgroundPositionY=-arr[3]*HEIGHT+"px";
          break;
 
      }
    }
  </script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論