Hello Science Buddies Forum.
I am currently working on my science fair project and decided that my experiment was going to be related to the bouncing ball animation experiment--
--Link: https://www.sciencebuddies.org/science- ... background
Now, I am already on the last step, which is trying to make the ball go up and down (the actual bouncing ball). My problem is that the red ball goes just down, never up and down, just down, and this is because i think my coding is wrong somewhere.
Here's my HTML layout:
<HTML>
________________________________________________________________________________________________________________________________________________
<HEAD><TITLE>ScienceFair</TITLE>
<!-- saved from C:\Users\ELECTROSHOP\Desktop\Science Fair Project Idea -->
<!-- When this code is saved as a local file, the preceding line tells Internet Explorer to treat this file according to the security rules for the Internet zone (plus any security rules specific for the Science Buddies website). -->
</HEAD>
<body>
<IMG ID="ball" STYLE="position: absolute; left: 200; top: 200; width: 10; height: 10;" SRC="ball1.gif">
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
var myBall = document.getElementById("ball");
var loc = 200;
setInterval ("myFunction()", 200);
var direction = 0;
function myFunction()
{
if(0 == direction)
{
/* move down */
loc += 10;
if(loc >= 500)
{
/* reached lower limit, change direction */
direction = 1;
}
}
else
{
/* move up */
loc -= 10;
if(loc < {
/* reached upper limit, change direction */
direction = 0;
}
}
myBall.style.top = loc;
}
// -->
</SCRIPT>
My Page
</BODY>
</HTML>
____________________________________________________________________________________________________________________________________
I repeat, the problem is that the red ball is just going down, not up and down.
Thanks for your assistance.
BouncingBall Coding problem, need urgent help.
Moderators: kgudger, bfinio, Moderators
-
- Posts: 2
- Joined: Thu Feb 28, 2013 6:40 pm
- Occupation: Student
- Project Question: Computer science, I need help with simple coding.
- Project Due Date: 03/10/2013
- Project Status: I am conducting my experiment
-
- Former Expert
- Posts: 65
- Joined: Tue Sep 27, 2011 4:56 pm
- Occupation: Student: 12th grade
- Project Question: Student volunteer.
- Project Due Date: N/a: see above.
- Project Status: Not applicable
Re: BouncingBall Coding problem, need urgent help.
Hello ScienceFairAddict,
I'm a little rusty on my HTML, but I'll try to help.
I read through your code quickly and noticed one typo-
Hope this helped, and good luck! If you have questions, feel free to ask.
I'm a little rusty on my HTML, but I'll try to help.
I read through your code quickly and noticed one typo-
It looks like you didn't actually specify an upper limit. If this was just a copying error, though, and you have an upper limit in your actual code, I can't see any other problems from a cursory read. In that case, I'd try putting some print statements in to see what functions and if statements the code is actually executing (I think these are supported in HTML) or switching the code around so that the ball goes up first. When in doubt, play around with your code a little!if(loc < {
/* reached upper limit, change direction */
direction = 0;
}
Hope this helped, and good luck! If you have questions, feel free to ask.
-Vysarge
~~~~~~~~~~~~~
Nature uses only the longest threads to weave her patterns, so that each small piece of her fabric reveals the organization of the entire tapestry.
-Richard Feynman
~~~~~~~~~~~~~
Nature uses only the longest threads to weave her patterns, so that each small piece of her fabric reveals the organization of the entire tapestry.
-Richard Feynman
-
- Posts: 2
- Joined: Thu Feb 28, 2013 6:40 pm
- Occupation: Student
- Project Question: Computer science, I need help with simple coding.
- Project Due Date: 03/10/2013
- Project Status: I am conducting my experiment
Re: BouncingBall Coding problem, need urgent help.
Thanks for your reply.
So, I think what you might have asked me to do, i changed the variable direction = 0 to variable direction = 1.
It still didn't do anything, the red ball is still not moving. Can you help me write the code, please. I really need your help.
So, I think what you might have asked me to do, i changed the variable direction = 0 to variable direction = 1.
It still didn't do anything, the red ball is still not moving. Can you help me write the code, please. I really need your help.
Re: BouncingBall Coding problem, need urgent help.
ScienceFairAddict,
vysarge's change of "if (loc <" to "if (loc < 0)" worked for me in Firefox. What browser are you using?
Heinz Hemken
vysarge's change of "if (loc <" to "if (loc < 0)" worked for me in Firefox. What browser are you using?
Heinz Hemken
Heinz Hemken
Mentor
Science Buddies Expert Forum
Mentor
Science Buddies Expert Forum
Re: BouncingBall Coding problem, need urgent help.
ScienceFairAddict,
Here is the corrected code:
All I did to your code was change to " if (loc < 0)" and reindented with html tidy (http://www.w3.org/People/Raggett/tidy/), which added the DOCTYPE heading, and a bit by hand.
Heinz Hemken
Here is the corrected code:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
<title>ScienceFair</title>
<!-- saved from C:\Users\ELECTROSHOP\Desktop\Science Fair Project Idea -->
<!-- When this code is saved as a local file, the preceding line tells Internet Explorer to treat this file according to the security rules for the Internet zone (plus any security rules specific for the Science Buddies website). -->
</head>
<body>
<img id="ball" style=
"position: absolute; left: 200; top: 200; width: 10; height: 10;"
src="ball1.gif" name="ball">
<script language="JavaScript1.2" type="text/javascript">
<!--
var myBall = document.getElementById("ball");
var loc = 200;
setInterval ("myFunction()", 30);
var direction = 0;
function myFunction() {
if(0 == direction) {
/* move down */
loc += 10;
if(loc >= 500) {
/* reached lower limit, change direction */
direction = 1;
}
}
else {
/* move up */
loc -= 10;
if (loc < 0) {
/* reached upper limit, change direction */
direction = 0;
}
}
myBall.style.top = loc;
}
// >
</script>
My Page
</body>
</html>
Heinz Hemken
Heinz Hemken
Mentor
Science Buddies Expert Forum
Mentor
Science Buddies Expert Forum