【问题标题】:Canvas diagonal movement [closed]帆布对角线运动[关闭]
【发布时间】:2014-09-28 22:59:52
【问题描述】:

我正在尝试用 HTML5 组合一个简单的“游戏”。但是我无法进行对角线运动。 “对角线运动”仅在两个按钮同时按下时才有效。即使这样,它也会移动一次。代码如下:

    // Getting canvas, and canvas context
    var c = document.getElementById("canvas");
    var ctx = c.getContext("2d");
    //Keymap, later passed as method parameter
    var map = [false,false,false,false];
    // Top, Right, Bottom, Left


    // Function for resetting keymap
    var resetMap = function() {
       for(i=0;i<map.length;i++) {
           map[i] = false;
       };
    };


    //Function for clearing the screen before drawing
    var clrScrn = function() {
        ctx.clearRect(0,0,500,500);  
    };

    // The player character
    var character = function() {
        this.x = 50;
        this.y = 50;
        this.h = 50;
        this.w = 50;
    };

        // Draw method of the character class
        character.prototype.draw = function() {
          ctx.fillRect(this.x,this.y,this.h,this.w);  
        };

        // The move method of the character class
        character.prototype.move = function(map) {


            if(map[0] === true) {
                if(map[1] == true) {
                    this.x+=5;
                    this.y-=5;
                    console.log("Pressed at the same time");
                }
                else {
                    this.y-=5;
                }
            }

            else if(map[1] === true) {
                this.x+=5;
            }              
            else if(map[2] === true) {
                this.y+=5;
            }              
            else if(map[3] === true) {
                this.x-=5;
            }
        };


    //Making a new character
    var player = new character();
        player.draw();


   // Drawing everything on screen
   var render = function() {

       clrScrn(); 
       player.move(map);
       player.draw();
       resetMap();

       requestAnimFrame(render);
   };

    //Calling  the render function
   render();

    //Binding event listener to window,checking key down, likely the source of the problem
    window.addEventListener("keydown",function(e){


        if(e.keyCode == 38 && e.keyCode == 39) {
            map[0] = true;
            map[1] = true;
        }

        else if(e.keyCode == 38) {
            map[0] = true;
         }
        if(e.keyCode == 39) {
            map[1] = true;
         }
        if(e.keyCode == 40) {
            map[2] = true;
         }
        if(e.keyCode == 37) {
            map[3] = true;
         }

         console.log(e.keyCode);

    },false);


    //Binding event listener to key up
    window.addEventListener("keyup",function(e){
        resetMap();
    },false);

【问题讨论】:

    标签: javascript html canvas


    【解决方案1】:

    在你的渲染函数中:

       player.move(map); // you move the player
       player.draw();    // you draw the player
       resetMap();       // you forget all pressed keys
    

    resetMap() 是您需要再次按下按键才能再移动一步的原因。

    请注意,当长时间按下某个键时,由于键盘重复,水平和垂直运动可能会意外起作用。但是你不应该依赖于游戏的按键重复。您应该分别检测各个键的按下和按下键,以确定是否按下了某个键。

    【讨论】:

    • 天哪,你是对的。多么愚蠢的错误。谢谢!
    【解决方案2】:

    尝试查看: JavaScript multiple keys pressed at once

    然后稍微重新处理你的 if/else 逻辑。

    【讨论】:

    • 感谢您的建议,虽然我之前已经找到了该线程。当前的 if else 逻辑仅用于演示问题。
    猜你喜欢
    • 2021-05-31
    • 2023-04-07
    • 2017-08-10
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2019-05-01
    相关资源
    最近更新 更多