How do i get this to only call once.?

I have a function which get’s called every one second. 

NOTE – Lets say the score update 1 every 2 seconds. 

local object = {} local oCounter = 1 local function spawnObject() if score == 0 then timee = 1 elseif score == 5 then timee = 2 end object[oCounter] = display.newImageRect( "image.png", 35, 35 ) object[oCounter].x = centerX object[oCounter].y = centerY object[oCounter].value = oCounter oCounter = oCounter + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

So every time the spawnObject function gets called then the if statement gets called. What i want is so the if statement gets called only when the score reaches the next number which is 5 in this case.

Thanks!

–SonicX278 

If I am following you, and you only want the if statement called once every 5 times, then the below should work.

local object = {} local oCounter = 1 local ifFlag = true local function spawnObject()     if ifFlag then timee = 1 ifFlag = false     elseif score == 5 then      timee = 2 ifFlag = true     end     object[oCounter] = display.newImageRect( "image.png", 35, 35 )     object[oCounter].x = centerX     object[oCounter].y = centerY     object[oCounter].value = oCounter     oCounter = oCounter + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

You just need to set the ifFlag to ‘true’ whenever you want the first part of the if statement to run. 

Ugh. Yes i did that multiple times while trouble shooting. It didn’t work. I will try again and report back. 

–SonicX278 

Ok huh?

If am a little confused so i hope my thoughts help.

if you only want to execute or change a value when your score changes to another values then keep track of current and previous scores ( old score and new score ) like this.

local object = {} local oCounter = 1 local iOldScore = 0 local iNewScore = 0 local function spawnObject() if iOldScore == iNewScore --Do nothing here unless you want to. timee = 1     elseif iOldScore ~= iNewScore then -- Scores have changed execute code and then make scores match      timee = 2 iOldScore = iNewScore     end     if score == 0 then timee = 1     elseif score == 5 then      timee = 2     end     object[oCounter] = display.newImageRect( "image.png", 35, 35 )     object[oCounter].x = centerX     object[oCounter].y = centerY     object[oCounter].value = oCounter     oCounter = oCounter + 1 iMyLoopCounter = iMyLoopCounter = + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

Yes. I’m still working on this problem. I will take a look at this after i get back from church. (About 2 hours) Then ill get back here and say if it worked or not.

–SonicX278 

Can you explain how your code works? I don’t see why i need 

iMyLoopCounter = iMyLoopCounter = + 1

And do i even need this piece?

 if score == 0 then timee = 1 elseif score == 5 then timee = 2 end

Not trying to sound harsh but seems like you just added code in not removing the old unnecessary one and that just gets confusing. Can you make take another look?

Thanks!

–SonicX278 

Well I made it work this way. 

local object = {} local oCounter = 1 local checkk = true local function timerCheckFunction() checkk = true end timee = timer.performWithDelay( 5000, timerCheckFunction, -1 ) local function spawnObject() if checkk == true then if score == 0 then timee = 1 checkk = false elseif score == 5 then timee = 2 checkk = false end end object[oCounter] = display.newImageRect( "image.png", 35, 35 ) object[oCounter].x = centerX object[oCounter].y = centerY object[oCounter].value = oCounter oCounter = oCounter + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

My spawn function timer goes every one second. Do you think it will be in sync with the timerCehckFunction timer?

–SonicX278 

Above was my fix but then I realized I could change the timee when clicking on the score. So i went with that. Thanks for the help!

–SonicX278 

sorry i got slammed doing other things. I meant to reply sooner, Glad you got it working.

Larry

If I am following you, and you only want the if statement called once every 5 times, then the below should work.

local object = {} local oCounter = 1 local ifFlag = true local function spawnObject()     if ifFlag then timee = 1 ifFlag = false     elseif score == 5 then      timee = 2 ifFlag = true     end     object[oCounter] = display.newImageRect( "image.png", 35, 35 )     object[oCounter].x = centerX     object[oCounter].y = centerY     object[oCounter].value = oCounter     oCounter = oCounter + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

You just need to set the ifFlag to ‘true’ whenever you want the first part of the if statement to run. 

Ugh. Yes i did that multiple times while trouble shooting. It didn’t work. I will try again and report back. 

–SonicX278 

Ok huh?

If am a little confused so i hope my thoughts help.

if you only want to execute or change a value when your score changes to another values then keep track of current and previous scores ( old score and new score ) like this.

local object = {} local oCounter = 1 local iOldScore = 0 local iNewScore = 0 local function spawnObject() if iOldScore == iNewScore --Do nothing here unless you want to. timee = 1     elseif iOldScore ~= iNewScore then -- Scores have changed execute code and then make scores match      timee = 2 iOldScore = iNewScore     end     if score == 0 then timee = 1     elseif score == 5 then      timee = 2     end     object[oCounter] = display.newImageRect( "image.png", 35, 35 )     object[oCounter].x = centerX     object[oCounter].y = centerY     object[oCounter].value = oCounter     oCounter = oCounter + 1 iMyLoopCounter = iMyLoopCounter = + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

Yes. I’m still working on this problem. I will take a look at this after i get back from church. (About 2 hours) Then ill get back here and say if it worked or not.

–SonicX278 

Can you explain how your code works? I don’t see why i need 

iMyLoopCounter = iMyLoopCounter = + 1

And do i even need this piece?

 if score == 0 then timee = 1 elseif score == 5 then timee = 2 end

Not trying to sound harsh but seems like you just added code in not removing the old unnecessary one and that just gets confusing. Can you make take another look?

Thanks!

–SonicX278 

Well I made it work this way. 

local object = {} local oCounter = 1 local checkk = true local function timerCheckFunction() checkk = true end timee = timer.performWithDelay( 5000, timerCheckFunction, -1 ) local function spawnObject() if checkk == true then if score == 0 then timee = 1 checkk = false elseif score == 5 then timee = 2 checkk = false end end object[oCounter] = display.newImageRect( "image.png", 35, 35 ) object[oCounter].x = centerX object[oCounter].y = centerY object[oCounter].value = oCounter oCounter = oCounter + 1 end dTimer = timer.performWithDelay( 1000, spawnObject, -1 )

My spawn function timer goes every one second. Do you think it will be in sync with the timerCehckFunction timer?

–SonicX278 

Above was my fix but then I realized I could change the timee when clicking on the score. So i went with that. Thanks for the help!

–SonicX278 

sorry i got slammed doing other things. I meant to reply sooner, Glad you got it working.

Larry