做个小游戏,练习js。
Game.prototype.findNear = function(o) {
var iMin = 99999,
index = -1;
for (var i = 0; i < this.len; i++) {
this.item[i].className = '';
if (this.checkPZ(o, this.item[i])) {
var l = dis(o, this.item[i]);
if (iMin > l) {
iMin = l;
index = i;
};
}
};
if (index == -1) {
return null;
} else {
return this.item[index];
};
function dis(o1, o2) {
var c1 = o1.offsetLeft - o2.offsetLeft,
c2 = o1.offsetTop - o2.offsetTop;
return Math.sqrt(c1 * c1 + c2 * c2);
}
};
var t = new Game(5, 5); //行与列,值越大,难度越高
document.getElementById('start').onclick = function() {
t.startGame();
}