2015-07-11 51 views
-2
var dungeonGenV1 = function(dungeonV1Key){ 
    var firstPartGenList = ["You see a square room with walls made out of packed dirt,", "You see a square room with walls made out of hard stone,", "You see a square room with walls made out of bricked stone,", "You see a square room with walls made out of colorless brick,", "You see a sqare room with walls made out of cold steel,", "You see a circular room with walls made out of packed dirt,", "You see a circular room with walls made out of hard stone,", "You see a circular room with walls made out of bricked stone,", "You see a circular room wtih walls made out of colorless brick,", "You see a circular room with walls made out of colorless brick,", "You see a circular room with walls made out of cold hard steel,"]; 
    var firstPartGenRnd1 = parseInt(Math.floor(Math.random() * firstPartGenList.length)); 
    var secondPartGenList = ["in the center of the room there is a pillar made of stone.", "in the center of the room there is a pillar made of metal.", "in the center of the room there is a pillar made of gold.", "there is 1 zombie in the room walking around.", "there is 2 zombies in the room walking around.", "there is 3 zombies in the room walking around.", "there is 4 zombies in the room walking around.", "there is 5 zombies in the room walking around.", "there is 6 zombies in the room walking around.", "there is 7 zombies in the room walking around.", "there is 8 zombies in the room walking around.", "there is 9 zombies in the room walking around.", "there is 10 zombies in the room walking around."]; 
    var secondPartGenRnd1 = parseInt(Math.floor(Math.random() * secondPartGenList.length)); 

    var monstersInCurrentDungeonRoom = { 
     firstMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     secondMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0  
     }, 
     thirdMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     fourthMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     fifthMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     sixthMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     seventhMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     eighthMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     ninthMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     }, 
     tenthMonster: { 
      name:0, 
      health:0, 
      damageDef:0, 
      fear:0, 
      smarts:0 
     } 
    }; 
    var monsters = { 
     /* all monsters have: 
     Health: The amount of hp, kinda obvious isn't it? 

     DamageDefault: With their hands, weapons add damage onto this so a sword that does\ 
     4 damage plus a goblins damage default is 5 damage, Might later factor in skill with weapons, but\ 
     let's just do simple things 

     fear: Chance of them running away, if you are more powerful, their friends are getting killed\ 
     will probably have it roll a chance on every turn if they run away. \ 
     but I doubt I'll have it like 10= 40%, I might erase fear, and just a special value specific to creatures\ 
     I have -5 as the number that fear is at for undead or controlled creatures. 

     smarts: I don't care right now to change it to intelligence or wisdom, but it's basically\ 
     a thing whether they try different tactics, so they might do more damage, this is just planning\ 
     for the future, I might remove it. 
     I have -5 as the amount for undead or controlled creatures 
     */ 
     goblin: { 
      health:5, 
      damageDef:1, 
      fear: 10, 
      smarts:2 
     }, 
     hobgoblin: { 
      health:7, 
      damageDef:2, 
      fear:7, 
      smarts:4 
     }, 
     zombie: { 
      health:6, 
      damageDef:2, 
      fear:-5, 
      smarts:-5 
     } 
    }; 

    var zombieAdd = function(zombieNumber){ 
     monstersInCurrentDungeonRoom.zombieNumber.name = "Zombie"; 
     monstersInCurrentDungeonRoom.zombieNumber.health = monsters.zombie.health; 
     monstersInCurrentDungeonRoom.zombieNumber.damageDef = monsters.zombie.damageDef; 
     monstersInCurrentDungeonRoom.zombieNumber.fear = monsters.zombie.fear; 
     monstersInCurrentDungeonRoom.zombieNumber.smarts = monsters.zombie.smarts; 
    } 

    if(secondPartGenRnd1 === 0){ 

     //start of pillars 

     console.log("0"); 

    }else if(secondPartGenRnd1 === 1){ 

     console.log("1"); 

    }else if(secondPartGenRnd1 === 2){ 

     console.log("2"); 

    }else if(secondPartGenRnd1 === 3){ 
     //start of first zombies 
     zombieAdd(firstMonster); 
    }else if(secondPartGenRnd1 === 4){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
    }else if(secondPartGenRnd1 === 5){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
    }else if(secondPartGenRnd1 === 6){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
    }else if(secondPartGenRnd1 === 7){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
     zombieAdd(fifthMonster); 
    }else if(secondPartGenRnd1 === 8){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
     zombieAdd(fifthMonster); 
     zombieAdd(sixthMonster); 
    }else if(secondPartGenRnd1 === 9){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
     zombieAdd(fifthMonster); 
     zombieAdd(sixthMonster); 
     zombieAdd(seventhMonster); 
    }else if(secondPartGenRnd1 === 10){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
     zombieAdd(fifthMonster); 
     zombieAdd(sixthMonster); 
     zombieAdd(seventhMonster); 
     zombieAdd(eighthMonster); 
    }else if(secondPartGenRnd1 === 11){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
     zombieAdd(fifthMonster); 
     zombieAdd(sixthMonster); 
     zombieAdd(seventhMonster); 
     zombieAdd(eighthMonster); 
     zombieAdd(ninthMonster); 
    }else if(secondPartGenRnd1 === 12){ 
     zombieAdd(firstMonster); 
     zombieAdd(secondMonster); 
     zombieAdd(thirdMonster); 
     zombieAdd(fourthMonster); 
     zombieAdd(fifthMonster); 
     zombieAdd(sixthMonster); 
     zombieAdd(seventhMonster); 
     zombieAdd(eighthMonster); 
     zombieAdd(ninthMonster); 
     zombieAdd(tenthMonster); 
    }else{ 
     alert("error in monsters for current room function.") 
    } 


    }; 
    //end of dungeonV1 generator 

你好,我在這裏有這個代碼,並且zombieAdd函數應該使用我做的任何事情zombieAdd(firstMonster);然後用它來用殭屍來替換第一個怪物的屬性。運行此通過jsHint顯示沒有錯誤,但運行它時,我得到:如何獲得具有變量的對象的路徑

Uncaught ReferenceError: firstMonster is not defined 

也許,如果它得到了過去,它會顯示secondMonster等,但如何將我設置了這樣我就可以使用該功能。或者,還有更好的方法?

回答

2

firstMonster僅在monstersInCurrentDungeonRoom內定義;當你打電話zombieAdd(firstMonster);你這樣做monstersInCurrentDungeonRoom這是不可能的(它也可能是地牢房的第一個怪物)。

,如果你重寫zombieAdd功能它的工作:

var zombieAdd = function(monster){ 
    monster.name = "Zombie"; 
    monster.health = monsters.zombie.health; 
    monster.damageDef = monsters.zombie.damageDef; 
    monster.fear = monsters.zombie.fear; 
    monster.smarts = monsters.zombie.smarts; 
} 

,並調用它像

zombieAdd(monstersInCurrentDungeonRoom.firstMonster). 

不過,我強烈建議你做一些更多的JavaScript做法,用更短的程序。您將學習一些非常有用的功能,如Arrays

相關問題