javascript - HTML 5 Canvas 按键时间事件

标签 javascript jquery html canvas jquery-events

我正在研究 Stick Hero (著名的 Android 游戏)在 HTML 5 中克隆,我正在尝试在 Javascript 中实现捕获按键时间(右箭头 - ASCII 39) 的代码,并相应地长出一根棍子。

<!doctype html>
<html>
<head>
    <title> Stuck Hero !</title>
    <script src='jquery-2.1.1.min.js'> </script>
</head>
<body>
    <canvas id='stuck' height='300' width='500'> </canvas>
</body>
<script>

        var stick_size=10;
    $(document).ready(function(){
        var canvas=$("#stuck")[0];
        var ctx=canvas.getContext('2d');
        var w=$("#stuck").width();
        var h=$("#stuck").height();
        var score=0;
        var stick=0;
        var timeout=0;

        console.log(w +" is width and "+h+" is the height ");
        paint(w,h,ctx);
        score_update(ctx);
        });


    function score_update(ctx){
        //function to print the score
        ctx.font = "20px Arial";
        ctx.strokeText("Your Score is :",1,300);
        console.log("Console Score Printed Successfully");
    }


    function paint(width,height,ctl) {
        var rand = Math.floor(Math.random() * 101);
        ctx=ctl;
        // function to print different background styles
        if(rand %3 ==0) {
            ctx.fillStyle = "#F0F0F0";
        }
        else if(rand %2 == 0){
            ctx.fillStyle = "#D0D0D0";
        }
        else{
            ctx.fillStyle = "#C0C0C0";
        }
        drawing(ctx,width,height);
        console.log("Console was Painted Successfully");
    }

    function drawing(ctx,width,height) {
        //filling rectangle
        ctx.fillRect(0,0,width,height);

        //drawing our hero
        ctx.beginPath();
        ctx.arc(20, 200, 15 , 0, 2 * Math.PI, false);
        ctx.fillStyle = 'green';
        ctx.fill();
        console.log("Shapes were drawn Successfully");
    }   

    //growing stick
    function grow_stick() {
        for(lv=200;lv>stick_size;lv++) {
            ctx.beginPath();
            ctx.moveTo(20,200);
            console.log("Moved to 20 , 200 in Canvas");
            ctx.lineTo(20,lv);
            console.log("Drawing Line from 20 to "+lv);
            ctx.stroke();           
            console.log("Stroke is Done");
        }
        console.log("Stick was growing Successfully");
    }

    function clearcanvas(ctl,width,height){
        //function to clear the screen data
        ctl.clearRect ( 0 , 0 ,width,height );
    }


    $(document).keydown(function(e){
        var key = e.which;
        if(key == "39"){  //38 is the code for up key
            //event.preventDefault();
            console.log("Down key is pressed");
            timeout= setInterval(grow_stick);
        }

    }).bind('keyup', function(e){
        if(e.which == "39") {
        console.log("Down key pressed is released ");
        clearTimeout(timeout);
        }
    });
</script>
</html>

我有一些问题和澄清

  1. 如何跟踪按键事件?
    我尝试在按下按键时设置超时功能,并在释放按键时释放超时功能(但 cleartimeout 存在一些问题)
  2. 如何在释放按键后不接受任何事件,我必须做其他动画?
    还没有尝试过,不知道
  3. 另一个常见问题
    如何准备我作为 HTML5 游戏开发人员的心态,欢迎任何引用链接或想法,我知道基本的编程

编辑:jsfiddle

最佳答案

  1. 尝试像这样使用间隔:

    var myInt=null
    $(document).keydown(function(e){
         var key = e.which;
         if(key == "39"){  //38 is the code for up key
             //event.preventDefault();
             console.log("Down key is pressed");
             myInt = setInterval(function(){ myAnimation() }, 40); 
             //40 milliseconds because of 25 images per sec
         }
    }).bind('keyup', function(e){
        if(e.which == "39") {
            console.log("Down key pressed is released ");
            clearInterval(myInt);
        }
    
    function myAnimation(){
       //your animation that last 40 milliseconds
    }
    
  2. 您可以在 keyeup 事件中设置某种 bool 值,并在每个事件中使用 if 检查它(if(keyreleased==false){//做正常的事情}else{//skip}) 动画完成后,将 bool 值设置回默认值

    var keyreleased=false;
    .bind('keyup', function(e){
    keyreleased=true;
    }
    
    someEvent(){
         if(keyreleased!=true){
              //do stuff
         }
         //animation done
         keyreleased=false;
    }
    
  3. 我不知道^^

关于javascript - HTML 5 Canvas 按键时间事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418145/

相关文章:

javascript - 如何创建跨域HTTP请求

javascript - 在javascript中对多维数组进行排序

javascript - 当我尝试插入迷你图时,我在每一行上得到相同的图表...如何获得正确的图表?

javascript - 使用新的 div 网格刷新页面

javascript - 修复滚动条上的 div 底部

javascript - 将字符串日期格式 "Mon Jun 01 2015 00:00:00 GMT+0100 (IST)"更改为 "YYYY-MM-DD"

javascript - 如何有条件地将数组转换为对象

html - 页眉和页脚 100% 宽度,内容仅 900 像素宽

javascript - Modal 触发时自动播放视频

jquery - 结合 2 个不同的计数代码来创建一个特定的结果