-3

如何创建对象并放入数组中,以便可以多次使用它

var createNew = new enemyMove(id);

所以每次我使用它我都会有不同的值和字段

function enemyMove() {


    var enemyimagew = document.getElementById("enemy");
    var leftenemy = parseInt(enemyimagew.style.left);
    var topenemy = parseInt(enemyimagew.style.top);


        if (leftenemy < leftplayer) {
            leftenemy = leftenemy + 5;

        }
        if (leftenemy > leftplayer) {
            leftenemy = leftenemy - 5;

        }

        if (topenemy < topplayer) {
            topenemy = topenemy + 5;
        }
        if (topenemy > topplayer) {
            topenemy = topenemy - 5;
        }

        enemyimagew.style.left = leftenemy + "px";
        enemyimagew.style.top = topenemy + "px";

        var hit_list = $("#x").collision(".lollol");
        hit_list.remove();   
}

function enemyMovetow() {


    var enemyimage = document.getElementById("enemy2");
    var leftenemy = parseInt(enemyimage.style.left);
    var topenemy = parseInt(enemyimage.style.top);


    if (leftenemy < leftplayer) {
        leftenemy = leftenemy + 5;

    }
    if (leftenemy > leftplayer) {
        leftenemy = leftenemy - 5;

    }

    if (topenemy < topplayer) {
        topenemy = topenemy + 5;
    }
    if (topenemy > topplayer) {
        topenemy = topenemy - 5;
    }

    enemyimage.style.left = leftenemy + "px";
    enemyimage.style.top = topenemy + "px";

    var hit_list = $("#x").collision(".lollol");
    hit_list.remove();
}

我想要这两个函数在一个数组上,所以我不会再写一个

4

1 回答 1

3

这是你想要的吗?

var enemies = [
    new enemyMove(id), new ememyMove(id + 1), new enemyMove(id + 2), // ...
];

如果您澄清更多,我可以进一步帮助您。

于 2013-04-01T18:37:22.370 回答