Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Help with canceling a function?
Started by AppdojoStudios Dec 12 2011 12:19 PM

- - - - -
20 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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???
uid: 14461 topic_id: 19043 reply_id: 319043


[TOPIC: post.html]
#2

peach pellen

[GLOBAL: userInfoPane.html]
peach pellen
  • Corona Geek

  • 8,866 posts
  • Alumni

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 :)
uid: 52491 topic_id: 19043 reply_id: 73462


[TOPIC: post.html]
#3

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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


[TOPIC: post.html]
#4

peach pellen

[GLOBAL: userInfoPane.html]
peach pellen
  • Corona Geek

  • 8,866 posts
  • Alumni

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

Eg;

for i = 1, 1000 do   if a == b then       break   end   -- more stuffend


Peach
uid: 52491 topic_id: 19043 reply_id: 73699


[TOPIC: post.html]
#5

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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?
uid: 14461 topic_id: 19043 reply_id: 73774


[TOPIC: post.html]
#6

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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


[TOPIC: post.html]
#7

Screaming Leaf

[GLOBAL: userInfoPane.html]
Screaming Leaf
  • Contributor

  • 144 posts
  • Corona SDK

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

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

uid: 94868 topic_id: 19043 reply_id: 73778


[TOPIC: post.html]
#8

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

Ya thats what I did but It still doesnt work :( Here's the code
[code=auto:0]
--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
uid: 14461 topic_id: 19043 reply_id: 73781


[TOPIC: post.html]
#9

Screaming Leaf

[GLOBAL: userInfoPane.html]
Screaming Leaf
  • Contributor

  • 144 posts
  • Corona SDK

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?
uid: 94868 topic_id: 19043 reply_id: 73800


[TOPIC: post.html]
#10

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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)
uid: 14461 topic_id: 19043 reply_id: 73811


[TOPIC: post.html]
#11

Screaming Leaf

[GLOBAL: userInfoPane.html]
Screaming Leaf
  • Contributor

  • 144 posts
  • Corona SDK

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?
uid: 94868 topic_id: 19043 reply_id: 73821


[TOPIC: post.html]
#12

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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).
uid: 14461 topic_id: 19043 reply_id: 73831


[TOPIC: post.html]
#13

darkconsoles

[GLOBAL: userInfoPane.html]
darkconsoles
  • Contributor

  • 795 posts
  • Guests

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
uid: 16142 topic_id: 19043 reply_id: 73832


[TOPIC: post.html]
#14

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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


[TOPIC: post.html]
#15

darkconsoles

[GLOBAL: userInfoPane.html]
darkconsoles
  • Contributor

  • 795 posts
  • Guests

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


[TOPIC: post.html]
#16

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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 :(
uid: 14461 topic_id: 19043 reply_id: 73853


[TOPIC: post.html]
#17

peach pellen

[GLOBAL: userInfoPane.html]
peach pellen
  • Corona Geek

  • 8,866 posts
  • Alumni

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


[TOPIC: post.html]
#18

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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


[TOPIC: post.html]
#19

peach pellen

[GLOBAL: userInfoPane.html]
peach pellen
  • Corona Geek

  • 8,866 posts
  • Alumni

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

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


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

Peach :)
uid: 52491 topic_id: 19043 reply_id: 74118


[TOPIC: post.html]
#20

AppdojoStudios

[GLOBAL: userInfoPane.html]
AppdojoStudios
  • Contributor

  • 110 posts
  • Corona SDK

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


[TOPIC: post.html]
#21

peach pellen

[GLOBAL: userInfoPane.html]
peach pellen
  • Corona Geek

  • 8,866 posts
  • Alumni

I'm not sure where that would be in you code, you may use event.target or self, depending.
uid: 52491 topic_id: 19043 reply_id: 74479



[topic_controls]
[/topic_controls]