如何使用变量获取对象的路径

时间:2015-07-11 14:07:44

标签: javascript

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等等,但是我如何设置它以便我可以使用该功能。或者有更好的方法吗?

1 个答案:

答案 0 :(得分: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