creating circles that drop

ok I have the following

local mycircle = display.newCircle(50,50, 25)

group:insert(mycircle)

physics.addBody(group, {density=0.01, radius=25, bounce=0.0})

Now, the circle DOES drop, but so does an ADDITIONAL circle and it is brown!  A circle created at 0,0.  I found out it was created by the last item.  If I take out radius=25 then a large brown rectangle centered at 0,0 is created and then it falls as well as my circle!  This is frustrating.  I copied it directly from your addBody, so I am confused what is going  on.

Few more questions… if I may

What if I will have LOTS of these circles and I want to do something when they are clicked.  Can you tell me what I need to look up to make them clickable?  Just tell me what to research and I will ask more questions if I can’t figure out how to use it.  Thanks!

Thanks!

I also tried transition_to and it worked GREAT!  The only problem I had with it, was I was UNABLE to figure out how to make the object drop to the bottom of the screen when I created the objects off screen (ie -40, -100, etc)  I used _H but then it would only go to _H-40, _H-100, etc…

Why not have an absolute X, Y you can make it transition to?  Or am I missing it?  I looked at the names of all the methods and function in both object and transition_to but nothing seemed to work.

Any way I can use physics engine because it worked great but don’t use gravity, just use velocity?  Ie I don’t want it to keep speeding up.

I looked in Physics but couldn’t find anything dealing with velocity?  That seemed strange.  Like I should be able to let’s say create asteroids moving at a certain speed.  Maybe I am missing a whole bunch of functions I don’t see. :frowning:

Basically I want my objects to fall to bottom of the screen and do something when they hit the bottom.  I am enjoying this just sometimes it seems frustrating when I can’t find things that I expect to find, like velocity in Physics.  Hope I am missing it somewhere.

hmmm my transition_to didn’t work as well as I hoped.  It seemed to use my last value.

example:

for j=1, 120 do

   for i=1, 4 do

   value=i*1000+j

   mycircle=display.newcircle(60*i, -60*j, 30)

   text=display.newText(i, 60*i, -60*j, Helvetica, 14)

   mycircle.id=value

   group:insert(mycircle)

   group:insert(text)

   --physics.addBody(group, {density=0.1, radius=30, bounce=0.0}) **

   transition.to(group, {time=15000, alpha=1, x=0, y=_H+60*j})   ***

   end

end

***I noticed that it seems to apply the transition to all of them instead of each one separately.  Because they all moved VERY fast.  If I changed that to y=_H then moved slow but then again won’t move off the screen - and only some of the elements will be seen.

** as you can imagine with my y values going so far negative, no matter how small I make my gravity or my density by the end the balls are moving very small.  I noticed if I make gravity 0.01 it doesn’t move - I guess it gets rounded to 0???  Is that a bug or a “feature”?

Thanks for your help.  Hope you can see I am “trying” on my own.

Ultimate goal have them move down the screen to the bottom and when they touch bottom do something, like play a sound.

***

To answer your first question (and your problem in post 3), you added the body to the group rather than the object, so that entire group will fall rather than just that circle. Here’s how you could create lots of circles and detect which one has been pressed:

[lua]

local circles = {}      – set up a table to hold all the circles

     

local touchCircle = function (event)      – touch handler for all circles

      local obj = event.target      – obj will be the circle that was pressed

      local cid = obj.id                  – cid will be the ID of the circle that was pressed

     

      if event.phase == “began” then            – when circle first pressed

            print (“CIRCLE NUMBER " …c id. .” WAS CLICKED!")        – print a message in console

      end

end

local createCircle(group, xCenter, yCenter, radius)         – function to spawn a new circle

      local c = display.newCircle(group, xCenter, yCenter, radius)            – draw circle object, assign to c

      physics.addBody(c, {density = 0.01 , radius = 25 , bounce = 0.0 })   – give circle a physics body

      c:addEventListener(“touch”, touchCircle)        – add the touch listener to make it clickable

      c.id = #circles + 1            – set the id of the circle.

      circles[#circles+ 1] = c       – add the new circle to the circles table

end

createCircle(group, 50 , 50 , 25 )           – command to spawn a new circle

[/lua]

For physics velocities setLinearVelocity is what you need (bottom of this page)

http://docs.coronalabs.com/api/type/Body/index.html

Do you have this line of code in your app:
 

physics.setDrawMode( “hybrid” )

If so, you’re telling physics to draw the physics shape it’s using over top of the object. The allows you to see how your physics shapes are interacting with each other.  The graphic that goes with the image isn’t actually used, just the shape defined with it.  In other words an image of a person will be a rectangle as it’s physics shape.  The hybrid draw mode lets you see that.  They will manifest themselves and green and brown shapes.

Rob

Thanks Rob and Nick!!!

Nick, I just finished implementing (fixed a couple small typos) what you gave me and it worked wonders!   Thank you SOOO much!

I appreciate your help VERY much.

Thanks!

I also tried transition_to and it worked GREAT!  The only problem I had with it, was I was UNABLE to figure out how to make the object drop to the bottom of the screen when I created the objects off screen (ie -40, -100, etc)  I used _H but then it would only go to _H-40, _H-100, etc…

Why not have an absolute X, Y you can make it transition to?  Or am I missing it?  I looked at the names of all the methods and function in both object and transition_to but nothing seemed to work.

Any way I can use physics engine because it worked great but don’t use gravity, just use velocity?  Ie I don’t want it to keep speeding up.

I looked in Physics but couldn’t find anything dealing with velocity?  That seemed strange.  Like I should be able to let’s say create asteroids moving at a certain speed.  Maybe I am missing a whole bunch of functions I don’t see. :frowning:

Basically I want my objects to fall to bottom of the screen and do something when they hit the bottom.  I am enjoying this just sometimes it seems frustrating when I can’t find things that I expect to find, like velocity in Physics.  Hope I am missing it somewhere.

hmmm my transition_to didn’t work as well as I hoped.  It seemed to use my last value.

example:

for j=1, 120 do

   for i=1, 4 do

   value=i*1000+j

   mycircle=display.newcircle(60*i, -60*j, 30)

   text=display.newText(i, 60*i, -60*j, Helvetica, 14)

   mycircle.id=value

   group:insert(mycircle)

   group:insert(text)

   --physics.addBody(group, {density=0.1, radius=30, bounce=0.0}) **

   transition.to(group, {time=15000, alpha=1, x=0, y=_H+60*j})   ***

   end

end

***I noticed that it seems to apply the transition to all of them instead of each one separately.  Because they all moved VERY fast.  If I changed that to y=_H then moved slow but then again won’t move off the screen - and only some of the elements will be seen.

** as you can imagine with my y values going so far negative, no matter how small I make my gravity or my density by the end the balls are moving very small.  I noticed if I make gravity 0.01 it doesn’t move - I guess it gets rounded to 0???  Is that a bug or a “feature”?

Thanks for your help.  Hope you can see I am “trying” on my own.

Ultimate goal have them move down the screen to the bottom and when they touch bottom do something, like play a sound.

***

To answer your first question (and your problem in post 3), you added the body to the group rather than the object, so that entire group will fall rather than just that circle. Here’s how you could create lots of circles and detect which one has been pressed:

[lua]

local circles = {}      – set up a table to hold all the circles

     

local touchCircle = function (event)      – touch handler for all circles

      local obj = event.target      – obj will be the circle that was pressed

      local cid = obj.id                  – cid will be the ID of the circle that was pressed

     

      if event.phase == “began” then            – when circle first pressed

            print (“CIRCLE NUMBER " …c id. .” WAS CLICKED!")        – print a message in console

      end

end

local createCircle(group, xCenter, yCenter, radius)         – function to spawn a new circle

      local c = display.newCircle(group, xCenter, yCenter, radius)            – draw circle object, assign to c

      physics.addBody(c, {density = 0.01 , radius = 25 , bounce = 0.0 })   – give circle a physics body

      c:addEventListener(“touch”, touchCircle)        – add the touch listener to make it clickable

      c.id = #circles + 1            – set the id of the circle.

      circles[#circles+ 1] = c       – add the new circle to the circles table

end

createCircle(group, 50 , 50 , 25 )           – command to spawn a new circle

[/lua]

For physics velocities setLinearVelocity is what you need (bottom of this page)

http://docs.coronalabs.com/api/type/Body/index.html

Do you have this line of code in your app:
 

physics.setDrawMode( “hybrid” )

If so, you’re telling physics to draw the physics shape it’s using over top of the object. The allows you to see how your physics shapes are interacting with each other.  The graphic that goes with the image isn’t actually used, just the shape defined with it.  In other words an image of a person will be a rectangle as it’s physics shape.  The hybrid draw mode lets you see that.  They will manifest themselves and green and brown shapes.

Rob

Thanks Rob and Nick!!!

Nick, I just finished implementing (fixed a couple small typos) what you gave me and it worked wonders!   Thank you SOOO much!

I appreciate your help VERY much.

Thanks!