简体   繁体   English

带Keydown的KineticJS形状运动

[英]KineticJS Shape Movement with Keydown

I have made a shape that I move with the help of arrow keys (keydown event) using transitionTo from KineticJS. 我已经使用KineticJS中的transitionTo借助箭头键(按下事件)移动了形状。

I have 2 problems: 我有2个问题:

1. After pressing the key the movement of the shape has a short delay. 1.按下键后,形状的移动会短暂延迟。 How do I remove the delay? 如何消除延迟?

2. How do I make the shape move diagonally with two arrow keys pressed at the same time?Here is the javascript: 2.如何在同时按下两个箭头键的情况下使形状沿对角线移动?这是JavaScript:

var stage = new Kinetic.Stage({
    container: 'container',
    width: screen.width,
    height: 500
});
var layer = new Kinetic.Layer();

var circle = new Kinetic.Circle({
    x: stage.getWidth() / 2,
    y: stage.getHeight() / 2,
    radius: 70,
    fill: 'yellow',
    stroke: 'black',
    strokeWidth: 4
});

// add the shape to the layer
layer.add(circle);

// add the layer to the stage
stage.add(layer);

window.addEventListener('keydown', function(e) {
    if (e.keyCode == 37) { //Left Arrow Key
        setTimeout(function () {
            circle.transitionTo({
                x: circle.getX() - 10,
                y: circle.getY(),
                duration: 0.01
            })
        }, 0);
    }
    if (e.keyCode == 38) { //Up Arrow Key
        setTimeout(function () {
            circle.transitionTo({
                x: circle.getX(),
                y: circle.getY() - 10,
                duration: 0.01
            })
        }, 0);
    }
    if (e.keyCode == 39) { //Right Arrow Key
        setTimeout(function () {
            circle.transitionTo({
                x: circle.getX() + 10,
                y: circle.getY(),
                duration: 0.01
            })
        }, 0);
    }
    if (e.keyCode == 40) { //Top Arrow Key
        setTimeout(function () {
            circle.transitionTo({
                x: circle.getX(),
                y: circle.getY() + 10,
                duration: 0.01
            })
        }, 0);
    }
});

Fiddle: http://jsfiddle.net/uUWmC/1/ 小提琴: http : //jsfiddle.net/uUWmC/1/

For the delay, you can unwrap setTimeout and transitionTo like the following; 对于延迟,您可以打开setTimeout和transitionTo,如下所示;

window.addEventListener('keydown', function(e) {
    if (e.keyCode == 37) //Left Arrow Key
        circle.attrs.x -= 10;
    if (e.keyCode == 38) //Up Arrow Key
        circle.attrs.y += 10;
    if (e.keyCode == 39) //Right Arrow Key
        circle.attrs.x += 10;
    if (e.keyCode == 40) //Top Arrow Key
        circle.attrs.y -= 10;
    layer.draw();
});

For the diagonal move, you cannot press two arrow keys at the same time. 对于对角线移动,不能同时按两个箭头键。 This is your operating system limit. 这是您的操作系统限制。 You can press it with alt key, ctrl key ... etc though. 您可以使用alt键,ctrl键...等来按下它。

The best way to detect diagonal movement to keep track of which keys have been pressed/released. 检测对角线运动的最佳方法,以跟踪已按下/释放的键。 I use a jQuery addon called "key_status.js" which allows you to check any key's status with something like: 我使用了一个名为“ key_status.js”的jQuery插件,它使您可以通过以下方式检查任何键的状态:

if (keydown.left) {
  console.log("left arrow is down")
}
if (keydown.left && keydown.up) {
  console.log("Left/Up Diagonal!")
}

Of course to do something like this you will need to wrap all the input checking in a setTimeout or requestAnimFrame. 当然,要做类似的事情,您需要将所有输入检查都包装在setTimeout或requestAnimFrame中。

I discovered this script and method at the excellent html5 game tutorial here: http://www.html5rocks.com/en/tutorials/canvas/notearsgame/ 我在以下出色的html5游戏教程中发现了此脚本和方法: http : //www.html5rocks.com/en/tutorials/canvas/notearsgame/

Direct Link To script: ( http://strd6.com/space_demo/javascripts/key_status.js ) 直接连结至指令码:( http://strd6.com/space_demo/javascripts/key_status.js

There are libraries that can help you identify key combinations. 有一些库可以帮助您识别按键组合。 Check out: 查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM