We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

I am not understanding this

I am not understanding this at all. I have to have 3 fish swim across the screen in different direction. But I can only get the one fish to swim. I have been looking ont he web and can not find anything that helps me. If someone can help me a web site or something thank you. I am ready to pull my hair out. For some reason I think it is something that is very small.But here is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Fish tank</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
// <![CDATA[
var fishPos = 0;
horizontal = new Array(50);
var fillPosition = 10;
for(var i = 0; i < 50; ++i) {
        horizontal[i] = fillPosition;
        fillPosition += 10;
}
function fishSwim(fishNumber) {
document.getElementById("fish"+fishNumber).style.left = horizontal[fishPos[fishNumber] + "px";
++fishPos[fishNumber];
if (fishPos[fishNumber] == 49)
        fishPos[fishNumber] = 0;
}

function startSwimming() {
        setInterval(fish1Swim, 100);
}
// ]]>
</script>
</head>
<body onload="startSwimming();">
<p><span id="fish1" style=
"position:absolute; left:10px; top:10px"><img src="fish1.gif" alt="Image of a fish" /></span></p>
<p><span id="fish2" style=
"position:absolute; left:10px; top:120px"><img src="fish3.gif" alt="Image of a fish" /></span></p>
<p><span id="fish3" style=
"position:absolute; left:10px; top:250px"><img src="fish2.gif" alt="Image of a fish" /></span></p>
</body>
</html>
3
Contributors
3
Replies
1 Year
Discussion Span
8 Months Ago
Last Updated
4
Views
Question
Answered
sha1023012
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

A. at the end of line 17 you have horizontal[fishPos[fishNumber] . That's a syntax error. You are missing a closing bracket after fishNumber .

B. On line 24 you have setInterval(fish1Swim, 100); , but there is no such thing as function fish1Swin(){...} declared anywhere, so I don't see how you can claim that you can make one fish swim (especially after the syntax error outlined on point A.). What you do have is function swimFish(){...} which expects a number, so what you need to do is call it once foreach of your fishes:

function startSwimming() {
        setInterval("fishSwim(1)", 100);
        setInterval("fishSwim(2)", 100);
        setInterval("fishSwim(3)", 100);
}

c. fishPos is declared as number on line 9, but you are using it as an array in lines 17-20. Also, based on lines 18-20, it seems like you are "depending" on something like fishPos[1] for fish1, fishPos[2] for fish2, etc. So line 9 should be changed to an array where indices 1,2, and 3 are required to be initialized:

//the -1 is irrelevant. It's indices 1-3 that you need for fishPos[1]-fishPos[3].
var fishPos = [-1,0,0,0];

If you implement the changes above, you should see three fishes swimming in unison across the street. I'll leave it up to you to make them swim in different directions/rates.

hielo
Veteran Poster
1,131 posts since Dec 2007
Reputation Points: 116
Solved Threads: 247
Skill Endorsements: 0

thank you i figure it out

sha1023012
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by hielo

hi guys, i am owner of a tropical fish and reptile store, im dont have the knowledge to create scripts.
is it possible to have fish swiming arround the homepage of my store website?
f possible does anyone has a script?

aquariums
Newbie Poster
1 post since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0700 seconds using 2.77MB