Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2004
    Location
    Brazil
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    a loop that only runs while mouse is down?

    Hi! Well, Im doing a simple game using javascript in which you shoot a sling shot. One of the things I plan on doing is that when you hold your left mouse button down, it is as if you were puling the ruber band on the sling shot; so, the longer you hold the mouse button down, the farther you are pulling the band. What Im trying to do is make a loop of some sort that will keep track of the time you hold your mouse button down, and when you release the mouse button, the loop will stop.
    this is what I have for now:

    ...
    <script>
    gg=0
    function pull(nn,xx){ //dont worry with the 'xx'... its for later purposes
    dontmove=nn
    if(nn==1){ //if nn = 1, the bullet moves
    hhh=document.getElementById("bullet") //the ("bullet") is a <div> tag containing the image for the bullet
    hhh.style.top=gg
    gg++
    setTimeout("pull(1,xx)",500) //this is supposed to do the loop
    }
    }
    </script>
    ...
    <body onmousedown="pull(1,event.clientX)" onmouseup="pull(0,event.clientX)"> //again... dont worry with the 'clientX'...
    ...

    what happens is that the "bullet" does move, but only if I keep pressing the mouse... I want it to move alone until the mouse is released. Actually I dont want the bullet to move yet, but if you show me the correct way to make the loop, I will know how to deal with the rest...

    Thanks!

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The first argument of setTimeout(), the function called, has to be a string. Thus, if you wanna use passed parameters, you must consider something like this:

    setTimeout("pull(1,"+xx+")",500)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Sep 2004
    Location
    Brazil
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hey thanks! Im going to try that.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    will do it for sure!
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •