It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

setTimeOut isn't working properly, please help

setTimeOut isn't working properly, please help

Postby liber8rhyme@live.co.uk on Tue Apr 22, 2008 10:03 am

I am recreating a breakout game for a university project and am having a little trouble.
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.
liber8rhyme@live.co.uk
 
Posts: 3
Joined: Tue Apr 22, 2008 9:50 am

Postby rangana on Tue Apr 22, 2008 10:14 am

See this part:
Code: Select all
var style = document.getElementById('ball').style;


Try changing it to:
Code: Select all
var style = document.getElementById('ball');


...Then try it use the style variable together with your style.top ;)

Code: Select all
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) :)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby liber8rhyme@live.co.uk on Tue Apr 22, 2008 10:18 am

Thanks for your prompt reply Rangana.
Unfortunately that didn't work, here's the HTML:
Code: Select all
<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>
liber8rhyme@live.co.uk
 
Posts: 3
Joined: Tue Apr 22, 2008 9:50 am

Postby liber8rhyme@live.co.uk on Tue Apr 22, 2008 10:21 am

I also have an external stylesheet for the other elements if that is of any use
liber8rhyme@live.co.uk
 
Posts: 3
Joined: Tue Apr 22, 2008 9:50 am

Postby rangana on Wed Apr 23, 2008 12:29 am

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 :)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 4 guests