Results 1 to 3 of 3

Thread: javascript collision

  1. #1

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    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>

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    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.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    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
  •  



Click Here to Expand Forum to Full Width