| View previous topic :: View next topic |
| Author |
Message |
liber8rhyme@live.co.uk
Joined: 22 Apr 2008 Posts: 3
|
Posted: Tue Apr 22, 2008 11:03 am Post subject: setTimeOut isn't working properly, please help |
|
|
I am recreating a breakout game for a university project and am having a little trouble.
I have the following code:
| Code: |
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. |
|
| Back to top |
|
 |
|
|
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 545 Location: Cebu City Philippines
|
Posted: Tue Apr 22, 2008 11:14 am Post subject: Re: setTimeOut isn't working properly, please help |
|
|
See this part:
| Code: |
var style = document.getElementById('ball').style;
|
Try changing it to:
| Code: |
var style = document.getElementById('ball');
|
...Then try it use the style variable together with your style.top
| Code: |
style.style.top = position;
|
...Im not certain if this will aide, but give it a try...also if nothing works, could you please include your markups (HTML)  |
|
| Back to top |
|
 |
liber8rhyme@live.co.uk
Joined: 22 Apr 2008 Posts: 3
|
Posted: Tue Apr 22, 2008 11:18 am Post subject: Re: setTimeOut isn't working properly, please help |
|
|
Thanks for your prompt reply Rangana.
Unfortunately that didn't work, here's the HTML:
| Code: |
<body onload = "initialize()">
<hr/>
<h1>Javascript Game</h1>
<hr/>
<a href = "#">Home</a>
<p/>
<form>
<div id = "gameBoard">
<div id = "boundary">
<div id = "ball"style = "left: 202.5px; right: 207.5;
top: 287.5px; bottom: 292.5;"></div>
<div id = "paddle"></div>
</div>
<div id = "gameControl">
<div id = gameTitle>Breakout Game</div>
<div id = "lives"></div>
<div id = "score"></div>
<input type = "button" value = "Start" id = "buttonStart" onclick = "ball.move()">
<br>
<input type = "button" value = "Stop" id = "buttonStop">
<br>
<input type = "button" value = "End Game" id = "buttonEnd">
<br>
</div>
</div>
</form>
</body>
</html> |
|
|
| Back to top |
|
 |
liber8rhyme@live.co.uk
Joined: 22 Apr 2008 Posts: 3
|
Posted: Tue Apr 22, 2008 11:21 am Post subject: Re: setTimeOut isn't working properly, please help |
|
|
| I also have an external stylesheet for the other elements if that is of any use |
|
| Back to top |
|
 |
rangana 500+ Club

Joined: 27 Feb 2008 Posts: 545 Location: Cebu City Philippines
|
Posted: Wed Apr 23, 2008 1:29 am Post subject: Re: setTimeOut isn't working properly, please help |
|
|
I'm confused (nothing new)
...What are you trying to achieve..could you explain a little bit further..we'll see how can we help you out with this
...firebug says your move function is not defined...which is true...and i'm unable to conceptualize how this one should work..since i'm completely lost  |
|
| Back to top |
|
 |
|