如何创建对象并放入数组中,以便可以多次使用它
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();
}
我想要这两个函数在一个数组上,所以我不会再写一个