Code below is meant to make a square move across the screen, It works in Netscape 4.* fine but in IE 5.5 the square speeds up really fast when don't want it to and see no reason for it speeding up, can someone help me????
<HTML>
<HEAD>
<TITLE>Dale's Game</TITLE>
<script language="javascript">
<!--
/*
** BROWSWER CHECK
*/
if (document.layers) {
var isNS = true;
var docString = "document.";
var styleString ="";
}
else if (document.all) {
var isIE = true;
var docString = "document.all.";
var styleString =".style";
}
/*
** VARIABLES
*/
var minPos = 20;
var maxPos = 400;
var location = minPos+2;
var isMoving = false;
var movingLeft = false;
var movingRight = false;
var nKey = 0;
var speed = 5; // smaller is faster
/*
** write style
*/
document.writeln('<style>');
document.writeln('#theSqr {position:absolute; left:' + location + '; top: 20; color:#23015D; width:20; height:20; clip:rect(0,20,20,0); background-color:#23015D; layer-background-color:#23015D;}');
document.writeln('</style>');
/*
** FUNCTION RETURNASDIV
*/
function returnAsDiv (theDivToConvert) {
var returnedObject = eval(docString + theDivToConvert + styleString);
return returnedObject;
}
document.onkeydown = keyDown
document.onkeyup = keyUp
if (isNS) document.captureEvents(Event.KEYDOWN | Event.KEYUP)
/*
** FUNCTION KEYDOWN
*/
function keyDown(e) {
if (isNS) {var nKey = e.which}
if (isIE) {var nKey = window.event.keyCode;}
//alert(nKey);
if (isIE && nKey == 90) {
movingLeft = true;
isMoving = true;
} else if (isIE && nKey == 88) {
movingRight = true;
isMoving = true;
} else if (isNS && nKey == 122) {
movingLeft = true;
isMoving = true;
} else if (isNS && nKey == 120) {
movingRight = true;
isMoving = true;
}
move();
}
/*
** FUNCTION KEYUP
*/
function keyUp() {
isMoving = false;
movingRight = false;
movingLeft = false;
}
function move() {
if ((movingLeft && location <= minPos) || (movingRight && location >= maxPos)) {
isMoving = false;
}
if (isMoving) {
theDiv = returnAsDiv('theSqr');
if(movingRight) ammount=2;
if(movingLeft) ammount=-2;
location = location + ammount;
theDiv.left = location;
setTimeout('move()', speed);
}
}
//-->
</script>
</HEAD>
<BODY>
<div ID="theSqr"></div>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Dale's Game</TITLE>
<script language="javascript">
<!--
/*
** BROWSWER CHECK
*/
if (document.layers) {
var isNS = true;
var docString = "document.";
var styleString ="";
}
else if (document.all) {
var isIE = true;
var docString = "document.all.";
var styleString =".style";
}
/*
** VARIABLES
*/
var minPos = 20;
var maxPos = 400;
var location = minPos+2;
var isMoving = false;
var movingLeft = false;
var movingRight = false;
var nKey = 0;
var speed = 5; // smaller is faster
/*
** write style
*/
document.writeln('<style>');
document.writeln('#theSqr {position:absolute; left:' + location + '; top: 20; color:#23015D; width:20; height:20; clip:rect(0,20,20,0); background-color:#23015D; layer-background-color:#23015D;}');
document.writeln('</style>');
/*
** FUNCTION RETURNASDIV
*/
function returnAsDiv (theDivToConvert) {
var returnedObject = eval(docString + theDivToConvert + styleString);
return returnedObject;
}
document.onkeydown = keyDown
document.onkeyup = keyUp
if (isNS) document.captureEvents(Event.KEYDOWN | Event.KEYUP)
/*
** FUNCTION KEYDOWN
*/
function keyDown(e) {
if (isNS) {var nKey = e.which}
if (isIE) {var nKey = window.event.keyCode;}
//alert(nKey);
if (isIE && nKey == 90) {
movingLeft = true;
isMoving = true;
} else if (isIE && nKey == 88) {
movingRight = true;
isMoving = true;
} else if (isNS && nKey == 122) {
movingLeft = true;
isMoving = true;
} else if (isNS && nKey == 120) {
movingRight = true;
isMoving = true;
}
move();
}
/*
** FUNCTION KEYUP
*/
function keyUp() {
isMoving = false;
movingRight = false;
movingLeft = false;
}
function move() {
if ((movingLeft && location <= minPos) || (movingRight && location >= maxPos)) {
isMoving = false;
}
if (isMoving) {
theDiv = returnAsDiv('theSqr');
if(movingRight) ammount=2;
if(movingLeft) ammount=-2;
location = location + ammount;
theDiv.left = location;
setTimeout('move()', speed);
}
}
//-->
</script>
</HEAD>
<BODY>
<div ID="theSqr"></div>
</BODY>
</HTML>
Comment