Help with canceling a function?

Hey so I have this function that creates a rectangle then loops with a delay (falling to the bottam as confetti). Its super simple and thats literaly all it does using a “for i = 0, 26 then” loop but I need to cancel the loop/function once a button is pressed. Is there any kind of “function.cancel” type of thing??? [import]uid: 14461 topic_id: 19043 reply_id: 319043[/import]

Maybe a flag?

such as;

[lua]local canFall = true[/lua]

Then a function on your button to set that to false.

Then in the loop add an “if” for if canFall == true then and and else return false.

Peach :slight_smile: [import]uid: 52491 topic_id: 19043 reply_id: 73462[/import]

hmmmm that didn’t work. I think its because the loop has already started and I need to stop it mid loop. [import]uid: 14461 topic_id: 19043 reply_id: 73538[/import]

Sorry, I shouldn’t have used return false then - break is what you should actually be using.

Eg;

[lua]for i = 1, 1000 do
if a == b then
break
end

– more stuff
end[/lua]

Peach [import]uid: 52491 topic_id: 19043 reply_id: 73699[/import]

but what does “a ==b” do? Like should I replace those with some variables like

if canFall == false then break end
and then write my other code here?
[import]uid: 14461 topic_id: 19043 reply_id: 73774[/import]

I tried using the code above /\ /\ but It didn’t work. There werent any errors, but it didnt stop the loop :frowning: [import]uid: 14461 topic_id: 19043 reply_id: 73777[/import]

a==b, I think was just an example. Maybe this will clarify it??

[code]
local canFall = true
for i=1,26 do
if canFall == false then
break
end

–make rectangle confetti stuff

end
local function pushButton()
–this function represents when your button is pushed (so don’t actually use it)
– just put the below line in the function that is called when your button is pressed
canFall = false
end

[/code] [import]uid: 94868 topic_id: 19043 reply_id: 73778[/import]

Ya thats what I did but It still doesnt work :frowning: Here’s the code

[code]
–creates confetti /
local function confetti (event)
for i = 0,26 do
if canFall == false then
break
end
randColor = math.random (1, 255)
rectX = math.random (0, 1000)
rectX2 = math.random (0, 1000)
rectX3 = math.random (0, 1000)
confettiSquareG = display.newRect(rectX, -50, 20, 30 )
confettiSquareG:setFillColor(0, 200, 100 )
confettiSquareG.onComplete = die
confettiSquareB = display.newRect(rectX2, -50, 20, 30 )
confettiSquareB:setFillColor(randColor, randColor, 200 )
confettiSquareB.onComplete = die
confettiSquareY = display.newRect(rectX3, -50, 20, 30 )
confettiSquareY:setFillColor(255, 255, 0 )
confettiSquareY.onComplete = die
transition.to ( confettiSquareB, { time=2000, delay=i* 100,y=( 900), onComplete =confettiSquareB})
transition.to ( confettiSquareG, { time=2000, delay=i* 100, y=( 900), onComplete = confettiSquareG})
transition.to ( confettiSquareY, { time=2000, delay=i* 100, y=( 900), onComplete = confettiSquareY})
end
end
–function for when button is tapped /

local function play (event)
if event.phase == “ended” then
if currentLevel+1 > #level then --this is just stuff for the last level
director:changeScene(“moreLevels”,“crossfade”)
nextButton:removeSelf()
else
canFall = false --changing canfall to false
director:changeScene(“nextLevel”)
nextButton:removeSelf()
end
end
end
[import]uid: 14461 topic_id: 19043 reply_id: 73781[/import]

Oh, wait. Maybe there is some confusion here.
Do you want to just cancel the creation of the confetti? This confetti function. When it runs once, it creates 27 confetti items and then sends them on a transition. Since this is all done in a for loop, this is all done as quickly as corona can make it happen (which is basically instantaneously). You won’t have any time to cancel that. You would have to make each piece of confetti after a delay, if you wanted to see an results like that.

Or do you want to cancel the animation of the confetti? Liking stopping them or removing them off of the screen?

Or is this confetti function called several times?
[import]uid: 94868 topic_id: 19043 reply_id: 73800[/import]

well there is already a delay so it does have that confetti effect (with random colors) but what I need to do is stop it after a button is pressed. Its only called once but Is there some way to cancel the for loop? (so the confeti stops getting created before it reaches the maximum amount in the loop) [import]uid: 14461 topic_id: 19043 reply_id: 73811[/import]

The only delay I see is the one associated with the transition for each confetti piece; So each piece of confetti is created, then they all wait for (i*100) milliseconds before they start moving down.

If you want cancel the creation of the confetti you would have to do something like this:

  
local canFall = true  
local confettiCount = 26  
  
local function confetti()  
 --confetti creation  
end  
  
local function createLotsOfConfetti()  
 if canFall == false then  
 return false  
 end  
  
 confetti()  
  
 confettiCount = confettiCount - 1  
  
 if confettiCount \> 0 then  
 timer.performWithDelay(100,createLotsOfConfetti)  
 end  
end  
  

See how I am actually delaying the creation of each individual confetti?
[import]uid: 94868 topic_id: 19043 reply_id: 73821[/import]

I already have the confetti working thats pretty close to what I did. But what I need to do is make it so that I can cancel the confetti. Right now it does the loop, confetti falls down 26 times, then stops. I need a button that can stop the looping mid way through (it all falls until a function is called which stops it, meaning confetty only may have fallen 10 times). I need this because when I end the level and change the scene the confetti is still dropping (still running through the for loop). [import]uid: 14461 topic_id: 19043 reply_id: 73831[/import]

as i know for loop is executed within frame(or second), so there’s no way to stop it
BUT, it appears to you that loop is working because of the transitions it makes happening, you need to pause them [import]uid: 16142 topic_id: 19043 reply_id: 73832[/import]

OOOOHHH thats perfect!!! Because in my case, the confetti stays out of the screen untill the transition! But how do I pause/cancel transitions? [import]uid: 14461 topic_id: 19043 reply_id: 73846[/import]

search http://developer.anscamobile.com/code/ this place for pausable transitions. There’s a module for them. [import]uid: 16142 topic_id: 19043 reply_id: 73852[/import]

Ya I did that and added this but it didn’t work

local function confetti (event)  
 for i = 0,26 do  
 if canFall == true then --NEW  
 randColor = math.random (1, 255)  
 rectX = math.random (0, 1000)  
 rectX2 = math.random (0, 1000)  
 rectX3 = math.random (0, 1000)  
 confettiSquareG = display.newRect(rectX, -50, 20, 30 )  
 confettiSquareG:setFillColor(0, 200, 100 )  
 confettiSquareG.onComplete = die  
 confettiSquareB = display.newRect(rectX2, -50, 20, 30 )  
 confettiSquareB:setFillColor(randColor, randColor, 200 )  
 confettiSquareB.onComplete = die  
 confettiSquareY = display.newRect(rectX3, -50, 20, 30 )  
 confettiSquareY:setFillColor(255, 255, 0 )  
 confettiSquareY.onComplete = die  
 local trans1 = transition.to ( confettiSquareB, { time=2000, delay=i\* 100,y=( 900), onComplete =confettiSquareB}) --NEW  
 local trans2 = transition.to ( confettiSquareG, { time=2000, delay=i\* 100, y=( 900), onComplete = confettiSquareG}) --NEW  
 local trans3 = transition.to ( confettiSquareY, { time=2000, delay=i\* 100, y=( 900), onComplete = confettiSquareY}) --NEW  
 else  
 transition.cancel(trans1) --NEW  
 transition.cancel(trans2) --NEW  
 transition.cancel(trans3) --NEW  
 end  
  
 end  
end  

I mean that looks like it should work :frowning: [import]uid: 14461 topic_id: 19043 reply_id: 73853[/import]

The transitions all have the same names for each loop - so you have 26 transitions with each name. Give them unique names. [import]uid: 52491 topic_id: 19043 reply_id: 73914[/import]

how do I give them different names when they are being created in a for loop? [import]uid: 14461 topic_id: 19043 reply_id: 73962[/import]

This is a little sample from one of my old apps; the code would create 15 images called grass1, grass2 … grass15.

[lua]grass = {}

for i = 1, 15 do
grass[i] = display.newImage(“grasstile.png”)
grass[i].x, grass[i].y = initX+count1*64, -180
count1 = count1+1
mapGroup:insert(grass[i])
end[/lua]

You could do something similar to name your transitions and confetti.

Peach :slight_smile: [import]uid: 52491 topic_id: 19043 reply_id: 74118[/import]

Alright i’lll try that so all i shud have to do is
grass[i].removeSelf()
right? [import]uid: 14461 topic_id: 19043 reply_id: 74405[/import]