...

View Full Version : array /collision detection



gencoglu4
01-01-2008, 07:03 PM
Hi, There
I am tring to create a simple collision test..The problem is I can not figure outhow to use an ARRAY..
I mean.. my car is moving and it hits first car then it keeps moving and hits the second...and so on..it will stop till it hits all 5 cars..

I will be very happy if you can fix my following codes..

Thanks in advance.


<script>
document.bgColor="pink";
var a =setInterval('basla()',100)
var tar=new array();
function basla() {
tar1a = document.all.imX;

tar1a.style.posLeft += 5;
for (var i = 1; i <= 5; i++) {
var tar = document.getElementById("im" + i);
if ((tar1a.style.posLeft <= tar.style.posLeft + tar.style.posWidth &&
tar1a.style.posLeft >= tar.style.posLeft) &&
tar1a.style.posTop <= tar.style.posTop + tar.style.posHeight &&
tar1a.style.posTop >= tar.style.posTop) {
tar.style.visibility = "hidden";
clearInterval(a);
}
}
}
</script>
<bgsound src="#" id=ses loop=1 autostart="false">
<img src="kap.gif" name=imX style="position:absolute;top:100;left:10;"><br><br>
<img src="kizss.gif" name=im1 style="position:absolute;top:100;left:50;"><br><br>
<img src="kizss.gif" name=im2 style="position:absolute;top:100;left:100;"><br><br>
<img src="kizss.gif" name=im3 style="position:absolute;top:100;left:150;"><br><br>
<img src="kizss.gif" name=im4 style="position:absolute;top:100;left:200;"><br><br>
<img src="kizss.gif" name=im5 style="position:absolute;top:100;left:250;"><br><br>
<img src="kizss.gif" name=im6 style="position:absolute;top:100;left:300;"><br><br>

BubikolRamios
01-01-2008, 08:55 PM
The minutes invested in beautifying code posting here have emazing impact, learn from expiriences.

rnd me
01-01-2008, 09:28 PM
The minutes invested in beautifying code posting here have emazing impact, learn from expiriences.




function basla() {
tar1a = document.all.imX;
tar = document.getElementById("im" + i);
tar1a.style.posLeft += 5;
for (var i = 1; i <= 5; i++) {
if ((tar1a.style.posLeft <= tar[i].style.posLeft + tar[i].style.posWidth &&
tar1a.style.posLeft >= tar[i].style.posLeft) &&
tar1a.style.posTop <= tar[i].style.posTop + tar[i].style.posHeight &&
tar1a.style.posTop >= tar[i].style.posTop) {
tar[i].style.visibility = "hidden";
clearInterval(a);
}
}
}

gencoglu4
01-01-2008, 11:11 PM
still there is a problem...it only hits the first one..
any more help ..please .?

thanks

Arty Effem
01-02-2008, 12:37 AM
still there is a problem...it only hits the first one..
How can it be doing that?
This code can't even get started because it's riddled with errors that the console would indicate.

gencoglu4
01-02-2008, 04:55 PM
I corrected it but still need help....I also need to asign onkeydown() function to move the object so that I can pick all of the objects on the screen just as in "PACKMAN games"


<script>
document.bgColor="pink";
var a =setInterval('basla()',100)
var tar=new array();
function basla() {
tar1a = document.all.imX;

tar1a.style.posLeft += 5;
for (var i = 1; i <= 5; i++) {
var tar = document.getElementById("im" + i);
if ((tar1a.style.posLeft <= tar.style.posLeft + tar.style.posWidth &&
tar1a.style.posLeft >= tar.style.posLeft) &&
tar1a.style.posTop <= tar.style.posTop + tar.style.posHeight &&
tar1a.style.posTop >= tar.style.posTop) {
tar.style.visibility = "hidden";
clearInterval(a);
}
}
}
// this is the second function...need help combineing this with the one above..
function KeyDown() {
if (event.keyCode==39)
{

fin.style.posLeft=fin.style.posLeft+20;

}
if (event.keyCode==37)
{

fin.style.posLeft=fin.style.posLeft-20;

}

if (event.keyCode==40)
{

fin.style.posTop=fin.style.posTop+20;

}
if (event.keyCode==38)
{

fin.style.posTop=fin.style.posTop-20;

}
}
</script>

<img src="kap.gif" name=imX style="position:absolute;top:100;left:10;"><br><br>
<img src="kizss.gif" name=im1 style="position:absolute;top:100;left:50;"><br><br>
<img src="kizss.gif" name=im2 style="position:absolute;top:100;left:100;"><br><br>
<img src="kizss.gif" name=im3 style="position:absolute;top:100;left:150;"><br><br>
<img src="kizss.gif" name=im4 style="position:absolute;top:100;left:200;"><br><br>
<img src="kizss.gif" name=im5 style="position:absolute;top:100;left:250;"><br><br>
<img src="kizss.gif" name=im6 style="position:absolute;top:100;left:300;"><br><br>

Arty Effem
01-02-2008, 06:36 PM
I corrected it but still need help...


var tar=new array();
I don't think so - the console must have something to say about that, which should be new Array().

mjlorbet
01-18-2008, 11:59 AM
additionally, all the name=imX attributes in the html should be changed to id="imX" so that document.getElementById() actually has an ID to get



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum