How to trigger a function when a boolean becomes true?

@Rob Miracle

Can I? its a local function for an object,

I spawn 10 balls on the top of the screen and then(after 4 seconds), when a boolean is true , I want them all to move 100 pixels down

Please let me know if there is a better way

Roy.

yes it can be done. if you show some code it would be easier to show how to do it

Ok thanks for trying to help

[lua]

– dispatch event to the initial drop

local tmr= timer.performWithDelay(2000,gem:dispatchEvent({name=“initDrop_listener” , state=“on”}),1)

–spawn the gems

for i=0 ,8  do

        local gem = display.newSprite( gemSheet, sequenceData )

        gem.width = 40

        gem.height = 40

        gem.x = 20

        gem.y = 424-40*i

        gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions

        gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions

        gem.MyGemID = board[gem.MyRow][gem.MyCol]

        gem:setSequence( “gem” … gem.MyGemID ) 

        gem:play() 

        gem.xScale=0.5

        gem.yScale=0.5

        gem.y = (-gem.MyRow*gemDimentions)

        – start the initial drop function

        local function initDrop_listener( event )

            if event.state == “on” then

                print(“drop success”) – debug that its firing

                gem:removeEventListener( “initDrop”, initDrop_listener )

            end

        end

        gem:addEventListener( “initDrop”, initDrop_listener )

    end

end

[/lua]

Obviously im doing something wrong :slight_smile:

Roy.

first line 2 name should be “initDrop”

second cause of the way your creating the balls this will only affect the last one created cause your over writing each one as the new one is created

try this

local gems = {} local function createBall(i)         local gem = display.newSprite( gemSheet, sequenceData )         gem.width = 40         gem.height = 40         gem.x = 20         gem.y = 424-40\*i         gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions         gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions         gem.MyGemID = board[gem.MyRow][gem.MyCol]         gem:setSequence( "gem" .. gem.MyGemID )          gem:play()          gem.xScale=0.5         gem.yScale=0.5         gem.y = (-gem.MyRow\*gemDimentions)         local function initDrop(  )                 print("drop success") -- debug that its firing         end         gem.tmr= timer.performWithDelay(2000, initDrop, 1)         return gem     end for i = 1, 8 do       gems[i] = createBall(i) end  

do away with the eventListener

But doesnt that mean that each gem has its own timer? cause the line

[lua]gem.tmr= timer.performWithDelay(2000, initDrop, 1)[/lua]

Is inside the spawned gems… I want the timer to be outside, is it possible?

Roy.

try this

local gems = {}     local function createBall(i)                  local gem = display.newSprite( gemSheet, sequenceData )                  gem.width = 40                  gem.height = 40                  gem.x = 20                  gem.y = 424-40\*i                  gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions                  gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions                  gem.MyGemID = board[gem.MyRow][gem.MyCol]                  gem:setSequence( "gem" .. gem.MyGemID )                   gem:play()                   gem.xScale=0.5                  gem.yScale=0.5                  gem.y = (-gem.MyRow\*gemDimentions)             local function initDrop( event )                     print("drop success") -- debug that its firing             end                  return gem              end      local function drop()   for i = 1,8 do     gems[i]:initDrop()   end end     for i = 1, 8 do           gems[i] = createBall(i)     end tmr = timer.performWithDelay( 2000, drop, 1 )

sorry had to edit should’ve tested first

First I want to thank you for your patient with helping me!

 

I get an error when I try this,

The error is - Attempt to index global ‘gem’ (a nil value)

And its referenced to the ‘addEventListener’ inside the loop…

Ive attached the project file, do you mind take a look at it?

The relevant rows are 64-95

 

Roy.

made edits to code above

will look at file but will be a while its 3am here gotta go bed

I think I got it to work

Many thank @jstrahan , good night :wink:

Roy.

great

nite