Understanding Loops

I’m trying to better understanding setting up a loop to join objects.  Right now I have them individually (see a small clip code below).  

I know there has to be a shorter why using the i = 1, 3 do type of code.  I’ve tried to set it up but no luck.  

My actual link will have more than three links but I thought this would work for example purposes.

I’d appreciate any help in setting up this loop.   Thanks  Lori  :rolleyes:


   --LINKS


local myCircleA = display.newImage (“Images/rockLink.png”)

myCircleA.x = 105; myCircleA.y = 370

 physics.addBody(myCircleA,“static”,{radius=10, bounce=0.5, friction =0.953})

local myCircleB = display.newImage (“Images/rockLink.png”)

myCircleB.x = 180; myCircleB.y = 280

  physics.addBody(myCircleB,“static”,{radius=10, bounce=0.5, friction =0.953})

    -------------------------------------

  local linkA = display.newImage(“Images/link.png”)

  linkA.x = 110; linkA.y=360

  linkA.rotation=45 

  physics.addBody( linkA, { density=1.0, friction=0.5, bounce=0.5 } )

  physics.newJoint( “pivot”, linkA, myCircleA, myCircleA.x, myCircleA.y)

  -------------------------------------

  local linkB=display.newImage(“Images/link.png”)

  linkB.x = 120; linkB.y=348

  linkB.rotation=45 

  physics.addBody( linkB, { density=1.0, friction=0.5, bounce=0.5 } )

  physics.newJoint( “pivot”, linkB, linkA, linkA.x, linkA.y)

  ---------------------------------------

  local linkC=display.newImage(“Images/link.png”)

  linkC.x = 130; linkC.y=336

  linkC.rotation=45 

  physics.addBody( linkC, { density=1.0, friction=0.5, bounce=0.5 } )

  physics.newJoint( “pivot”, linkC, linkB, linkB.x, linkB.y)

Ultimately, you really want the distances between the links to be regular so that using a loop allows you to easily repeat the same values. You would also want to put all the objects being linked into a display group so you can loop through them without having to explicitly name them. Though it’s not exactly your code (images left out, for example) this is how I would create a series of circles and link them with pivot joints to create a chain:

local rope = display.newGroup() local function newLink( group, x, y, xGap, yGap, r ) local link = display.newCircle( group, x, y, r ) physics.addBody( link, "dynamic" ) if (group.numChildren \> 1) then local prev = group[group.numChildren-1] link.joint = physics.newJoint( "pivot", prev, link, x+xGap, y+yGap ) end end local x, y, xGap, yGap, r = 100, 100, 25, 0, 30 for i=1, 10 do newLink( rope, x, y, r ) x, y = x+xGap, y+yGap end

Disclaimer: I’ve not run this code.

I have a png that I am using to link together.  Is it possible to  use the picture instead of creating a circle?

Thank you for responding.

Yes, just swap out the newCircle for the newImage

<lgalcott>: Another possible way would be to do something like:

local circle = {}; for i = 1, 3 do circle[i]= display.newImage ("Images/rockLink.png") circle[i].x = 105; circle[i] = 370 &nbsp;physics.addBody(circle[i],"static",{radius=10, bounce=0.5, friction =0.953}) end

I haven’t run the above code and it’s incomplete, and as <horacebury> says you should insert into a display group, but it might give you an idea.

And pay attention to variable scopes.

I will give this a try.

Thank you for your response!  Have a great day.

Lori

Ultimately, you really want the distances between the links to be regular so that using a loop allows you to easily repeat the same values. You would also want to put all the objects being linked into a display group so you can loop through them without having to explicitly name them. Though it’s not exactly your code (images left out, for example) this is how I would create a series of circles and link them with pivot joints to create a chain:

local rope = display.newGroup() local function newLink( group, x, y, xGap, yGap, r ) local link = display.newCircle( group, x, y, r ) physics.addBody( link, "dynamic" ) if (group.numChildren \> 1) then local prev = group[group.numChildren-1] link.joint = physics.newJoint( "pivot", prev, link, x+xGap, y+yGap ) end end local x, y, xGap, yGap, r = 100, 100, 25, 0, 30 for i=1, 10 do newLink( rope, x, y, r ) x, y = x+xGap, y+yGap end

Disclaimer: I’ve not run this code.

I have a png that I am using to link together.  Is it possible to  use the picture instead of creating a circle?

Thank you for responding.

Yes, just swap out the newCircle for the newImage

<lgalcott>: Another possible way would be to do something like:

local circle = {}; for i = 1, 3 do circle[i]= display.newImage ("Images/rockLink.png") circle[i].x = 105; circle[i] = 370 &nbsp;physics.addBody(circle[i],"static",{radius=10, bounce=0.5, friction =0.953}) end

I haven’t run the above code and it’s incomplete, and as <horacebury> says you should insert into a display group, but it might give you an idea.

And pay attention to variable scopes.

I will give this a try.

Thank you for your response!  Have a great day.

Lori