PDA

View Full Version : controling moving objects


cpa3b
08-13-2002, 12:24 AM
Hi, I know how to get an image to move around the screen, but I wanted to control where the images went. I want to make a box and keep the moving objects in that box only, rather than have the objects move around the entire screen randomly. If you can help, I would greatly appreciate it. Thanks in advance!

joh6nn
08-13-2002, 08:59 AM
do you want the pictures to randomly move in the box? i think i might have something for you, but i'm not positive. can you explain what you want in more detail?

cpa3b
08-15-2002, 02:36 AM
That is exactly what I want. What I want to make is a banner for my fantasy hockey league. I want the logos of all of the individual teams to be floating around randomally within my logo- but only in my box. Thanks for your help!

joh6nn
08-15-2002, 03:10 AM
ok, give me a bit, i'm working on it. should have it for you in a while.

joh6nn
08-15-2002, 06:57 AM
ok, here's what i came up with. It's been tested in Mozilla 1.0, and IE6. i tried to build in backwards compatability, but i couldn't get that to work, so for right now in will only work in newer browsers.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
<script>
var pics = [['imag0',1,1],['imag1',-1,1],['imag2',1,-1],['imag3',-1,-1]];

function bounce(brray, xmax, ymax) {
var objekt, x, y, w, h;
for (var i in brray) {
objekt = document.getElementById(brray[i][0]);
x = parseInt(objekt.style.left);
y = parseInt(objekt.style.top);
w = parseInt(objekt.style.width);
h = parseInt(objekt.style.height);
if ( isNaN(x) ) { x = 0; }
if ( isNaN(y) ) { y = 0; }
if ( ( (x == (xmax - w) ) && ( brray[i][1] > 0 ) ) || ( (x == 0) && ( brray[i][1] < 0 ) ) ) { brray[i][1] *= -1; }
if ( ( (y == (ymax - h) ) && ( brray[i][2] > 0 ) ) || ( (y == 0) && ( brray[i][2] < 0 ) ) ) { brray[i][2] *= -1; }
objekt.style.left = (x + brray[i][1]) + "px";
objekt.style.top = (y + brray[i][2]) + "px";
}
}

window.onload = function() { setInterval("bounce(pics, 100, 100);", 50); }
</script>
</head>

<body>

<div style="position: absolute; width: 100px; height: 100px; background-color: #FF0000; left: 0px; top: 0px;">
<img id="imag0" src="somegif.gif" style="position: absolute; background-color: #00FF00; width: 10px; height: 10px; left: 35px; top: 65px;">
<img id="imag1" src="somegif.gif" style="position: absolute; background-color: #0000FF; width: 10px; height: 10px; left: 45px; top: 55px;">
<img id="imag2" src="somegif.gif" style="position: absolute; background-color: #FFFF00; width: 10px; height: 10px; left: 55px; top: 45px;">
<img id="imag3" src="somegif.gif" style="position: absolute; background-color: #FF00FF; width: 10px; height: 10px; left: 65px; top: 35px;">
</div>

</body>
</html>

notice the things in bold. those are the only things that you should need to change.

the first line in bold is a list of the different images that you want to move around. the red part is the image's id tag. the part in blue is the x-axis; -1 is left, 1 is right. the part in green is the y-axis; -1 is down, 1 is up. you can have as many images as you want, you just need to make an entry on this bold line for all of them, and you need to actually make the HTML for that image.

the next two bits of blue and green are the size of the box that you want things to move around in. for instance, if your logo is 468 x 60, then you change the blue to 468, and the green to 60.

the last bit is the number of milliseconds that it should wait between iterations. that is, how fast it should it be moving the boxes. bigger numbers are slower, smaller numbers are faster.

hope this is what you wanted.