从数组中随机选择,然后将选择分配给变量

时间:2015-11-28 20:08:58

标签: javascript arrays variables object random

我有一个从对象中随机选择一个怪物的函数。这是功能:

public Game1 ()
{
    graphics = new GraphicsDeviceManager (this);
    Content.RootDirectory = "Content";              
    graphics.IsFullScreen = true;       
}

protected override void LoadContent ()
{
    spriteBatch = new SpriteBatch (GraphicsDevice);
    texture = this.Content.Load<Texture2D> ("testTexture");
}

这是对象:

var travel = function(direction) {
        var newRoom = rooms[currentRoom.paths[direction]];
        if (!newRoom) {
            $("<p>You can't go that way.</p>").properDisplay();
        }
        else {
            currentRoom = newRoom;
            $("<p>You are now in the " + currentRoom.name + " Room.</p>").properDisplay();
                if (currentRoom.hasMonsters) {
                    function pickRand() {
                        var monsterArray = Object.keys(monsters);   
                        var randomKey = Math.floor(Math.random() * monsterArray.length);
                        return $("<p>Holy Crap!  There's a " + monsterArray[randomKey] + " in here!</p>").properDisplay();  
                    }
                    pickRand();

                }
        }
    };

它设置为随机选择&#34; Zombie&#34;,&#34; Skeleton&#34;或&#34; Ghoul。&#34;一切正常。如何随机选择任何内容并将其保存到变量?

我尝试了几件事:

var monsters = { zombie: { hitPoints: 10, loot: "magic knife" }, skeleton: { hitPoints: 15, loot: "magic shield" }, ghoul: { hitPoints: 12, loot: "magic helm" } };

var beast = pickRand();

但没有运气。我错过了什么?

2 个答案:

答案 0 :(得分:1)

看起来问题是你要返回properDisplay函数的返回值。

如果您使用的是怪物数组而不是地图,则可以在选择随机数据时存储所有怪物信息:

var monsters = [
    {
        name: 'zombie',
        hitPoints: 10,
        loot: "magic knife"
    },
    {
        name: 'skeleton',
        hitPoints: 15,
        loot: "magic shield"
    },
    {
        name: 'ghoul',
        hitPoints: 12,
        loot: "magic helm"
    }
];

function pickRand(arr) {
    var index = Math.floor(Math.random() * arr.length);
    return arr[index];
}

var monster = pickRand(monsters);

现在你有了你的怪物,你可以显示它:

$("<p>Holy Crap!  There's a " + monster.name + " in here!</p>").properDisplay();

演示:https://jsfiddle.net/louisbros/3cu9spfd/

答案 1 :(得分:0)

var beast = pickRand();应该完全奏效。你只需要在调用时正确分配变量吗?

var travel = function(direction) {
        var newRoom = rooms[currentRoom.paths[direction]];
        if (!newRoom) {
            $("<p>You can't go that way.</p>").properDisplay();
        }
        else {
            currentRoom = newRoom;
            $("<p>You are now in the " + currentRoom.name + " Room.</p>").properDisplay();
                if (currentRoom.hasMonsters) {
                    function pickRand() {
                        var monsterArray = Object.keys(monsters);   
                        var randomKey = Math.floor(Math.random() * monsterArray.length);
                        return $("<p>Holy Crap!  There's a " + monsterArray[randomKey] + " in here!</p>").properDisplay();  
                    }
                    var beast = pickRand();
                }
        }
    }