2015-07-11 2 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.

Смежные вопросы