2017-07-08 7 views
0

私は単純なプラットフォームゲームをプログラミングしていますが、私はプレーヤーの動きをプログラムして設定しています。ここに私の更新機能があります:フェイザー - 複数のキーイベントを許可できません

update: function() { 

    game.physics.arcade.collide(this.player, this.platform); 
    game.camera.follow(this.player); 

     if(this.cursor.right.isDown){ 
      this.player.body.velocity.x= 200; 
      this.player.animations.play('correr', 5, true); 
      this.player.scale.x=1; 
     } 

     else if(this.cursor.left.isDown){ 
      this.player.body.velocity.x= -200; 
      this.player.animations.play('correr', 5, true); 
      this.player.scale.x=-1; 
     } 

     else if(this.jump.isDown && this.player.body.wasTouching.down) { 
      this.player.body.velocity.y= -400 
     } 

     else if((this.cursor.right.isDown || this.cursor.left.isDown) && this.jump.isDown){ 
      this.player.body.velocity.x= 200; 
      this.player.body.velocity.y=-200; 
     } 

     else{ 
      this.player.body.velocity.x = 0; 
      this.player.animations.stop(); 
      this.player.frame = 4; 
     } 
    } 

すべてが正常に動作しますが、私の最後の他に、プレイヤーがジャンプして歩くが、それは動作しないべきであると仮定した場合!私の意図は、ジャンプキー+左または右のキーを押しながら歩いている間にジャンプすることができるプレイヤーです。今はちょうど最初にジャンプして歩くことができます。

私は最初のif節でそれを移動しようとしたため、この最後のelseが実行されていないことはわかりませんが、完全に機能しましたが、エラーを特定できません。

ありがとうございます。

答えて

1

問題はあなたの最初のカップルif/elseステートメントです。

これらの2つのビットは、左右の矢印キーをすべてキャプチャします。

if (this.cursor.right.isDown) { 
    // Code is checked first, and will trigger if the right arrow key is down. Nothing else will trigger. 
} else if (this.cursor.left.isDown){ 
    // Code is checked second, and will trigger if the left arrow key is down. Nothing else will trigger. 
} 

これは決して引き起こされないことを意味します。

} else if ((this.cursor.right.isDown || this.cursor.left.isDown) && this.jump.isDown) { 
    // Code will never trigger, since right and left are covered by your first two if/else statements. 
} 

複数のif/else文を実行することもできます。

// here, reset the player velocity 
this.player.body.velocity.x = 0; 

if (this.cursor.right.isDown) { 
    // code 
} else if (this.cursor.left.isDown) { 
    // code 
} else { 
    this.player.animations.stop(); 
    this.player.frame = 4; 
{ 

if (this.jump.isDown && this.player.body.touching.down) { 
    // code 
} else if (...) { 
    // code as needed 
} 

公式のフェイザーチュートリアルcovers this in part 6

+0

感謝を見つけることをお勧めします、私はその方法を試してみます。しかし、私はそれを動かすように修正しました else if((this.cursor.right.isDown || this.cursor.left.isDown)&& this.jump.isDown) 最初の条件を評価し、すべてを解決しました。 – Santiago

0
this.player.body.velocity.x = 0; 

は、「アイドル」状態であると言うことができるため、プレイヤーの開始状態と更新機能の最初の行の1つにする必要があります。

また、私はジャンプタイマーに

var jumpTimer = 0; 
................... 
if(jump && (player.body.onFloor()|| 
player.body.touching.down)&& this.time.now > jumpTimer){ 

     player.body.velocity.y = -400; 
     jumpTimer = this.time.now + 750; 
     player.animations.play('jump'); 
    } 

もっとフェイザー力学の例と、ここでhttps://gamemechanicexplorer.com/#platformer-4

関連する問題