infinite loop I need some help

I’m trying to create an infinite loop, But I get stuck in one part.

I have this code

local  function doMore () ball = {}         for column = 1, 12 do             for row = 1, 10 do             ball[column] = display.newCircle( math.random(1, 800), math.random(-10, 1000), math.random(10, 50) )             ball[column].alpha = 1             ball[column]:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255))             ball[column].x = math.random(80, 600) + (column\*math.random(80, 500))             ball[column].y = math.random(10, 700) + (row\*math.random(80, 1000))                 transition.to(ball[column], {time=20000, x=math.random(24, 5000), y=math.random(80, 2000)})         end     end end doMore ()  

I want to be able to make all those balls fade out – transition.to alpha = 0 – I guess

using the onComplete

like this

transition.to(ball[column], {time=20000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=again})

So the new function must have another set of balls, similar to the first one.

but making the first set vanishing little by little (alpha=0)

as the new loop will make another balls )like balls2[column],

when the new balls2 reaches the end, then have a call for the first ball loop and do that cycle forever

like part of a background animation.

Would you please help me out to figure this out.

Thanks

34 views and no one knows— if I discover the answer

I will shared it with you guys, don’t worry.

I modified the code and I got to a point that

I can tell the function to do something onComplete

local ball local  function doMore () ball = {}         for column = 1, 12 do             for row = 1, 10 do             ball[column] = display.newCircle( 0, 0, math.random(10, 50) )             ball[column].alpha = 1             ball[column]:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255))             ball[column].x = math.random(-200, 600) + (column\*math.random(1, 500))             ball[column].y = math.random(-100, 700) + (row\*math.random(1, 1000))                 local function again ()                         for column = 1, 12 do                         for row = 1, 10 do                                 transition.to(ball[column], {time=1000, x=200, y=200, alpha=0})                         end                     end                     end                 transition.to(ball[column], {time=20000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=again})         end     end end doMore ()

The strange thing it’s that I see about 100 circles at the beginning of running the code

they all are moving in different colors and size, and direction.

But on complete only about 10 0r 15 circles move and vanished (alpha=0)

I though all of them should vanished.

I still need help, let’s see who knows the answer

I’m not sure I understand exactly what you’re trying to do.  But in answer to your question why you see 100 balls at first (actually, 120, since you have 10 rows x 12 columns) and only 10-15 balls later (actually, 12, since you have 12 columns) is because of the way you’re storing the balls.  You’re storing the balls is ball[column], forgetting about the row.  As a result, each ball in the next row overwrites your reference to the previous ball in that row.

One way to fix that would be to replace ball[column] with ball[(row-1)*column + column].  Alternatively, you could make ball a 2-dimensional table instead of a 1-dimensional table.

  • Andrew

A couple of things:

  1. Based on where the ‘again’ function is defined, you’re actually creating 120 different ‘again’ functions, one per object (they’re all equivalent, but they should be separate Lua function-objects)

  2. Each one of your initial transitions (that move to new x,y) launches a separate call to the onComplete function … so you have 120 calls to the ‘again’ function … but each single call to the ‘again’ function is itself launching 120 new transitions (to change alpha) … so you’re actually making 120*120=14,400 calls to transition the alpha parameter (many of which are modifying the same objects)

I think you might want 1 ‘doMore’ function and 1 ‘again’ function.  Make the ‘doMore’ function do the 120 transitions to move to the new x,y coords, then, outside of the two loops, add one more transition to do the ‘onComplete’ call to the ‘again’ function (really, this should also be a timer).  Then make the ‘again’ function do the 120 transitions for alpha.

Thanks – aukStudios and jbp1

I can tell that you know a lot.

unfortunately I don’t.–


When you say that you are not sure what I want. –

it’s just some circles moving from one place to another

at a random location and color an sizes

and do that forever.

if I do that only once.

the circles will stop moving.

and I want that as an animation to keep moving

all the time, like a background.


Both of you are right. actually I was running my code

and I saw millions of balls all over, after the second time,

now I know it was 14,400.


as far as the – ball[column] – or “storing the balls”

I copy that from another place, it was something like this

for i = 1, 12 do for j = 1, 10 do ball[i] = display.newCircle( 100, 100, 10)

I just change the – i – for column so I can understand it better.

but I have no idea of the “storing” thing


and you are right that I’m making 120 calls

but

how do I fix the code?

would it be to much to ask, if you can re-write the code

to see how it works?

thanks

Here’s an approach to have, say, 100 balls on the screen.  When one ball finishes moving, it starts another ball to take it’s place:

local makeNewBall local function finishTransition( obj ) transition.to( obj, {time=1000, x=200, y=200, alpha=0, onComplete=makeNewBall}) end makeNewBall = local function() local ball = display.newCircle( 0, 0, math.random(10, 50) ) ball.alpha = 1 ball:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255)) ball.x = math.random(-200, 600) + (column\*math.random(1, 500)) ball.y = math.random(-100, 700) + (row\*math.random(1, 1000)) transition.to( ball, {time=20000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=finishTransition}) return ball end function doMore() for n = 1, 100 do ball[n] = makeNewBall() end end

This may not be 100% correct, but the idea is that the doMore function kicks off 100 objects.  Each of those 100 objects initiates its own transition (move to x,y), then runs ‘finishTransition’ when it’s done.  The finishTransition function does the alpha-transition and when that is complete, it fires off another makeNewBall.

Technically, the alpha-transition calls makeNewBall with an existing object (which can be ignored).  Not sure if Corona will balk at that.

If you want to track all the balls through the “ball[n]” array, you’ll need to hack something into makeNewBall or make another onComplete function for the alpha-transition.

Thank you jbp1 – I run your code

and it gives me errors.

but I started from scratch, with just one ball or circle.

like this…

I was thinking and thinking of this. (last night I did not sleep much)


This is exactly what I want – I did this with just “one” ball

local oneCircle = display.newCircle( 800, 1000, 50 )     local function bye ()         display.remove (oneCircle)     end transition.to(oneCircle, {time=1000, x=1, alpha=0, onComplete=bye})

I did this with one circle only, in white.


the idea is the same, but a lot of circles 100 , 200.


the circles will move in a random direction on x and y


they can have different colors (r, g, B)


different sizes, :scale


and when they remove.self


then a lot more circles will show up and do the same


I hope this gives you more idea of what I’m looking for.

I think I’m close, but I really need your help


I have no idea how I got the yellow little face

I was trying to write – red, green blue

where do those little faces come from?

hi guys…


this is exactly what I need.

I did this with 2 circles, or balls.

I can just keep doing it and get 200 of this chunk of codes.

but I know for sure that is not the best way.

I need a “for - loop” I guess.

That is why I do need your help.

but I got 2 circles working exactly how I want.

like this

local function oneCircleFunction ()     local oneCircle = display.newCircle( 800, 1000, math.random(10, 50) )         oneCircle:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255))         local function bye ()             display.remove (oneCircle)             oneCircleFunction ()         end     transition.to(oneCircle, {time=5000, x=math.random(1, 1000), y=math.random(1, 5000), alpha=0, onComplete=bye}) end timer.performWithDelay( 1000, oneCircleFunction ) local function oneCircleFunction2 ()     local oneCircle2 = display.newCircle( 800, 1000, math.random(5, 65) )         oneCircle2:setFillColor(math.random(1, 255),math.random(10, 200),math.random(58, 240))         local function bye2 ()             display.remove (oneCircle2)             oneCircleFunction2 ()         end     transition.to(oneCircle2, {time=4000, x=math.random(105, 2500), y=math.random(10, 4600), alpha=0, onComplete=bye2}) end timer.performWithDelay( 500, oneCircleFunction2 )

Thanks for your help

I hope someone can actually write on loop to be able to do 100 or 200 circles

This code works for me:

local makeNewBall local count = 0 local function cleanUp( obj ) count = count - 1 print( "clean up "..count ) obj:removeSelf() timer.performWithDelay( 100, makeNewBall ) end local function finishTransition( obj ) print( "finish tran" ) transition.to( obj, {time=1000, x=200, y=200, alpha=0, onComplete=cleanUp}) end makeNewBall = function() count = count + 1 print( "new ball "..count ) local ball = display.newCircle( 0, 0, math.random(10, 50) ) ball.alpha = 1 ball:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255)) ball.x = math.random(-200, 600) + math.random(1, 500) ball.y = math.random(-100, 700) + math.random(1, 1000) transition.to( ball, {time=5000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=finishTransition}) return ball end function doMore() for n = 1, 100 do makeNewBall() end end doMore()

jbp1 – The best in the world!

yes!

it works!!!

thank you, thank you very much.

I’m going to star working on my new app now.

this is just the beginning.

Thank you one more time

your friend

Victor


after reading the code.


the actual loop is inside the doMore function.

for n = 1, 100

so inside the loop, you just call the function (1, 2, 3, 4, 5 and so on…untill you reach 100)

you are not making the “object”


now you have a function to make the objects.

–is there any reason why you did

makeNewBall = function()

instead of

function makeNewBall()

now, I don really get this line, why?

count = count + 1

also, you create the object, display, the position x, y and so on

but at the end you put

return ball

what is exactly the “return”

I don’t get this concept of return.

I’ve seen it many times, I just don’t understand it.


then on the finishTransition you put – obj – inside the parenthesis

I though that was only for

– obj - event.target

where did you get the  – obj – from?


now finally in the cleanUp function you put

count = count - 1

i don’t get it. why - 1?


last 2 questions –

how long have you been programming?

and where did you learn all this?

Some of this is left-over from debugging of the code …

and I’m not all that fluent in Lua yet, so maybe this would work by just defining “function makeNewBall” instead of the forward declaration (“local makeNewBall”).  It just comes out from the fact that there is a circular dependency among the 3 functions … so we first tell Lua that there is thing called “makeNewBall” and don’t worry, we’ll define it later (which is the “makeNewBall = function()” part).  

The “count” stuff is just for debugging … making sure that the count goes to zero in between each transition.

The “return ball” part of makeNewBall is just if you want to keep track of individual objects in some other array (like in your original code).  In doMore, you could add something like “ball[i] = makeNewBall()”.  But you’d need to do some more work to capture the objects created by the cleanUp function – timer.performWithDelay just executes the function you give it, it can’t do anything with a return value from that function.  

You could potentially use a file-local variable (array) and the count variable and store the circle objects inside the makeNewBall function.  Or you could just add each ball to a new displayGroup, then look at the children of that group (i.e. let Corona track the objects for you).

thanks…

I’m going to keep studying a lot more…

I hope I can really understand the concept really well…one day.

for now the code works really good

thank you jbp1

34 views and no one knows— if I discover the answer

I will shared it with you guys, don’t worry.

I modified the code and I got to a point that

I can tell the function to do something onComplete

local ball local  function doMore () ball = {}         for column = 1, 12 do             for row = 1, 10 do             ball[column] = display.newCircle( 0, 0, math.random(10, 50) )             ball[column].alpha = 1             ball[column]:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255))             ball[column].x = math.random(-200, 600) + (column\*math.random(1, 500))             ball[column].y = math.random(-100, 700) + (row\*math.random(1, 1000))                 local function again ()                         for column = 1, 12 do                         for row = 1, 10 do                                 transition.to(ball[column], {time=1000, x=200, y=200, alpha=0})                         end                     end                     end                 transition.to(ball[column], {time=20000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=again})         end     end end doMore ()

The strange thing it’s that I see about 100 circles at the beginning of running the code

they all are moving in different colors and size, and direction.

But on complete only about 10 0r 15 circles move and vanished (alpha=0)

I though all of them should vanished.

I still need help, let’s see who knows the answer

I’m not sure I understand exactly what you’re trying to do.  But in answer to your question why you see 100 balls at first (actually, 120, since you have 10 rows x 12 columns) and only 10-15 balls later (actually, 12, since you have 12 columns) is because of the way you’re storing the balls.  You’re storing the balls is ball[column], forgetting about the row.  As a result, each ball in the next row overwrites your reference to the previous ball in that row.

One way to fix that would be to replace ball[column] with ball[(row-1)*column + column].  Alternatively, you could make ball a 2-dimensional table instead of a 1-dimensional table.

  • Andrew

A couple of things:

  1. Based on where the ‘again’ function is defined, you’re actually creating 120 different ‘again’ functions, one per object (they’re all equivalent, but they should be separate Lua function-objects)

  2. Each one of your initial transitions (that move to new x,y) launches a separate call to the onComplete function … so you have 120 calls to the ‘again’ function … but each single call to the ‘again’ function is itself launching 120 new transitions (to change alpha) … so you’re actually making 120*120=14,400 calls to transition the alpha parameter (many of which are modifying the same objects)

I think you might want 1 ‘doMore’ function and 1 ‘again’ function.  Make the ‘doMore’ function do the 120 transitions to move to the new x,y coords, then, outside of the two loops, add one more transition to do the ‘onComplete’ call to the ‘again’ function (really, this should also be a timer).  Then make the ‘again’ function do the 120 transitions for alpha.

Thanks – aukStudios and jbp1

I can tell that you know a lot.

unfortunately I don’t.–


When you say that you are not sure what I want. –

it’s just some circles moving from one place to another

at a random location and color an sizes

and do that forever.

if I do that only once.

the circles will stop moving.

and I want that as an animation to keep moving

all the time, like a background.


Both of you are right. actually I was running my code

and I saw millions of balls all over, after the second time,

now I know it was 14,400.


as far as the – ball[column] – or “storing the balls”

I copy that from another place, it was something like this

for i = 1, 12 do for j = 1, 10 do ball[i] = display.newCircle( 100, 100, 10)

I just change the – i – for column so I can understand it better.

but I have no idea of the “storing” thing


and you are right that I’m making 120 calls

but

how do I fix the code?

would it be to much to ask, if you can re-write the code

to see how it works?

thanks

Here’s an approach to have, say, 100 balls on the screen.  When one ball finishes moving, it starts another ball to take it’s place:

local makeNewBall local function finishTransition( obj ) transition.to( obj, {time=1000, x=200, y=200, alpha=0, onComplete=makeNewBall}) end makeNewBall = local function() local ball = display.newCircle( 0, 0, math.random(10, 50) ) ball.alpha = 1 ball:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255)) ball.x = math.random(-200, 600) + (column\*math.random(1, 500)) ball.y = math.random(-100, 700) + (row\*math.random(1, 1000)) transition.to( ball, {time=20000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=finishTransition}) return ball end function doMore() for n = 1, 100 do ball[n] = makeNewBall() end end

This may not be 100% correct, but the idea is that the doMore function kicks off 100 objects.  Each of those 100 objects initiates its own transition (move to x,y), then runs ‘finishTransition’ when it’s done.  The finishTransition function does the alpha-transition and when that is complete, it fires off another makeNewBall.

Technically, the alpha-transition calls makeNewBall with an existing object (which can be ignored).  Not sure if Corona will balk at that.

If you want to track all the balls through the “ball[n]” array, you’ll need to hack something into makeNewBall or make another onComplete function for the alpha-transition.

Thank you jbp1 – I run your code

and it gives me errors.

but I started from scratch, with just one ball or circle.

like this…

I was thinking and thinking of this. (last night I did not sleep much)


This is exactly what I want – I did this with just “one” ball

local oneCircle = display.newCircle( 800, 1000, 50 )     local function bye ()         display.remove (oneCircle)     end transition.to(oneCircle, {time=1000, x=1, alpha=0, onComplete=bye})

I did this with one circle only, in white.


the idea is the same, but a lot of circles 100 , 200.


the circles will move in a random direction on x and y


they can have different colors (r, g, B)


different sizes, :scale


and when they remove.self


then a lot more circles will show up and do the same


I hope this gives you more idea of what I’m looking for.

I think I’m close, but I really need your help


I have no idea how I got the yellow little face

I was trying to write – red, green blue

where do those little faces come from?

hi guys…


this is exactly what I need.

I did this with 2 circles, or balls.

I can just keep doing it and get 200 of this chunk of codes.

but I know for sure that is not the best way.

I need a “for - loop” I guess.

That is why I do need your help.

but I got 2 circles working exactly how I want.

like this

local function oneCircleFunction ()     local oneCircle = display.newCircle( 800, 1000, math.random(10, 50) )         oneCircle:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255))         local function bye ()             display.remove (oneCircle)             oneCircleFunction ()         end     transition.to(oneCircle, {time=5000, x=math.random(1, 1000), y=math.random(1, 5000), alpha=0, onComplete=bye}) end timer.performWithDelay( 1000, oneCircleFunction ) local function oneCircleFunction2 ()     local oneCircle2 = display.newCircle( 800, 1000, math.random(5, 65) )         oneCircle2:setFillColor(math.random(1, 255),math.random(10, 200),math.random(58, 240))         local function bye2 ()             display.remove (oneCircle2)             oneCircleFunction2 ()         end     transition.to(oneCircle2, {time=4000, x=math.random(105, 2500), y=math.random(10, 4600), alpha=0, onComplete=bye2}) end timer.performWithDelay( 500, oneCircleFunction2 )

Thanks for your help

I hope someone can actually write on loop to be able to do 100 or 200 circles

This code works for me:

local makeNewBall local count = 0 local function cleanUp( obj ) count = count - 1 print( "clean up "..count ) obj:removeSelf() timer.performWithDelay( 100, makeNewBall ) end local function finishTransition( obj ) print( "finish tran" ) transition.to( obj, {time=1000, x=200, y=200, alpha=0, onComplete=cleanUp}) end makeNewBall = function() count = count + 1 print( "new ball "..count ) local ball = display.newCircle( 0, 0, math.random(10, 50) ) ball.alpha = 1 ball:setFillColor(math.random(12, 255),math.random(5, 255),math.random(5, 255)) ball.x = math.random(-200, 600) + math.random(1, 500) ball.y = math.random(-100, 700) + math.random(1, 1000) transition.to( ball, {time=5000, x=math.random(24, 5000), y=math.random(80, 2000), onComplete=finishTransition}) return ball end function doMore() for n = 1, 100 do makeNewBall() end end doMore()