How to simulate fuse for bomb?

Have bombs being spawned but I’m not sure how to spawn each one with a “fuse” that is set to detonate when it reaches zero or how to begin the countdown for each “fuse”

This looks interesting - i think your request needs clarification.

Ideas about how to code a solution to create a countdown per spawned bomb.

or

ideas of how to graphically represent a fuse burning down.

I’ve done neither in the past but i think a clarification of your request might start the ball rolling or fuse if you like.

T.

I’ll have to represent it graphically at some point but I was primarily concerned with how to create a countdown per spawned bomb.

I have the bombs being spawned into an array as bomb[i] and i goes up by 1 every time a new bomb is spawned

this may help"

  local bombs = {} local timers = {} local function explode(e) local params =  e.source.params print(" id " .. params.bomb.id) -- now activate explosion action .. whatever that may be end   for i = 1, 10 do table.insert(bombs, display.newImage("bomb.png")) bombs[i].id = i bombs[i].x = 50 + (i \* 30) bombs[i].y = 50 timers[i] = timer.performWithDelay( i \* 1000, explode ) timers[i].params = { bomb = bombs[i] } end  

Could you explain a little bit what the “local params” does and why it’s used in " timers[i].params?"

Here is a link to corona docs about passing parameters to the ‘performWithDelay’ function :

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

In the example I sent in the previous reply, at the very end of the loop  (for i = 1, 10), the last lines in that loop

do 2 things…

  1. creates a separate timer for each bomb

  2. attaches a table to the timer.

This table I named ‘params’.  But you can name it anything you want.

The explode function is shared by all 10 timers.  So, it needs to know what timer has called it.

‘source’ is the timer that called it.

‘params’ is the table attached to the timer when it was created

‘bomb’ is a member of that table that points to the bomb object (so explode function knows which bomb to explode)

In the explode function I chose to ‘localize’ the table ‘params’ that is attached to the timer object

local params = e.source.params

print("id " … params.bomb.id)

But, you do NOT have to do it that way.  You can erase the line ‘local params = e.source.params’ and access the bomb object directly like so …

print("id " … e.source.params.id)

I could have chose to not attach a ‘table’ to the timer object, but instead just attached the variable/handle/pointer to the

timer object directly …

timers[i].bomb = bombs[i]

I chose to use a table in the original example, but either way works. The advantage of the table is, if I need to add more information later that the explode function may need I can just add those variables to the params table, and they all get attached and sent together to the explode function.

It is common to name such a table params (short for parameters) but is no required.  The table can be called almost anything.

Here is another example:

– NO TABLE PASSED … just a pointer(handle) to bomb object attached to timer object

– AND THE HANDLE ‘bomb’ IS NOT LOCALIZED

– as each timer expires, the bomb image will disappear from screen(as if it exploded)

local bombs = {} local timers = {} local function explode(e) e.source.bomb.isVisible = false end   for i = 1, 10 do table.insert(bombs, display.newImage("bomb.png")) bombs[i].id = i bombs[i].x = 50 + (i \* 30) bombs[i].y = 50 timers[i] = timer.performWithDelay( i \* 1000, explode ) timers[i].bomb = bombs[i] end

Wow! Thanks for the great explanation and the help.  I understand this thoroughly now.

good to hear. hope you make a great app.

Do you know how I’d get rid of that timer?

I have the bombs being dragged to a zone and once they’re in the that zone they shouldn’t blow up. But the timer doesn’t stop.

I’ve tried to use removeSelf() but I keep getting errors. I’ve been stuck on this for over a week lol

Antonio,

Easy to do.  I am assuming you already have an event function for dragging the bomb.  So, do 2 things.

  1. In the loop that creates the bombs and timers, add that event listener to each bomb object. Although I imagine you already have down that, otherwise I am not sure how you are managing the dragging of the bombs into that zone.

  2. In ‘your’ event function that handles the dragging of the bomb, in the ‘end-phase’ just 

call timer.cancel(timers[e.target.id]).  

That is why I added the field ‘id’ to each bomb object, just for this kind of situation.

Also, there are many other ways to do the same thing, but the main answer to your particular question is the use of  timer.cancel(‘handle to the timer you want to cancel’)

  local bombs = {} local timers = {} local function explode(e) e.source.bomb.isVisible = false print(" source " .. e.source.flag) end   local function onDrag(e) if e.phase == "began" then -- assume you have some code here for dragging bomb elseif e.phase == "moved" then -- assume you have some code here for dragging bomb elseif e.phase == "ended" then -- assume you have some code here for dragging bomb timer.cancel(timers[e.target.id]) end return true end   for i = 1, 10 do table.insert(bombs, display.newImage("bomb.png")) bombs[i].id = i bombs[i].x = 50 + (i \* 30) bombs[i].y = 50 timers[i] = timer.performWithDelay( i \* 1000, explode ) timers[i].bomb = bombs[i] timers[i].flag = i \* 10 bombs[i]:addEventListener("touch", onDrag) end    

This looks interesting - i think your request needs clarification.

Ideas about how to code a solution to create a countdown per spawned bomb.

or

ideas of how to graphically represent a fuse burning down.

I’ve done neither in the past but i think a clarification of your request might start the ball rolling or fuse if you like.

T.

I’ll have to represent it graphically at some point but I was primarily concerned with how to create a countdown per spawned bomb.

I have the bombs being spawned into an array as bomb[i] and i goes up by 1 every time a new bomb is spawned

this may help"

  local bombs = {} local timers = {} local function explode(e) local params =  e.source.params print(" id " .. params.bomb.id) -- now activate explosion action .. whatever that may be end   for i = 1, 10 do table.insert(bombs, display.newImage("bomb.png")) bombs[i].id = i bombs[i].x = 50 + (i \* 30) bombs[i].y = 50 timers[i] = timer.performWithDelay( i \* 1000, explode ) timers[i].params = { bomb = bombs[i] } end  

Could you explain a little bit what the “local params” does and why it’s used in " timers[i].params?"

Here is a link to corona docs about passing parameters to the ‘performWithDelay’ function :

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

In the example I sent in the previous reply, at the very end of the loop  (for i = 1, 10), the last lines in that loop

do 2 things…

  1. creates a separate timer for each bomb

  2. attaches a table to the timer.

This table I named ‘params’.  But you can name it anything you want.

The explode function is shared by all 10 timers.  So, it needs to know what timer has called it.

‘source’ is the timer that called it.

‘params’ is the table attached to the timer when it was created

‘bomb’ is a member of that table that points to the bomb object (so explode function knows which bomb to explode)

In the explode function I chose to ‘localize’ the table ‘params’ that is attached to the timer object

local params = e.source.params

print("id " … params.bomb.id)

But, you do NOT have to do it that way.  You can erase the line ‘local params = e.source.params’ and access the bomb object directly like so …

print("id " … e.source.params.id)

I could have chose to not attach a ‘table’ to the timer object, but instead just attached the variable/handle/pointer to the

timer object directly …

timers[i].bomb = bombs[i]

I chose to use a table in the original example, but either way works. The advantage of the table is, if I need to add more information later that the explode function may need I can just add those variables to the params table, and they all get attached and sent together to the explode function.

It is common to name such a table params (short for parameters) but is no required.  The table can be called almost anything.

Here is another example:

– NO TABLE PASSED … just a pointer(handle) to bomb object attached to timer object

– AND THE HANDLE ‘bomb’ IS NOT LOCALIZED

– as each timer expires, the bomb image will disappear from screen(as if it exploded)

local bombs = {} local timers = {} local function explode(e) e.source.bomb.isVisible = false end   for i = 1, 10 do table.insert(bombs, display.newImage("bomb.png")) bombs[i].id = i bombs[i].x = 50 + (i \* 30) bombs[i].y = 50 timers[i] = timer.performWithDelay( i \* 1000, explode ) timers[i].bomb = bombs[i] end

Wow! Thanks for the great explanation and the help.  I understand this thoroughly now.

good to hear. hope you make a great app.

Do you know how I’d get rid of that timer?

I have the bombs being dragged to a zone and once they’re in the that zone they shouldn’t blow up. But the timer doesn’t stop.

I’ve tried to use removeSelf() but I keep getting errors. I’ve been stuck on this for over a week lol

Antonio,

Easy to do.  I am assuming you already have an event function for dragging the bomb.  So, do 2 things.

  1. In the loop that creates the bombs and timers, add that event listener to each bomb object. Although I imagine you already have down that, otherwise I am not sure how you are managing the dragging of the bombs into that zone.

  2. In ‘your’ event function that handles the dragging of the bomb, in the ‘end-phase’ just 

call timer.cancel(timers[e.target.id]).  

That is why I added the field ‘id’ to each bomb object, just for this kind of situation.

Also, there are many other ways to do the same thing, but the main answer to your particular question is the use of  timer.cancel(‘handle to the timer you want to cancel’)

  local bombs = {} local timers = {} local function explode(e) e.source.bomb.isVisible = false print(" source " .. e.source.flag) end   local function onDrag(e) if e.phase == "began" then -- assume you have some code here for dragging bomb elseif e.phase == "moved" then -- assume you have some code here for dragging bomb elseif e.phase == "ended" then -- assume you have some code here for dragging bomb timer.cancel(timers[e.target.id]) end return true end   for i = 1, 10 do table.insert(bombs, display.newImage("bomb.png")) bombs[i].id = i bombs[i].x = 50 + (i \* 30) bombs[i].y = 50 timers[i] = timer.performWithDelay( i \* 1000, explode ) timers[i].bomb = bombs[i] timers[i].flag = i \* 10 bombs[i]:addEventListener("touch", onDrag) end