修改这段代码,使一个兔子在跳跃时,不在对该兔子调用randomBunny函数
时间: 2024-03-25 18:37:01 浏览: 16
以下是修改后的代码:
```
var jumptimer;
function bunnyjump(jumpingbunnyId) {
var id = "bunny" + jumpingbunnyId;
var count = 1;
var offset = 10;
var direction = "up";
// 停止随机选择兔子跳跃的定时器
clearInterval(randomevent);
jumptimer = setInterval(function() {
var bunny = document.getElementById(id);
if (count == 21) {
direction = "down";
} else if (count == 0) {
direction = "up";
}
if (direction == "up") {
count++;
} else if (direction == "down") {
count--;
}
bunny.style.bottom = count * offset + "px";
}, 70);
bunny.jumpevent = setInterval(function() {
var bunny = document.getElementById(id);
if (count == 21) {
direction = "down";
} else if (count == 0) {
direction = "up";
}
if (direction == "up") {
count++;
} else if (direction == "down") {
count--;
}
bunny.style.bottom = count * offset + "px";
}, 70);
}
function kill(bunny) {
var id_str = bunny.id;
var id = parseInt(id_str[id_str.length - 1]);
var blinksrc = "img/" + bunny.id + "-blink.png";
bunny.style.backgroundImage = "url(" + blinksrc + ")";
setTimeout(function() {
clearInterval(bunny.jumpevent);
bunny.jumpevent = null;
bunny.style.display = "none";
// 重新启动随机选择兔子跳跃的定时器
randomevent = setInterval(function() {
randomBunny();
}, 1000);
}, 200);
}
var bunnies = 5;
var randomevent = null;
function randomBunny() {
var random = Math.ceil(Math.random() * bunnies);
bunnyjump(random);
}
// 初始时启动随机选择兔子跳跃的定时器
randomevent = setInterval(function() {
randomBunny();
}, 1000);
```
修改后的代码在 `bunnyjump` 函数中,添加了一行 `clearInterval(randomevent);`,该行代码会停止随机选择兔子跳跃的定时器。在 `kill` 函数中,添加了一行 `randomevent = setInterval(function() { randomBunny(); }, 1000);`,该行代码会重新启动随机选择兔子跳跃的定时器。这样,当一个兔子在跳跃时,不会再次被随机选择并跳跃。