Help with delay code

About advanced scenes, and the Thyme scripting language used in Algodoo.

Help with delay code

Postby pfjeka » Thu Feb 21, 2013 1:10 pm

Hi, I'm making a simple thyme gun for a main tank cannon. A box, when hit by a laser, spawns a circle with a given speed. To shoot i should activate the laser with a key.
Problem: if I hold down the laser activation key, there will be a lot of objects spawned. I need to spawn only one object every X seconds, to have a slow, controlled rate of fire. I'm sure someone has already solved this problem in the past.
How can I implement this function in the laser, o in the spawning box? The spawning code is actually in the box.
I read different things about delays, timers and other things, but I can not do anything that works. I know I could make a mechanical delayer (like a circle with slow speed motor), but I prefer to do a script, to have less objects in the scene.
Thank you.
pfjeka
 
Posts: 9
Joined: Mon Feb 11, 2013 8:10 pm

Re: Help with delay code

Postby Kilinich » Thu Feb 21, 2013 3:55 pm

For laser:

Code: Select all
_lastShot = 0
_delay = 5
onLaserHit = (e) => {
  (sim.time > e.laser._lastShot + e.laser._delay) || (e.laser._lastShot > sim.time) ? {
    //do shot
    e.laser._lastShot = sim.time
  } : {}
}
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Help with delay code

Postby pfjeka » Thu Feb 21, 2013 5:11 pm

Thank you man!
pfjeka
 
Posts: 9
Joined: Mon Feb 11, 2013 8:10 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 5 guests