No, it should be new Array(5) if you are going to give it a size. The argument there is the NUMBER of elements, *NOT* the element number of the last element.
0,1,2,3,4 is 5 elements, so Array(5).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>BUZZ</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
// control speed, etc., with these. Treated as constants:
var INTERVAL = 20;
var MAXSPEED = 10;
var MAXDURATION = 200;
// The maximal screen positions allowed (0,0 implied as minimal):
var xmax = 0;
var ymax = 0;
// constructor for one "Bug" object:
function Bug( obj, x,y,dx,dy,t )
{
this.object = obj;
this.xpos = x;
this.ypos = y;
this.xspeed = dx;
this.yspeed = dy;
this.duration = t;
}
// the master array controlling everything:
var Bugs = [];
// if the page is resized, reset the limits
function setLimits( )
{
xmax = document.documentElement.clientWidth - 48;
ymax = document.documentElement.clientHeight - 48;
}
// called when the page is loaded...sets up all the bugs
function startBugs( )
{
setLimits(); // on startup, same as a resize
// handles any number of bugs, up to 9999
for ( var bugnum = 0; bugnum < 9999; ++bugnum )
{
// find one
var bugimg = document.getElementById("bug"+bugnum);
if ( bugimg == null ) break; // but stop when no more to be found
// put this bug, with its initial info, into the array:
Bugs[bugnum] =
new Bug( bugimg,
// random starting position
Math.floor(Math.random() * xmax),
Math.floor(Math.random() * ymax),
// and random starting speed
Math.floor(Math.random() * 2 * MAXSPEED) - MAXSPEED,
Math.floor(Math.random() * 2 * MAXSPEED) - MAXSPEED,
// and random duration
Math.floor(Math.random() * MAXDURATION)
);
}
// the "tick" time
setInterval( moveBugs, INTERVAL );
// and the first movement...which actually moves bugs to starting positions
moveBugs();
}
// called each "tick"
function moveBugs()
{
// process all moving objects:
for ( var bugnum = 0; bugnum < Bugs.length; ++bugnum )
{
var bug = Bugs[bugnum];
// find new position
var x = bug.xpos + bug.xspeed;
var y = bug.ypos + bug.yspeed;
// don't allow position to be out of bounds
if ( x < 0 ) x = 0;
else if ( x > xmax ) x = xmax;
if ( y < 0 ) y = 0;
else if ( y > ymax ) y = ymax;
// the key to the whole thing...
// if the object is *at* any boundary *OR* if duration has expired for this movement...
if ( x==0 || x==xmax || y==0 || y==ymax || --bug.duration <= 0 )
{
// then get new random speed (both x and y) and duration
bug.xspeed = Math.floor(Math.random() * 2 * MAXSPEED) - MAXSPEED;
bug.yspeed = Math.floor(Math.random() * 2 * MAXSPEED) - MAXSPEED;
bug.duration = Math.floor(Math.random() * MAXDURATION);
}
// finally, store the position back in array...
bug.xpos = x; bug.ypos = y;
// and move the object
bug.object.style.left = x + "px";
bug.object.style.top = y + "px";
}
}
</script>
<style type="text/css">
img.bug {
position: absolute;
z-index: 99;
height: 48px;
width: 48px;
}
</style>
</head>
<body onload="startBugs()" onresize="setLimits()">
<img class="bug" id="bug0" src="bug.jpg" alt="buggy0" />
<img class="bug" id="bug1" src="bug.jpg" alt="buggy1" />
<img class="bug" id="bug2" src="bug.jpg" alt="buggy2" />
<img class="bug" id="bug3" src="bug.jpg" alt="buggy3" />
<img class="bug" id="bug4" src="bug.jpg" alt="buggy4" />
</body>
</html>
Instead of having multiple parallel arrays of individual values, let's use ONE array of custom objects. Much cleaner.
Hello,
I would like do have the same code but without direction and speed variation.
And then give each image a different starting position and direction.
I would like do have the same code but without direction and speed variation.
And then give each image a different starting position and direction.
You mean you want to SPECIFY the staring positions and directions yourself, rather than randomly picking them?
Okay. HOW do you want to specify them? And what directions will you specify? Just up, down, left, right? Or 45 degree diagonals? Or some finer control than that?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I want each image to follow a different path.
It should look harmonious.
It can start from anywhere and in any direction.
but not aligned, like if it was random
Or the change direction can happen when they hit the corner of the screen.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>BUZZ</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
// control speed, etc., with these. Treated as constants:
var INTERVAL = 20;
var MAXSPEED = 10;
var MAXDURATION = 200;
// The maximal screen positions allowed (0,0 implied as minimal):
var xmax = 0;
var ymax = 0;
// constructor for one "Bug" object:
function Bug( obj, x,y,dx,dy,t )
{
this.object = obj;
this.xpos = x;
this.ypos = y;
this.xspeed = dx;
this.yspeed = dy;
this.duration = t;
}
// the master array controlling everything:
var Bugs = [];
// if the page is resized, reset the limits
function setLimits( )
{
xmax = document.documentElement.clientWidth - 48;
ymax = document.documentElement.clientHeight - 48;
}
// called when the page is loaded...sets up all the bugs
function startBugs( )
{
setLimits(); // on startup, same as a resize
// handles any number of bugs, up to 9999
for ( var bugnum = 0; bugnum < 9999; ++bugnum )
{
// find one
var bugimg = document.getElementById("bug"+bugnum);
if ( bugimg == null ) break; // but stop when no more to be found
// decipher the ALT= as xpos, ypos, xmovement, ymovement
var info = bugimg.alt.split(",");
// put this bug, with its initial info, into the array:
Bugs[bugnum] =
new Bug( bugimg,
// x and y start:
Number(info[0]),
Number(info[1]),
// and random starting speed
Number(info[2]),
Number(info[3]),
// and random duration
Math.floor(Math.random() * MAXDURATION)
);
}
// the "tick" time
setInterval( moveBugs, INTERVAL );
// and the first movement...which actually moves bugs to starting positions
moveBugs();
}
// called each "tick"
function moveBugs()
{
// process all moving objects:
for ( var bugnum = 0; bugnum < Bugs.length; ++bugnum )
{
var bug = Bugs[bugnum];
// find new position
var x = bug.xpos + bug.xspeed;
var y = bug.ypos + bug.yspeed;
// don't allow position to be out of bounds
if ( x < 0 ) x = 0;
else if ( x > xmax ) x = xmax;
if ( y < 0 ) y = 0;
else if ( y > ymax ) y = ymax;
// the key to the whole thing...
// if the object is *at* any boundary *OR* if duration has expired for this movement...
if ( x==0 || x==xmax || y==0 || y==ymax )
{
bug.xspeed = - bug.xspeed;
bug.yspeed = - bug.yspeed;
}
// finally, store the position back in array...
bug.xpos = x; bug.ypos = y;
// and move the object
bug.object.style.left = x + "px";
bug.object.style.top = y + "px";
}
}
</script>
<style type="text/css">
img.bug {
position: absolute;
z-index: 99;
height: 48px;
width: 48px;
}
</style>
</head>
<body onload="startBugs()" onresize="setLimits()">
<img class="bug" id="bug0" src="logo.jpg" alt="100,100,10,0" />
<img class="bug" id="bug1" src="logo.jpg" alt="800,800,0,-10" />
<img class="bug" id="bug2" src="logo.jpg" alt="400,400,3,3" />
<img class="bug" id="bug3" src="logo.jpg" alt="400,400,-5,-10" />
<img class="bug" id="bug4" src="logo.jpg" alt="1200,300,-20,0" />
</body>
</html>
There are some left-over extraneous variables in there now. Just ignore them.
As you can probably see, this uses the ALT= to tell the program what to do. The coding means
ALT="xstart,ystart,xspeed,yspeed"
So you can control where it starts, what direction it moves in, and how fast.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.