douben6670 2014-02-05 19:18
浏览 44
已采纳

在Javascript中从数组运行代码的最佳和最安全的方法是什么?

So this is most likely the wrong way to do this, but I've been giving some thought to this approach for a while and I can't think of a better way to do it.

I am working on a roguelike game with a friend using Javascript and Canvas for the front end and storing our data (monsters, items, etc) into a MySQL database using PHP.

We have divided our playing field into four separate quadrants. The entire play field is a 20 x 20 square and so we have been coding 10 x 10 dungeon quadrants. The idea is to grab code from an array with dungeon quadrant code within it and somehow execute this code. Currently we have four quadrant functions we are calling which draw the environment objects within those quadrants and give them values, but those are hard coded in the functions. I would like to be able to randomly choose a value from the array and place that code inside those functions instead.

I've heard tell of a mysterious and black magic laden procedure called eval(). Do I need to turn to the dark side or is there a better way?

I'll include a little code so you can see the basic idea.

////Matrix creation / declaration
var coordinates = new Array(mapWidth);
for (var i = 0; i <mapWidth; i++) {
    coordinates[i] = new Array(mapHeight);          
}

//sets array to 0 which is not an object
for (var i=0; i<mapWidth; i++) {
    for (var j=0; j<mapHeight; j++) {
        coordinates[i][j] = 0;
    }
}

function quadrantOneLoader()
{
    var x = 0;
    var y = 0;

    for (var i = 0; i < 10; i++)
    {
        if (i % 2 === 1 && i != 3)
        {
            coordinates[x + i][y + 1] = new environment();
            coordinates[x + i][y + 1].image.src = "images/column.png";
        }
        else if (i % 2 === 1)
        {
            coordinates[x + i][y + 1] = new environment();
            coordinates[x + i][y + 1].image.src = "images/brokenColumn.png";
        }
    }

    for (var i = 0; i < 10; i++)
    {
        if (i % 2 === 0 && i !== 8)
        {
            coordinates[x + i][y + 3] = new environment();
            coordinates[x + i][y + 3].image.src = "images/column.png";
        }
        else if (i % 2 === 0)
        {
            coordinates[x + i][y + 3] = new environment();
            coordinates[x + i][y + 3].image.src = "images/brokenColumn.png";
        }
    }

    for (var i = 0; i < 10; i++)
    {
        if (i % 2 === 0 && i !== 4)
        {
            coordinates[x + i][y + 7] = new environment();
            coordinates[x + i][y + 7].image.src = "images/column.png";
        }
        else if (i % 2 === 0)
        {
            coordinates[x + i][y + 7] = new environment();
            coordinates[x + i][y + 7].image.src = "images/brokenColumn.png";
        }
    }

    for (var i = 0; i < 10; i++)
    {
        if (i % 2 === 1 && i !== 7)
        {
            coordinates[x + i][y + 9] = new environment();
            coordinates[x + i][y + 9].image.src = "images/column.png";
        }
        else if (i % 2 === 1)
        {
            coordinates[x + i][y + 9] = new environment();
            coordinates[x + i][y + 9].image.src = "images/brokenColumn.png";
        }
    }

    coordinates[x + 1][y + 5] = new environment();
    coordinates[x + 1][y + 5].image.src = "images/stocks.png";

    coordinates[x + 4][y + 5] = new environment();
    coordinates[x + 4][y + 5].image.src = "images/candelabra.png";

    coordinates[x + 7][y + 5] = new environment();
    coordinates[x + 7][y + 5].image.src = "images/stocks.png";
}

So basically the code that is inside the quadrantOneLoader() function would be placed inside an array. I want to inject that into this function so it would look more like this:

function quadrantOneLoader()
{
    var x = 0;
    var y = 0;

    quadrants[Math.floor(Math.random()*quadrants.length())];
}
  • 写回答

2条回答 默认 最新

  • douchunxian9740 2014-03-04 19:14
    关注

    We ended up doing something simple like this:

    var dungeonCode = new Array();
    
    dungeonCode[0] = function(/*pass whatever you want here, we chose to pass x and y*/)
    {
        //code to place the dungeon tiles here
    }
    

    Then to call it all you do is:

    dungeonCode[0]();
    

    We ended up making a bunch of these dungeon quadrants and calling them using a random number generator.

    I guess the parenthesis tell the interpreter to execute whatever code contained within the array as a function. Neat!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况