I have the following code:
- Code: Select all
var lives = 2;
var score = 0;
var ball;
var position;
var direction;
function initialize()
{
document.getElementById('lives').innerHTML = "Lives: " + lives;
document.getElementById('score').innerHTML = "Score: " + score;
ball = new object();
direction = "north";
}
function object()
{
this.move = move;
function move()
{
var style = document.getElementById('ball').style;
if (direction == "north")
{
position = parseInt(style.top);
if (position > 0)
{
position = position - 2;
setTimeout('move()', 10);
style.top = position;
}
else
{
direction = "south"
collide();
}
}
if (direction == "south")
{
position = parseInt(style.top);
if (position < 287)
{
position = position + 2;
setTimeout('move()', 10);
style.top = position;
}
else
{
direction = "north";
collide();
}
}
}
}
function collide()
{
if (direction == "north")
{
bounce("north");
}
}
function bounce(dir)
{
if (dir == "north")
{
direction = "north";
move();
}
}
It seems as if the setTimeOut call isn't working as each time i call 'ball.move()' the object only moves 2px. I know the move function works as i have tried using:
style = document.getElementById('ball').style
However, i have been told that i need to represent ball as an object with methods.
Any help with this would be greatly appreciated as i am near the end of my tether.



