Problem with physics and loops

Hello.

I have a little problem. I hope you can help me to solve this one.


I have a loop that creates 4 balls

balls = {}         for s = 1, 4 do             balls[s] = display.newImage("recamara/rPelota.png", 50 + (s\*150), 10)             group:insert ( balls[s] )             balls[s]:scale (.5, .5)             balls[s].isVisible = false             --physics.addBody( balls[s], "dynamic", { density=0.9, friction=0.1, bounce=.6, radius=24 } )         end

As you can see I did not addBody, because I want to add body later in the game.

now I have a function that adds the body and make the balls visible

local function four ()         balls[s].isVisible = true         physics.addBody( balls[s], "dynamic", { density=3.0, friction=0.5, bounce=1, radius=20 } )         transition.to(tx4, {time=7000, alpha=0})     end

When I tap on the number 4, I call the function four and I supposed to see the balls falling

tx4 = display.newText ("4", 0, 0, "Helvetica", 150 )     group:insert ( tx4 )     tx4.x = 750; tx4.y = 300     tx4:setTextColor(0, 0, 0)     tx4.isVisible = false     tx4:addEventListener ("tap", four)

But when I call the function on --tx4 I get a “nil” value

and I put local balls on top.

So how do I create the objects with a loop

and make them not visible and no physics

and then after in another function, make them visible

and add body.?

How would I solve this problem?

Please I need help

thanks

Looks like you forgot the for do loop in your function “four”.  Try this:

local function four () for s = 1, 4 do         balls[s].isVisible = true         physics.addBody( balls[s], "dynamic", { density=3.0, friction=0.5, bounce=1, radius=20 } )         transition.to(tx4, {time=7000, alpha=0}) end end  

It’s incredible how things can be so simple, when you know what to do.

I was thinking, how can I add the physics and the visible true, to each object.

I did not know that you need to do also a loop in the other function.

I did it, and it works perfect, like magic.

Thank you very much. I have no words to say thanks to all the people that have help me

learn corona, and lua.

The only way I know it’s trying to make nice apps for kids to try to teach them a few

things while having fun playing with the iPad.

Thank you for everything!

Victor

Let me ask you one more thing, related to the loops.

in my game, I create the objects. So all the objects have to fall at the end.

in the iPad the .y is 768

so if the object keeps falling, eventually the .y of the object will be 780 and 790 and 800 and so on…

if I use this logic.

How do I tell the program in the loop

if balls.y == 800 then

    audio.play(win)

end

How do the program “knows” when each ball.y will be 800?

Thanks

You won’t be able to detect the balls reaching y == 800 in the loop you have.  The loop sets up the balls’ initial states and completes before the balls even start moving.

Since you are using physics objects there are a couple different ways to get what you want.  The first would be to set up another physics object like a long rectangle that would serve as a “floor”, make it a sensor, and put it at the y position 800.  The sensor object will still detect collision events, so you could set up a listener such that whenever a ball hits the sensor you plays the audio “win” sound, destroy the ball, or whatever you want it to do. 

Another way would be to set up a runtime listener to check the y position of each ball every frame.  If a ball’s y position is > 800 then you could call the function to play the win sound.

Thanks, I used the Runtime – it works!

the isSensor – I don’t know how to do that yet. – if you have a sample I will really appreciate it.

also which of those two ways will be “better”

The isSensor is simply a property of the physics object.  In your case you could have a “floor” physics object and set floor.isSensor = true.  Now when the balls collide with the floor you will get normal collision events firing which you can detect with a listener attached to the floor.  If a collision is detected you know to use the win sequence function.  You can also do this with an physics object floor that isn’t a sensor, but then the balls will actually stop or bounce when they hit the floor which may not be the effect you want.

In terms of what’s “better” I usually go with the solution that I know how to get working!  In your case it sounds like you were able to get the first way to work, so then I would say, “good enough” and move on to other problems.  You can spend a lot of time trying to optimize your code, or be smart/clever/elegant with it, but gain no noticeable improvement in the final effect.  If you are coding by yourself, as long as you understand what you’re doing and it works then that’s all you need, imo.  (Of course, if other people will read or use your code that might be a very good reason to clean things up. )

Looks like you forgot the for do loop in your function “four”.  Try this:

local function four () for s = 1, 4 do         balls[s].isVisible = true         physics.addBody( balls[s], "dynamic", { density=3.0, friction=0.5, bounce=1, radius=20 } )         transition.to(tx4, {time=7000, alpha=0}) end end  

It’s incredible how things can be so simple, when you know what to do.

I was thinking, how can I add the physics and the visible true, to each object.

I did not know that you need to do also a loop in the other function.

I did it, and it works perfect, like magic.

Thank you very much. I have no words to say thanks to all the people that have help me

learn corona, and lua.

The only way I know it’s trying to make nice apps for kids to try to teach them a few

things while having fun playing with the iPad.

Thank you for everything!

Victor

Let me ask you one more thing, related to the loops.

in my game, I create the objects. So all the objects have to fall at the end.

in the iPad the .y is 768

so if the object keeps falling, eventually the .y of the object will be 780 and 790 and 800 and so on…

if I use this logic.

How do I tell the program in the loop

if balls.y == 800 then

    audio.play(win)

end

How do the program “knows” when each ball.y will be 800?

Thanks

You won’t be able to detect the balls reaching y == 800 in the loop you have.  The loop sets up the balls’ initial states and completes before the balls even start moving.

Since you are using physics objects there are a couple different ways to get what you want.  The first would be to set up another physics object like a long rectangle that would serve as a “floor”, make it a sensor, and put it at the y position 800.  The sensor object will still detect collision events, so you could set up a listener such that whenever a ball hits the sensor you plays the audio “win” sound, destroy the ball, or whatever you want it to do. 

Another way would be to set up a runtime listener to check the y position of each ball every frame.  If a ball’s y position is > 800 then you could call the function to play the win sound.

Thanks, I used the Runtime – it works!

the isSensor – I don’t know how to do that yet. – if you have a sample I will really appreciate it.

also which of those two ways will be “better”

The isSensor is simply a property of the physics object.  In your case you could have a “floor” physics object and set floor.isSensor = true.  Now when the balls collide with the floor you will get normal collision events firing which you can detect with a listener attached to the floor.  If a collision is detected you know to use the win sequence function.  You can also do this with an physics object floor that isn’t a sensor, but then the balls will actually stop or bounce when they hit the floor which may not be the effect you want.

In terms of what’s “better” I usually go with the solution that I know how to get working!  In your case it sounds like you were able to get the first way to work, so then I would say, “good enough” and move on to other problems.  You can spend a lot of time trying to optimize your code, or be smart/clever/elegant with it, but gain no noticeable improvement in the final effect.  If you are coding by yourself, as long as you understand what you’re doing and it works then that’s all you need, imo.  (Of course, if other people will read or use your code that might be a very good reason to clean things up. )