如何在按下但不连续设置键盘按键事件后仅按下一次
How to set keyboard key pressed event only once after pressed but not continously
我正在使用下面的代码将播放器向上移动,但问题是只要按住按钮播放器就会继续移动。我怎样才能改变这种行为,以便无论玩家按下多少次按钮只移动一次?
if (cursor.up.isDown){
player.body.velocity.y = -200;
player.animations.stop('move');
}
充当触发器开关的 bool 应该完成这项工作:
var flipFlop;
function update() {
if (cursor.up.isDown){
if (!flipFlop) {
player.body.velocity.y = -200;
player.animations.stop('move');
flipFlop = true;
}
}
if (cursor.up.isUp) {
flipFlop = false;
}
}
注意flipFlop变量是在更新循环外声明的,否则每帧都会重新创建。
Kamen Minkov 的答案确实有效,但如果你的想法是跳跃,则 bool 将不起作用,一旦你释放按钮,你可以再次按下它并继续向上,甚至不接触任何地面。
但您可以使用此功能来验证 body 是否着陆
function touchingDown(someone) {
var yAxis = p2.vec2.fromValues(0, 1);
var result = false;
for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) {
var c = game.physics.p2.world.narrowphase.contactEquations[i];
if (c.bodyA === someone.data || c.bodyB === someone.data) {
var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis
if (c.bodyA === someone.data) d *= -1;
if (d > 0.5) result = true;
}
} return result;
}
并调用发送 body
if ( (cursor.up.isDown) && (touchingDown(player.body)) ){
player.body.velocity.y = -200;
player.animations.stop('move');
}
OBS:P2 Physics 的功能,但对于街机,body 已经有一个字段说明它是否正在着陆。
我知道这是一个古老的 post 但我也被困在这里并使用你的例子来想出这个简单好用的解决方案
this.speed = 100
this.player.setVelocity(0,0)
if (this.cursors.left.isDown){
this.player.setVelocityX(-this.speed)
}else if(this.cursors.right.isDown){
this.player.setVelocityX(this.speed)
}else{
this.player.setVelocityX(0)
}if(this.cursors.up.isDown){
this.player.setVelocityY(-this.speed)
}else if (this.cursors.down.isDown){
this.player.setVelocityY(+this.speed)
}else{
this.player.setVelocityY(0)
}
首先,我们将玩家速度设置为 0 和 0(x 和 y)。
我们检查 x 轴上的任何更改,否则将其重置为 0。
检查 y 轴是否有变化,否则也将其重置为 0。
我发现这个可以让角色以更流畅的动作移动。并且还允许将左侧和上方等压在一起。
如果你需要角色移动得更方,那么将速度更改为 .x 或 .y
我正在使用下面的代码将播放器向上移动,但问题是只要按住按钮播放器就会继续移动。我怎样才能改变这种行为,以便无论玩家按下多少次按钮只移动一次?
if (cursor.up.isDown){
player.body.velocity.y = -200;
player.animations.stop('move');
}
充当触发器开关的 bool 应该完成这项工作:
var flipFlop;
function update() {
if (cursor.up.isDown){
if (!flipFlop) {
player.body.velocity.y = -200;
player.animations.stop('move');
flipFlop = true;
}
}
if (cursor.up.isUp) {
flipFlop = false;
}
}
注意flipFlop变量是在更新循环外声明的,否则每帧都会重新创建。
Kamen Minkov 的答案确实有效,但如果你的想法是跳跃,则 bool 将不起作用,一旦你释放按钮,你可以再次按下它并继续向上,甚至不接触任何地面。
但您可以使用此功能来验证 body 是否着陆
function touchingDown(someone) {
var yAxis = p2.vec2.fromValues(0, 1);
var result = false;
for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) {
var c = game.physics.p2.world.narrowphase.contactEquations[i];
if (c.bodyA === someone.data || c.bodyB === someone.data) {
var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis
if (c.bodyA === someone.data) d *= -1;
if (d > 0.5) result = true;
}
} return result;
}
并调用发送 body
if ( (cursor.up.isDown) && (touchingDown(player.body)) ){
player.body.velocity.y = -200;
player.animations.stop('move');
}
OBS:P2 Physics 的功能,但对于街机,body 已经有一个字段说明它是否正在着陆。
我知道这是一个古老的 post 但我也被困在这里并使用你的例子来想出这个简单好用的解决方案
this.speed = 100
this.player.setVelocity(0,0)
if (this.cursors.left.isDown){
this.player.setVelocityX(-this.speed)
}else if(this.cursors.right.isDown){
this.player.setVelocityX(this.speed)
}else{
this.player.setVelocityX(0)
}if(this.cursors.up.isDown){
this.player.setVelocityY(-this.speed)
}else if (this.cursors.down.isDown){
this.player.setVelocityY(+this.speed)
}else{
this.player.setVelocityY(0)
}
首先,我们将玩家速度设置为 0 和 0(x 和 y)。 我们检查 x 轴上的任何更改,否则将其重置为 0。 检查 y 轴是否有变化,否则也将其重置为 0。
我发现这个可以让角色以更流畅的动作移动。并且还允许将左侧和上方等压在一起。 如果你需要角色移动得更方,那么将速度更改为 .x 或 .y