Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-03-2004, 09:29 PM   PM User | #1
gaBRiel
New Coder

 
Join Date: Sep 2004
Location: Brazil
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
gaBRiel is an unknown quantity at this point
a loop that only runs while mouse is down?

Hi! Well, I´m 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 I´m 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 don´t 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!
gaBRiel is offline   Reply With Quote
Old 11-04-2004, 09:40 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 11-04-2004, 02:03 PM   PM User | #3
gaBRiel
New Coder

 
Join Date: Sep 2004
Location: Brazil
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
gaBRiel is an unknown quantity at this point
hey thanks! Im going to try that.
gaBRiel is offline   Reply With Quote
Old 11-04-2004, 04:18 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
will do it for sure!
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:16 AM.


Advertisement
Log in to turn off these ads.