|
-
Oct 6th, 2004, 11:02 AM
#1
Thread Starter
Fanatic Member
javascript collision
hi,
this is another example of "collision detection" but I get error messages " I could not make it work!
Coul you please simpilfy/fix it for me..Thanks
--------------------------
<html>
<head>
<title>Collision Objects</title>
<script type="text/javascript">
var loaded=false;
var leftKey=37,upKey=38,rightKey=39,downKey=40;
function absCoords(node) {
var refNode = node.parentNode;
var coords = {x: parseInt(node.offsetLeft), y: parseInt(node.offsetTop)};
while (typeof refNode.offsetLeft != 'undefined') {
coords.x += parseInt(refNode.offsetLeft);
coords.y += parseInt(refNode.offsetTop);
refNode = refNode.parentNode;
}
return coords;
}
function collision(node1,node2) {
var thisOffset = absCoords(node1);
var nodeOffset = absCoords(node2);
return (thisOffset.x >= nodeOffset.x && thisOffset.x <= nodeOffset.x + parseInt(node2.offsetWidth) && thisOffset.y >= nodeOffset.y && thisOffset.y <= nodeOffset.y + parseInt(node2.offsetHeight))
} window.onload=new Function("loaded=true");
document.onkeydown=movePos;
function movePos(evt) {
if(loaded) {
var node=document.getElementById('c1');
var nodePos=absCoords(node);
var e=evt||event;
if(e.which==leftKey||e.keyCode==leftKey) node.style.left=nodePos.x-5;
else if(e.which==upKey||e.keyCode==upKey) node.style.top=nodePos.y-5;
else if(e.which==rightKey||e.keyCode==rightKey) node.style.left=nodePos.x+5;
else if(e.which==downKey||e.keyCode==downKey) node.style.top=nodePos.y+5;
alert(collide(node,document.getElementById('c2')));
}
}
</script>
</head>
<body>
<div id="scrollarea" style="position:absolute;left:300px;top:200px;width:200px ;height:200px;color:white;background:black;">
<div id="c1" style="position:absolute;left:100px;top:100px;background:red;">you</div>
<div id="c2" style="position:absolute;left:100px;top:150px;z-index:1;background:blue;">enemy</div>
</div>
</body>
</html>
-
Oct 6th, 2004, 12:09 PM
#2
Frenzied Member
Please do not make new threads on the same topic. Post everything in the same thread. If no one knows the answer, no one will answer. That's how it works.
-
Oct 6th, 2004, 12:34 PM
#3
Frenzied Member
I could probably figure it out, I just can't be bothered. Use flash, it has a built it function for this.
Maybe if I happen to read this while not in a grumpy mood, maybe then.
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|