Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-22-2008, 08:49 AM   #1 (permalink)
Emancip8
Recruit
 
Join Date: Apr 2008
Posts: 1
Emancip8 is on a distinguished road
Question setTimeout not working, javascript

I'm recreating breakout using javascript and have run into a problem with the animation. I have been trying to use setTimeout for a function when i call the function it is only running once. I am clueless!
Any help with this would be greatly appreciated
Here's the script and markup:
Code:
<html>
            <head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Web Programming Assignment 2 - Javascript Game</title>
		<link rel = "stylesheet" type = "text/css" href = "style.css" \>
		<script>
			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('ball');
				direction = "north";
			}
			
			function object(element)
			{
				this.style = document.getElementById(element).style;
				this.move = move;
			}
			
			function move(object)
			{
				var me = this;

				if (direction == "north") 
				{
					position = parseInt(object.style.top);
					if (position > 0) 
					{
						position--;
						object.style.top = position;
						setTimeout(me.move, 0);
					}
					else 
					{
						direction = "south";
					}
				}
				if (direction == "south") 
				{
					position = parseInt(object.style.top);
					if (position < 287) 
					{
						position++;
						object.style.top = position;
						setTimeout(me.move, 0);
					}
					else 
					{
						direction = "north";
						collide();
					}
				}
			}
			
			function collide()
			{
				if (direction == "north")
				{
					move();
				}
			}
		</script>	
	</head>
	<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: 205px; right: 210; 
					top: 287px; bottom: 292;"></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(ball)">
					<br>
					<input type = "button" value = "Stop" id = "buttonStop">
					<br>
					<input type = "button" value = "End Game" id = "buttonEnd">
					<br>
				</div>
			</div>
		</form>	
	</body>
</html>
Emancip8 is offline   Reply With Quote
Old 05-19-2008, 01:15 PM   #2 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 227
falsepride is on a distinguished road
i recently had the same problem you are. you can't use 'this.function()' in setTimer. when set timer goes off, it calls the function outside the scope of the this keyword. you must use a specific object in setTimer(i think youll want it to be setTimeout(ball.move(), 0); ). by the way when trying to run the script in IE, it pointed out a few other javascript errors. you might want to thorough check your script.
falsepride is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Submit Form Options? Redline HTML, XML, Javascript, AJAX 5 05-19-2006 08:59 AM
Javascript is *not* Java Belisarius Java 0 05-27-2005 07:45 PM
Rotating .swf files at random with JavaScript? VisionsIC HTML, XML, Javascript, AJAX 0 05-18-2005 12:12 PM
javascript alert without refreshing? sde HTML, XML, Javascript, AJAX 2 06-30-2004 12:52 PM


All times are GMT -8. The time now is 01:42 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting