Skip to main content
gamedev.net
🔒 Locked

Javascript jump code doesn't work o

Started by Fl4sh 8 replies 1.2k views
Original Post
Fl4sh
Fl4sh
Novice at games...why doesn't this jump code work? It's in javascript.

function jump(){	x = document.getElementById("char");	var cutoff = 100;	var bottom = x.style.bottom;		for (x = 0; x <= cutoff; x += 5)	{		x.style.bottom = x + "px";		document.frm.txt3.value = x.style.bottom;	}		for (y = cutoff; y <= bottom; y -= 5)	{		x.style.bottom = y + "px";		document.frm.txt3.value = x.style.bottom;	}	}


[Edited by - Fl4sh on November 8, 2010 9:59:42 AM]
They hated on Jeezus, so you think I give a f***?!
Fl4sh
Fl4sh
In my mind it's supposed to move the div up in increments of 5 til it gets to 100, then move the div back down to where it was. D:

They hated on Jeezus, so you think I give a f***?!
sbn
sbn
In the first loop you're overwriting the value of x. Change it to this and try again:
function jump(){	xChar = document.getElementById("char");	var cutoff = 100;	var bottom = xChar .style.bottom;		for (x = 0; x <= cutoff; x += 5)	{		xChar.style.bottom = x;		document.frm.txt3.value = xChar.style.bottom;	}		for (y = cutoff; y <= bottom; y -= 5)	{		xChar.style.bottom = y;		document.frm.txt3.value = xChar.style.bottom;	}	}

Fl4sh
Fl4sh
Wierd it just sets the bottom position to 0 and doesn't do anything else. ;o

It's because xChar.style.bottom is empty for some reason.

	xChar = document.getElementById("char");	var cutoff = 1000;	var bottom = xChar .style.bottom;	alert(bottom);		for (x = 0; x <= cutoff; x += 5)	{		xChar.style.bottom = x + "px";				document.frm.txt3.value = xChar.style.bottom;	}		for (y = cutoff; y >= bottom; y -= 5)	{		xChar.style.bottom = y + "px";		document.frm.txt3.value = xChar.style.bottom;	}
They hated on Jeezus, so you think I give a f***?!
Captain P
Captain P
Looks like a typical mistake: the content of your loops isn't spread across time, so to say. Your code does exactly what you tell it - it loops until the object is where it should be. But you don't want it to happen immediately - you'll want to wait a little time between each position update (while making sure the screen is redrawn after every little move - however that is done in javascript).<br><br>In pseudo-code:<br><pre>main game loop<br> while(not yet there)<br> move<br> // moved to destination seemingly immediately<br> draw</pre><br>versus:<br><pre>main game loop<br> if(not yet there)<br> move<br> // moved a little closer to destination<br> draw</pre>
Fl4sh
Fl4sh
Yeah thats what I was thinking. The loop goes way too fast for the movement to be visible. ;o
They hated on Jeezus, so you think I give a f***?!
Zahlman
Zahlman
Quote:
Original post by Fl4sh
Yeah thats what I was thinking. The loop goes way too fast for the movement to be visible. ;o


Not just that, but you don't allow for anything else to happen while the loop is going on.

To make this kind of thing work you really need to learn about timing, in general. (Learning about physics would be a good idea, too, if you want things to look right.) :)
Anntor
Anntor
Quote:
Original post by Fl4sh
Wierd ... xChar.style.bottom is empty for some reason...



There's a reason behind that. The style object in javascript is a reference to the inline style property of an HTML element. If you set your elements style with separate css blocks, than you can't access that through javascripts style object.<br><br>separate css style applied to that element (you can't use javascript style object to access this):<br><!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre><br><style type=<span class="cpp-literal">"text/css"</span>><br>div {bottom: 33px;}<br></style><br><br><div id=<span class="cpp-literal">"char"</span>></div><br><br></pre></div><!--ENDSCRIPT--><br>inline style attribute (here you can use the style object in javascript):<br><!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre><br><div id=<span class="cpp-literal">"char"</span> style=<span class="cpp-literal">"bottom: 33px;"</span>></div><br><br></pre></div><!--ENDSCRIPT--><br><br>Different browsers offer different ways to retrieve the computed, final style of an element after all CSS rules got applied to it. But these methods return different results between different browsers. So they are not the best choice. (But sometimes the only one depending on the applications requirements)<br><br>There is only one solution, which works cross browser: Properties of the style attribute you want to change dynamicly should be set with javascript before using them from within javascript. That way the element get its own inline style attribute assigned.<br><br>example:<br><!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre><br><script type=<span class="cpp-literal">"text/javascript"</span>><br>xChar.style.bottom = '33px'; <span class="cpp-comment">// assign it for the first time</span><br><br>{<br> bottom = parseInt(xChar.style.bottom, <span class="cpp-number">10</span>); <span class="cpp-comment">// here you can access it</span><br>}<br></script><br><br><div id=<span class="cpp-literal">"char"</span>></div><br><br></pre></div><!--ENDSCRIPT-->
Fl4sh
Fl4sh
Quote:
Original post by Anntor
Quote:
Original post by Fl4sh
Wierd ... xChar.style.bottom is empty for some reason...



There's a reason behind that. The style object in javascript is a reference to the inline style property of an HTML element. If you set your elements style with separate css blocks, than you can't access that through javascripts style object.<br><br>separate css style applied to that element (you can't use javascript style object to access this):<br>*** Source Snippet Removed ***<br>inline style attribute (here you can use the style object in javascript):<br>*** Source Snippet Removed ***<br><br>Different browsers offer different ways to retrieve the computed, final style of an element after all CSS rules got applied to it. But these methods return different results between different browsers. So they are not the best choice. (But sometimes the only one depending on the applications requirements)<br><br>There is only one solution, which works cross browser: Properties of the style attribute you want to change dynamicly should be set with javascript before using them from within javascript. That way the element get its own inline style attribute assigned.<br><br>example:<br>*** Source Snippet Removed ***<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>Wow thx alot. I've read several javascript books and have never heard that before. :p It works...not like I want but it works.
They hated on Jeezus, so you think I give a f***?!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.