2016-07-28 5 views
0

私は基本的なテキストアドベンチャーゲームを作成しようとしており、自分のキャラクターにダメージを与えるのに苦しんでいるようです。私はダメージの数字を得ることができますが、何らかの理由で彼らの健康に影響を与えているようには見えません。詳細に見なければ、あなたのコードが動作しない1つの明白な理由は、コードの以下の部分が間違っているということです損害はオブジェクトに適用されないようですか?

function Person(firstName, lastName, hp, concious, str, def, agi, frc) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.hp  = hp; 
    this.concious = concious; 
    this.str  = str; 
    this.def  = def; 
    this.agi  = agi; 
    this.frc  = frc; 

    var sayName = function(p) { 
     console.log(p.firstName); 
    } 

    this.attack = function(Person) { 
     var attPow = Math.floor(Math.random() * this.str); 
     var attSpe = Math.floor(Math.random() * this.agi); 
     var defAtt = Math.floor(Math.random() * Person.def); 
     var defAgi = Math.floor(Math.random() * Person.agi); 
     console.log(attPow, attSpe, defAtt, defAgi); 

     if (attPow > defAtt && attSpe > defAgi){ 
      return Person.hp -= attPow + attSpe - defAtt; 
      console.log(Person.hp); 
     } if(attPow < defAtt && attSpe < defAgi) { 
      alert("You missed"); 
     } if (attPow < defAtt) { 
      alert("He blocked you're attack"); 
     } if (attSpe < defAgi) { 
      alert("He dodged you're attack"); 
     } 
    } 
}; 

var Angelo = new Person("Angelo", " ", 75, 50, 5, 5, 5, 5); 
var boy = new Person("boy","dead", 75,50,5,2,5,5); 
Angelo.attack(boy); 

答えて

0

「エンジェルポリチスの答え」と指摘されています。あなたのシナリオでは、else-ifキーワードを使用して、すべてのifステートメントをブロックにまとめる必要があります。

コードは問題ありませんが、断続的にしか動作しません。理由はあなたがケータリングしていないifブロックに特定のケースがないためです。条件チェックにelseを追加すると、それに気づくでしょう。

if (attPow > defAtt && attSpe > defAgi){ 
    console.log("ATTACK!"); 
    return Person.hp -= attPow + attSpe - defAtt; 
} else if(attPow < defAtt && attSpe < defAgi) { 
    console.log("You missed"); 
} else if (attPow < defAtt) { 
    console.log("He blocked you're attack"); 
} else if (attSpe < defAgi) { 
    console.log("He dodged you're attack"); 
} 
else { 
    console.log("????"); 
} 

var Angelo = new Person("Angelo", " ", 75, 50, 5, 5, 5, 5); 
var boy = new Person("boy","dead", 75,50,5,2,5,5); 
console.log("before HP:" + boy.hp); 
Angelo.attack(boy); 
console.log("after HP:" + boy.hp); 

少年のHPには何の影響もありません。あなたはこれを見るでしょう。

出力:HPの前に

:75
???? HPの後
:75

お知らせconsole.log("????")がトリガーましたか?これは、あなたが処理していないシナリオがないことを意味します。 、defAttattSpedefAgiの変数をelse句の変数にダンプし、欠落しているものがあるかどうかを確認することをお勧めします。

+0

ああ、私はそれを得ていると思う。ダンプでは、方程式全体を完全に削除することを意味しますか? – l34lo1

+0

@ l34lo1 - ダンプは、変数の値を出力することを意味します。 :Pそれが助けてくれることを望みます。まだ質問がある場合は教えてください。私があなたを助けてくれたなら、私の答えの横にあるチェックマークをクリックすることで答えをマークすることができます。 –

0

if (attPow > defAtt && attSpe > defAgi){ 
    return Person.hp -= attPow + attSpe - defAtt; 
    console.log(Person.hp); 
} if(attPow < defAtt && attSpe < defAgi) { 
    alert("You missed"); 
} if (attPow < defAtt) { 
    alert("He blocked you're attack"); 
} if (attSpe < defAgi) { 
    alert("He dodged you're attack"); 
} 

完全なチェックを作成するには、持っていますチェーンに次の形式でこれらのif文:

if (attPow > defAtt && attSpe > defAgi){ 
    return Person.hp -= attPow + attSpe - defAtt; 
    console.log(Person.hp); 
} else if(attPow < defAtt && attSpe < defAgi) { 
    alert("You missed"); 
} else if (attPow < defAtt) { 
    alert("He blocked you're attack"); 
} else if (attSpe < defAgi) { 
    alert("He dodged you're attack"); 
} 
関連する問題