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"
<html xmlns="http://www.w3.org/1999/xhtml">
<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";
if (fishPos[fishNumber] == 49)
        fishPos[fishNumber] = 0;

function startSwimming() {
        setInterval(fish1Swim, 100);
// ]]>
<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>

Recommended Answers

All 3 Replies

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.

thank you i figure it out

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.