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???
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
Help with canceling a function?
Maybe a flag?
such as;
local canFall = true
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 :)
hmmmm that didn't work. I think its because the loop has already started and I need to stop it mid loop.
Sorry, I shouldn't have used return false then - break is what you should actually be using.
Eg;
1 2 3 4 5 6 7 | for i = 1, 1000 do if a == b then break end -- more stuff end |
but what does "a ==b" do? Like should I replace those with some variables like
1 2 3 | if canFall == false then break end |
I tried using the code above /\ /\ but It didn't work. There werent any errors, but it didnt stop the loop :(
a==b, I think was just an example. Maybe this will clarify it??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 |
Ya thats what I did but It still doesnt work :( Here's the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | --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 |
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?
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)
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 |
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).
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
OOOOHHH thats perfect!!! Because in my case, the confetti stays out of the screen untill the transition! But how do I pause/cancel transitions?
search http://developer.anscamobile.com/code/ this place for pausable transitions. There's a module for them.
Ya I did that and added this but it didn't work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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 |
The transitions all have the same names for each loop - so you have 26 transitions with each name. Give them unique names.
how do I give them different names when they are being created in a for loop?
This is a little sample from one of my old apps; the code would create 15 images called grass1, grass2 ... grass15.
1 2 3 4 5 6 7 8 | 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 |
Alright i'lll try that so all i shud have to do is
grass[i].removeSelf()
right?
I'm not sure where that would be in you code, you may use event.target or self, depending.
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed