2016-05-20 16 views
-2

誰かが助けてくれることを願っています。私はスタック上のJavaScriptの例を見てきましたが、以下のステートメントを動作させることができました。 OR文を追加すると、エラーが発生します。誰かが構文を助けることができますか?JavaScriptのIFとOR文

if (this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey) 
    || ((this.drawX <= -this.width && this.isLeftKey) { 
    this.speed = 0; 
} else { 
    this.speed = 5; 
} 
+0

条件構文が間違っているようです。これで試してみてください: 'if((this.drawX> = CANVAS_WIDTH - this.width && this.isRightKey == true) ||(this.drawX <= 0 - this.width && this.isLeft Key == true)) ' – dlopez

+0

'isLeft'と 'Key'の間にスペースがあるようですが間違っています。ブール値== trueをチェックする必要はありません。それは定義によって真実または偽です。 – ManoDestra

答えて

1

これを試してみてください:ウルコードで

if ((this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey==true) 
    || (this.drawX <= 0 - this.width && this.isLeft Key==true)) 

    { 
    this.speed = 0; 
    } 

    else { 
    this.speed =5; 
    } 

問題:

1. odd no of `(` , ie. brackets are not closed properly 

2. wrong position of || condition . 

3. this.isLeft Key , space issue 
0

あなたの括弧はオフになっています。 ifの全体の状態は、かっこで囲む必要があります。視認性を向上させるためには、あまりにも括弧で||の分岐ごとにラップする悪い考えではありません。他の回答として

if ((this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey==true) || 
    (this.drawX <= 0 - this.width && this.isLeft Key==true)) { 
     // code comes here... 
0

があなたのコードで問題を言及しており、私は私にお答えしたいと思います。条件と)があまりにも閉じている場合(開口部がありません

  1. 、あなたはそれがない位置で(を持っています。今

    var bool = (this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey === true) || 
          (this.drawX <= 0 - this.width && this.isLeftKey === true); 
    
    this.speed = bool ? 0 : 5; // <=== condition ? true : false; 
    

    あなたのコード内の問題:あなたは、この多くを簡素化することができます必須。

  2. this.isLeft Key単語の間にスペースがあります。
+0

ありがとうございます、すべてのソリューションが完璧に機能しました! – stckpete

+0

私は、この回答ではポイント2のすべての他の解を疑う。 – Jai