In my game,如果汽车坠毁,推土机运行间隔应该在1700之后在jquery上停止。但是,我的朋友告诉我,推土机不会停在每台电脑的同一个地方。虽然每个相关元素的位置设置为绝对,但是在每台计算机中处理相同的1700。这是为什么?
$(document).ready(function () {
var jump = new Audio("jump.wav");
var bulldozer = new Audio("bulldozer.wav");
var warning = new Audio("warning.wav");
var fireworks = new Audio("fireworks.wav");
$('.restart').click(function () {
location.reload();
});
$('.homepage').click(function () {
window.location.href = 'index.html';
});
$(".textbox").focus();
setTimeout(areyouready, 2000);
function areyouready()
{
throwdice();
function throwdice()
{
var random1 = Math.floor(Math.random() * 150);
var random2 = Math.floor(Math.random() * 150);
$(".firstnumber").text(random1);
$(".secondnumber").text(random2);
}
var movingbulldozer;
movingbulldozer = setInterval(function () {
var bulldozerright = parseInt($(".bulldozer").css("right"))
if (bulldozerright < 1100) {
$(".bulldozer").css("right", "+=2");
}
else {
$(".bulldozer").css("right", "-=1500");
}
}, 5);
var winpoint;
winpoint = setInterval(function ()
{
var num1 = parseInt($(".firstnumber").html());
var num2 = parseInt($(".secondnumber").html());
var total = num1 + num2;
var entry = parseInt($(".textbox").val());
var cartop = parseInt($(".car2").css("top"))
if (entry == total && cartop == 121)
{
$(".car2").css("top", "-=150");
function gobacksoil() {
$(".car2").css("top", "+=150");
}
setTimeout(gobacksoil, 1000);
jump.play();
$(".textbox").val("");
throwdice();
var oldscore = parseInt($(".expertscoreboard").html());
var newscore = oldscore + 15;
$(".expertscoreboard").html(newscore);
if (newscore == 150)
{
clearInterval(movingbulldozer);
$('.bulldozer').hide();
$('.firstnumber').hide();
$('.secondnumber').hide();
$(".car2").css("top", "+=150");
$(".textbox").val("YOU WON THE GAME!");
fireworks.play();
$('.fireworks').fadeIn(3000);
$('.textbox').attr("disabled", true);
}
}
else if (entry == total && cartop != 121)
{
warning.play();
$(".textbox").val("");
}
}, 50);
var gameover;
gameover = setInterval(function () {
var bulldozerdistance = parseInt($(".bulldozer").css("right"))
var cartop = parseInt($(".car2").css("top"))
if (bulldozerdistance == 350 && cartop == 121)
{
$(".car2").addClass("crashedcar");
bulldozer.play();
$('.textbox').attr("disabled", true);
$('.textbox').val("game over");
endinggame = setTimeout(function ()
{
clearInterval(movingbulldozer);
}, 1700);
}
}, 1);
}
});
答案 0 :(得分:3)
推土机停在不同位置有两个原因。
首先,您只检测到一个准确的位置(bulldozerdistance == 350
),这在检查时不太可能。要解决此问题,您需要使用bulldozerdistance <= 350
。
第二个是由第一个引起的。大多数时候推土机只在间隔停止时停止。然而,实际上每10-15毫秒执行一次间隔(1毫秒)。间隔时间不准确,它们只保证至少在给定的时间内,但根据浏览器(和机器)的活动,它们可能通常会更长。