我使用KineticJS的transitionTo在箭頭鍵(keydown事件)的幫助下製作了一個形狀。
我有2個問題:
1.按壓形狀的移動具有短延遲密鑰後。我如何消除延遲? ?
2.如何讓形狀同時按下兩個箭頭鍵角移動這裏是JavaScript:Keydown的KineticJS形狀運動
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);
}
});
小提琴:http://jsfiddle.net/uUWmC/1/
1.看不到任何延遲,感覺響應我。 2.請參閱[這裏](http://stackoverflow.com/questions/7614340/listen-to-multiple-keydowns)(玩'超時值')。爲什麼你在'setTimeout'中包含'transitionTo'? –