0

嗨,如何多次使用一个方法而不在 JavaScript 中重复和创建另一个方法。因为我所做的是我创建了一个具有相同字段和代码的方法,但唯一的区别是 id。我需要每个方法的一个参数,即 id。

function enemyGet() {

    new enemyMove2()
    new enemyMove()
    new enemyMove3()
}

  function enemyMove2() {

    var enemyimage = document.getElementById("enemy");
        var leftenemy = parseInt(enemyimage.style.left);
        var topenemy = parseInt(enemyimage.style.top);
        var time = setTimeout("enemyMove2()", 20)

        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();

}

function enemyMove() {


    var enemyimage = document.getElementById("enemy2");
    var leftenemy = parseInt(enemyimage.style.left);
    var topenemy = parseInt(enemyimage.style.top);
    var time = setTimeout("enemyMove()", 20)

    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();

}

function enemyMove3() {


    var enemyimage = document.getElementById("enemy3");
    var leftenemy = parseInt(enemyimage.style.left);
    var topenemy = parseInt(enemyimage.style.top);
    var time = setTimeout("enemyMove3()", 20)

    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

3 回答 3

2

使用参数:

function enemyGet() {
    // pass the id as a argument to the function
    enemyMove("enemy2")
    enemyMove("enemy")
    enemyMove("enemy3")
}

function enemyMove(enemy) {
    // the variable enemy is the id passed as an argument to the function 
    var enemyimage = document.getElementById(enemy);
        var leftenemy = parseInt(enemyimage.style.left);
        var topenemy = parseInt(enemyimage.style.top);
        // Pass the parameter in timeout as well. You have access to enemy variable due to concept of closure
        var time = setTimeout(function(){enemyMove(enemy)}, 20)

        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();

}
于 2013-04-04T02:33:37.003 回答
1

在这种情况下,您可以参数化调用之间变化的值。

您可以将变量id作为参数传递给该enemyMove方法。

function enemyMove(id) {


    var enemyimage = document.getElementById(id);
    var leftenemy = parseInt(enemyimage.style.left);
    var topenemy = parseInt(enemyimage.style.top);
    var time = setTimeout(function(){
        enemyMove(id);
    }, 20)

    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();

}

然后

function enemyGet() {

    new enemyMove('enemy')
    new enemyMove('enemy2')
    new enemyMove('enemy3')
}
于 2013-04-04T02:31:46.887 回答
-1

您可以使用方括号表示法来引用该函数:

// Store reference to global object for use wherever
var global = this;

...
  var n = 3;

  // Call method based on value of n
  global['enemyMove' + n]();

...

您可以将您的函数属性(方法)设置为全局/窗口对象以外的某个对象。

于 2013-04-04T02:36:49.190 回答