Flash Games
 FAQ   Search   Memberlist   Usergroups   Register  Profile   Log in to check your private messages   Log in 


setTimeOut isn't working properly, please help



 

Post new topic   Reply to topic  
   DEVPPL Forum Index -> JavaScript Forum
View previous topic :: View next topic  
Author Message
liber8rhyme@live.co.uk



Joined: 22 Apr 2008
Posts: 3

PostPosted: Tue Apr 22, 2008 11:03 am    Post subject: setTimeOut isn't working properly, please help Reply with quote

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
View user's profile Send private message
rangana
500+ Club


Joined: 27 Feb 2008
Posts: 627
Location: Cebu City Philippines

PostPosted: Tue Apr 22, 2008 11:14 am    Post subject: Re: setTimeOut isn't working properly, please help Reply with quote

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 Wink

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) Smile
Back to top
View user's profile Send private message Yahoo Messenger
liber8rhyme@live.co.uk



Joined: 22 Apr 2008
Posts: 3

PostPosted: Tue Apr 22, 2008 11:18 am    Post subject: Re: setTimeOut isn't working properly, please help Reply with quote

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
View user's profile Send private message
liber8rhyme@live.co.uk



Joined: 22 Apr 2008
Posts: 3

PostPosted: Tue Apr 22, 2008 11:21 am    Post subject: Re: setTimeOut isn't working properly, please help Reply with quote

I also have an external stylesheet for the other elements if that is of any use
Back to top
View user's profile Send private message
rangana
500+ Club


Joined: 27 Feb 2008
Posts: 627
Location: Cebu City Philippines

PostPosted: Wed Apr 23, 2008 1:29 am    Post subject: Re: setTimeOut isn't working properly, please help Reply with quote

I'm confused (nothing new) Smile

...What are you trying to achieve..could you explain a little bit further..we'll see how can we help you out with this Wink

...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 Smile
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    DEVPPL Forum Index -> JavaScript Forum All times are GMT + 1 Hour
Page 1 of 1

 
 
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User: Pass:
Log me on automatically each visit:

 


Powered by phpBB © 2001, 2005 phpBB Group - Modified by DEVPPL

Flash Games - Sitemap