Javascript 绝对位置;在不同的屏幕上获得不同的结果
,如果汽车撞车,推土机运行间隔应在jquery上1700后停止。然而,我的朋友告诉我,推土机并不是停在每台电脑的同一个地方。尽管每个相关元素的位置都被设置为绝对位置,1700在每台计算机中的处理是相同的。为什么呢Javascript 绝对位置;在不同的屏幕上获得不同的结果,javascript,jquery,html,css,Javascript,Jquery,Html,Css,,如果汽车撞车,推土机运行间隔应在jquery上1700后停止。然而,我的朋友告诉我,推土机并不是停在每台电脑的同一个地方。尽管每个相关元素的位置都被设置为绝对位置,1700在每台计算机中的处理是相同的。为什么呢 $(document).ready(function () { var jump = new Audio("jump.wav"); var bulldozer = new Audio("bulldozer.wav"); var warning = new Au
$(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);
}
});
$(文档).ready(函数(){
var jump=新音频(“jump.wav”);
var推土机=新音频(“推土机.wav”);
var warning=新音频(“warning.wav”);
var fireworks=新音频(“fireshouse.wav”);
$('.restart')。单击(函数(){
location.reload();
});
$('.homepage')。单击(函数(){
window.location.href='index.html';
});
$(“.textbox”).focus();
设置超时(areyouready,2000);
函数areyouready()
{
杂碎();
函数throwdice()
{
var random1=Math.floor(Math.random()*150);
var random2=数学地板(数学随机()*150);
$(“.firstnumber”).text(随机数1);
$(“.secondnumber”).text(随机数2);
}
移动式推土机;
移动推土铲=设置间隔(功能(){
var推土机右=parseInt($(“.推土机”).css(“右”))
如果(推土铲右侧<1100){
$(“.推土机”).css(“右”,“+=2”);
}
否则{
$(“.推土机”).css(“右”,“-=1500”);
}
}, 5);
var-winpoint;
winpoint=setInterval(函数()
{
var num1=parseInt($(“.firstnumber”).html();
var num2=parseInt($(“.secondnumber”).html();
var总计=num1+num2;
var entry=parseInt($(“.textbox”).val();
var cartop=parseInt($(“.car2”).css(“top”))
如果(条目=total&&cartp==121)
{
$(“.car2”).css(“顶部”,“-=150”);
函数gobacksoil(){
$(“.car2”).css(“顶部”,“+=150”);
}
设置超时(gobacksoil,1000);
跳。玩();
$(“.textbox”).val(“”);
杂碎();
var oldscore=parseInt($(“.expertscoreboard”).html();
var newscore=oldscore+15;
$(“.expertscoreboard”).html(新闻核心);
如果(新闻核心==150)
{
净距(移动推土机);
$('.推土机').hide();
$('.firstnumber').hide();
$('.secondnumber').hide();
$(“.car2”).css(“顶部”,“+=150”);
$(“.textbox”).val(“你赢了比赛!”);
放烟火;
$('烟花').fadeIn(3000);
$('.textbox').attr(“已禁用”,true);
}
}
否则如果(条目==总计和纸箱!=121)
{
警告。播放();
$(“.textbox”).val(“”);
}
}, 50);
var gameover;
gameover=setInterval(函数(){
var推土机距离=parseInt($(“.推土机”).css(“右”))
var cartop=parseInt($(“.car2”).css(“top”))
如果(推土铲距离==350&&cartop==121)
{
$(“.car2”).addClass(“crashedcar”);
推土机;
$('.textbox').attr(“已禁用”,true);
$('.textbox').val(“游戏结束”);
endinggame=setTimeout(函数()
{
净距(移动推土机);
}, 1700);
}
}, 1);
}
});
推土机在不同位置停止有两个原因
$(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);
}
});
首先,您只检测到准确的位置(
推土铲距离==350
),在检查时不太可能是真的。要解决此问题,您需要使用推土铲距离。请阅读以下内容:请向我们展示移动推土铲的代码。您可以展示更多的代码以及您正在尝试执行的操作,推土铲应在何时停止?哪种浏览器不工作(我假设IE?)推土机在为我碾过汽车后消失,或者没有碾过汽车。这种情况会发生,因为你有一个1毫秒的延迟间隔。实际上,该间隔每10-15毫秒执行一次。间隔并不准确,只能保证至少花费给定的时间,但根据浏览器(和机器)的活动,间隔可能会更长。如果你想知道推土机停止的确切位置,你需要观察它的位置,并在到达想要的位置时缩短间隔。如果我单击“重新启动”,我无法输入答案,它会在游戏结束时卡住