Funkcja niechce sie powtarzac co okreslony czas
oto kod
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
function gonie(i)
{
var char = $("#character").position();
var char2 = $("#"+i+"").position();
if(char.left<char2.left)
{
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-70px 0px", left: char2.left - 3 });
}, 40);//szybkosc
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-35px 0px", left: char2.left - 6 });
}, 80);//szybkosc
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "0px 0px", left: char2.left - 9 });
}, 120);//szybkosc
}
else if(char.left>char2.left)
{
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-70px -70px", left: char2.left + 3 });
}, 40);//szybkosc
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-35px -70px", left: char2.left + 6 });
}, 80);//szybkosc
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "0px -70px", left: char2.left + 9 });
}, 120);//szybkosc
}
if(char.top<char2.top)
{
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-70px -35px", top: char2.top - 3 });
}, 40);
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-35px -35px", top: char2.top - 6 });
}, 80);
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "0px -35px", top: char2.top - 9 });
}, 120);
}
else if(char.top>char2.top)
{
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-70px -105px", top: char2.top + 3 });
}, 40);
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "-35px -105px", top: char2.top + 6 });
}, 80);
setTimeout(function() {
$("#"+i+"").css({ backgroundPosition: "0px -105px", top: char2.top + 9 });
}, 120);//szybkosc
}
}
setInterval("gonie('character2')",240);
$(".health div").animate({ width: 25 }, 1000);
$(".health2 div").animate({ width: 25 }, 1000);
var last_walk = 0;
$("html").keydown(function(e) {
if (new Date().getTime() - last_walk < 300) {//predkosc
return true;
}
var char = $("#character").position();
if (e.keyCode == 37) {
setTimeout(function() {
$("#character").css({ backgroundPosition: "-70px 0px", left: char.left - 3 });
}, 100);//szybkosc
setTimeout(function() {
$("#character").css({ backgroundPosition: "-35px 0px", left: char.left - 6 });
}, 200);//szybkosc
setTimeout(function() {
$("#character").css({ backgroundPosition: "0px 0px", left: char.left - 9 });
}, 300);//szybkosc
}
else if (e.keyCode == 38) {
setTimeout(function() {
$("#character").css({ backgroundPosition: "-70px -35px", top: char.top - 3 });
}, 100);
setTimeout(function() {
$("#character").css({ backgroundPosition: "-35px -35px", top: char.top - 6 });
}, 200);
setTimeout(function() {
$("#character").css({ backgroundPosition: "0px -35px", top: char.top - 9 });
}, 300);
} else if (e.keyCode == 39) {
setTimeout(function() {
$("#character").css({ backgroundPosition: "-70px -70px", left: char.left + 3 });
}, 100);
setTimeout(function() {
$("#character").css({ backgroundPosition: "-35px -70px", left: char.left + 6 });
}, 200);
setTimeout(function() {
$("#character").css({ backgroundPosition: "0px -70px", left: char.left + 9 });
}, 300);
} else if (e.keyCode == 40) {
setTimeout(function() {
$("#character").css({ backgroundPosition: "-70px -105px", top: char.top + 3 });
}, 100);
setTimeout(function() {
$("#character").css({ backgroundPosition: "-35px -105px", top: char.top + 6 });
}, 200);
setTimeout(function() {
$("#character").css({ backgroundPosition: "0px -105px", top: char.top + 9 });
}, 300);
}
if (char.left <= 0) {
setTimeout(function() {
$("#character").css({ backgroundPosition: "0px 0px", left: char.left + 800 });
$("#a").css({ background:"url(bg.jpg)"});
}, 300);//szybkosc
}
last_walk = new Date().getTime();
});
});
<script type="text/javascript">
#a
{
background: transparent url(bg2.jpg) no-repeat;
width: 600px;
height: 450px;
padding:0px;
}
#character {
background: transparent url(assassin.png) no-repeat;
height: 35px;
left: 36px;
top: 18px;
position: absolute;
width: 35px;
}
#character2 {
background: transparent url(assassin.png) no-repeat;
height: 35px;
left: 180px;
top: 18px;
position: absolute;
width: 35px;
}
#character .health {
background-color: #060;
border: 1px solid #060;
height: 2px;
margin: -7px auto 0 auto;
width: 25px;
}
#character .health div {
background-color: #6F0;
height: 2px;
width: 1px;
}
#character2 .health2 {
background-color: #060;
border: 1px solid #060;
height: 2px;
margin: -7px auto 0 auto;
width: 25px;
}
#character2 .health2 div {
background-color: #6F0;
height: 2px;
width: 1px;
}