newbie: animation problem

G

Guest

Trying to learn javascript, first attempt at a very simple game. In this
code it should result in some bouncing text. IE reports javascript error and
nothing moves: line 1, char 1, object expected. If changing the line:
gameLoop = setTimeout("go()",100); to alert("bla"); go(); the animation
works so i guess it has to do something with the setTimeout-call. Anyone an
idea what is wrong here?

Thnx, MrB

<html>
<head>
<title></title>
<SCRIPT LANGUAGE="javascript">
<!--

var inGame;
var gameLoop;
var posX;
var posY;
var rX;
var rY;
var speed;

function init()
{
inGame = true;
//gameLoop = null;
posX = 105;
posY = 100;
rX = 1;
rY = 1;
speed = 10 ;

// document.image_ball = new Image();
// document.image_ball.src ="images\ball.gif";
document.write ("<div id='ball' style='position:absolute'>hi</div>");
// <img name='ballimg' src='ball.gif'>
go();
}

function go()
{
if (inGame)
{
checkBorder();
ball.style.posLeft = posX;
ball.style.posTop = posY;
gameLoop = setTimeout("go()",100);
}
}

function checkBorder()
{
var calcX = posX + rX * speed;
var calcY = posY + rY * speed;
if ( calcX > 200 || calcX < 0) {rX *= -1;}
if ( calcY > 200 || calcY < 0) {rY *= -1;}
posX = posX + rX * speed;
posY = posY + rY * speed;
}

//-->
</SCRIPT>
</head>

<body onLoad="init()">

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top