Use a function/loop to spawn objects in a curve?

I’ve written the following program to try and distribute small rectangular objects around a 90 degree curve. Each time the function is called, it spawns a new object, which is offset by a small increment in its X, Y and Rotation parameters.

For some reason, though the code runs without error, it doesn’t seem to create more than one object. If it does, then they’re all layered on top of each other and not being offset by the correct amount.

I know I could create the objects manually, but it’s more fun and a good learning excercise to spawn them with a loop. (Or at least it would be if I could get it working!)

display.setStatusBar(display.HiddenStatusBar)  
  
   
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
  
local plangroup = display.newGroup()  
local topleft = display.newRect (0, 0, 31, 34)  
topleft.x = \_W / 2 -- 160  
topleft.y = \_H / \_H + topleft.contentHeight / 2 - 1 -- 17  
topleft:setFillColor(255, 0, 0)  
topleft.alpha = .75  
plangroup:insert(topleft)  
print(topleft.x)  
  
local topright = display.newRect (0, 0, 31, 34)  
topright.x = \_W / 2   
topright.y = \_H - topright.contentHeight / 2   
topright:setFillColor(255, 0, 0)  
topright.alpha = .75  
plangroup:insert(topright)  
  
local bottommid = display.newRect (0, 0, 20, 166)  
bottommid.x = \_W / \_W + bottommid.contentWidth / 2 - 1   
bottommid.y = \_H / 2  
bottommid:setFillColor(255, 0, 0)  
bottommid.alpha = .75  
plangroup:insert(bottommid)  
  
local function createCurve()  
local hpdeg = 0  
local hpxmod = 12  
local hpx = \_W / 2 - topleft.contentWidth / 2 - hpxmod  
local hpymod = 0  
local hpy = \_H / \_H + topleft.contentHeight / 2 - 1 + hpymod  
 while hpdeg \< 90 do   
 local curveSect = display.newRect (0, 0, 12, 34)  
 curveSect:setFillColor(255, 0, 0)  
 curveSect.alpha = .75  
 plangroup:insert(curveSect)  
 curveSect.x = hpx  
 curveSect.y = hpy  
 curveSect:rotate( -9 )  
  
 local function increasevar()  
 hpxmod = hpxmod + 12  
 hpymod = hpymod + 12  
 hpdeg = hpdeg + 9  
 end  
  
 increasevar()  
  
 print(hpdeg)  
 end  
end  
createCurve()  

Thanks [import]uid: 67933 topic_id: 14044 reply_id: 314044[/import]

What device is this for? I tested it on a few and got 4 rectangles on the iPhone simulator, one of which was rotated slightly.

This tutorial is unrelated in that it is for a memory/matching game, however it lays out the tiles neatly and that part might interest you?

http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-memory-match-game/ [import]uid: 52491 topic_id: 14044 reply_id: 51723[/import]

Ah, I see what you mean.

The other 3 rectangles are; topleft, topright and bottommid. They are working fine.

I wanted to use the function “createCurve” to create 10 rectangles in a curve, which join “topleft” and “bottommid.” Each of these rectangles is to be rotated 9 degrees, so the resulting curve has 10 sections. Hope that makes sense?

Looking at it some more, the slightly rotated rectangle is darker than the others, so it appears 10 objects are being spawned on top of each other.

All I need to know is why the rectangles are not offset/rotated in increments. Is there a problem with my function, or am I thinking about it wrong?

I’ll check the link as well, thanks.
[import]uid: 67933 topic_id: 14044 reply_id: 51725[/import]

Ok, so I’ve manually set up every little rectangle to illustrate what I was after.

The code is drag and drop.

Now the rectangle is getting caught on the little edges. Is there any way to make this process easier?

display.setStatusBar(display.HiddenStatusBar)  
  
physics = require("physics")  
physics.start()  
physics.setGravity(-4,0) -- change gravity to control the jump curve.  
physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.  
   
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
  
local plangroup = display.newGroup()  
local playergroup = display.newGroup()  
local player1 = display.newRect (0, 0, 30, 30)  
player1.x = \_W / 2 + 100  
player1.y = 35 + player1.contentHeight / 2  
player1:setFillColor(0, 0, 255)  
playergroup:insert(player1)  
physics.addBody(player1, {density = 0, friction = 0, bounce = 0})  
  
function setup()  
  
local topleft = display.newRect (0, 0, 31, 34)  
topleft.x = \_W / 2 -- 160  
topleft.y = \_H / \_H + topleft.contentHeight / 2 - 1 -- 17  
topleft:setFillColor(255, 0, 0)  
topleft.alpha = .75  
physics.addBody(topleft, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(topleft)  
  
local topright = display.newRect (0, 0, 31, 34)  
topright.x = \_W / 2   
topright.y = \_H - topright.contentHeight / 2   
topright:setFillColor(255, 0, 0)  
topright.alpha = .75  
physics.addBody(topright, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(topright)  
  
local bottommid = display.newRect (0, 0, 20, 166)  
bottommid.x = \_W / \_W + bottommid.contentWidth / 2 - 1   
bottommid.y = \_H / 2  
bottommid:setFillColor(255, 0, 0)  
bottommid.alpha = .75  
physics.addBody(bottommid, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(bottommid)  
  
local sect1 = display.newRect (0, 0, 12, 34)  
sect1.x = \_W / 2 - topleft.contentWidth/ 2 - 7  
sect1.y = \_H / \_H + sect1.contentHeight / 2 - 1  
sect1:rotate ( -5 )  
sect1:setFillColor(255, 0, 0)  
sect1.alpha = .75  
physics.addBody(sect1, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect1)  
  
local sect2 = display.newRect (0, 0, 12, 34)  
sect2.x = \_W / 2 - topleft.contentWidth/ 2 - 20  
sect2.y = \_H / \_H + sect2.contentHeight / 2   
sect2:rotate ( -9 )  
sect2:setFillColor(255, 0, 0)  
sect2.alpha = .75  
physics.addBody(sect2, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect2)  
  
local sect3 = display.newRect (0, 0, 12, 34)  
sect3.x = \_W / 2 - topleft.contentWidth/ 2 - 33  
sect3.y = \_H / \_H + sect3.contentHeight / 2 + 2  
sect3:rotate ( -13 )  
sect3:setFillColor(255, 0, 0)  
sect3.alpha = .75  
physics.addBody(sect3, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect3)  
  
local sect4 = display.newRect (0, 0, 12, 34)  
sect4.x = \_W / 2 - topleft.contentWidth/ 2 - 46  
sect4.y = \_H / \_H + sect4.contentHeight / 2 + 6  
sect4:rotate ( -19 )  
sect4:setFillColor(255, 0, 0)  
sect4.alpha = .75  
physics.addBody(sect4, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect4)  
  
local sect5 = display.newRect (0, 0, 12, 34)  
sect5.x = \_W / 2 - topleft.contentWidth/ 2 - 59  
sect5.y = \_H / \_H + sect5.contentHeight / 2 + 11  
sect5:rotate ( -25 )  
sect5:setFillColor(255, 0, 0)  
sect5.alpha = .75  
physics.addBody(sect5, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect5)  
  
local sect6 = display.newRect (0, 0, 12, 34)  
sect6.x = \_W / 2 - topleft.contentWidth/ 2 - 71  
sect6.y = \_H / \_H + sect6.contentHeight / 2 + 17  
sect6:rotate ( -30 )  
sect6:setFillColor(255, 0, 0)  
sect6.alpha = .75  
physics.addBody(sect6, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect6)  
  
local sect7 = display.newRect (0, 0, 12, 34)  
sect7.x = \_W / 2 - topleft.contentWidth/ 2 - 82  
sect7.y = \_H / \_H + sect7.contentHeight / 2 + 24  
sect7:rotate ( - 36 )  
sect7:setFillColor(255, 0, 0)  
sect7.alpha = .75  
physics.addBody(sect7, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect7)  
  
local sect8 = display.newRect (0, 0, 12, 34)  
sect8.x = \_W / 2 - topleft.contentWidth/ 2 - 92  
sect8.y = \_H / \_H + sect8.contentHeight / 2 + 32  
sect8:rotate ( -40 )  
sect8:setFillColor(255, 0, 0)  
sect8.alpha = .75  
physics.addBody(sect8, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect8)  
  
local sect9 = display.newRect (0, 0, 12, 34)  
sect9.x = \_W / 2 - topleft.contentWidth/ 2 - 101  
sect9.y = \_H / \_H + sect9.contentHeight / 2 + 40  
sect9:rotate ( - 45 )  
sect9:setFillColor(255, 0, 0)  
sect9.alpha = .75  
physics.addBody(sect9, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect9)  
  
local sect10 = display.newRect (0, 0, 12, 34)  
sect10.x = \_W / 2 - topleft.contentWidth/ 2 - 109  
sect10.y = \_H / \_H + sect10.contentHeight / 2 + 49  
sect10:rotate ( -50 )  
sect10:setFillColor(255, 0, 0)  
sect10.alpha = .75  
physics.addBody(sect10, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect10)  
  
local sect11 = display.newRect (0, 0, 12, 34)  
sect11.x = \_W / 2 - topleft.contentWidth/ 2 - 117  
sect11.y = \_H / \_H + sect11.contentHeight / 2 + 59  
sect11:rotate ( -54 )  
sect11:setFillColor(255, 0, 0)  
sect11.alpha = .75  
physics.addBody(sect11, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect11)  
  
local sect12 = display.newRect (0, 0, 12, 34)  
sect12.x = \_W / 2 - topleft.contentWidth/ 2 - 124  
sect12.y = \_H / \_H + sect12.contentHeight / 2 + 70  
sect12:rotate ( - 60 )  
sect12:setFillColor(255, 0, 0)  
sect12.alpha = .75  
physics.addBody(sect12, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect12)  
  
local sect13 = display.newRect (0, 0, 12, 34)  
sect13.x = \_W / 2 - topleft.contentWidth/ 2 - 130  
sect13.y = \_H / \_H + sect13.contentHeight / 2 + 82  
sect13:rotate ( -66 )  
sect13:setFillColor(255, 0, 0)  
sect13.alpha = .75  
physics.addBody(sect13, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect13)  
  
local sect14 = display.newRect (0, 0, 12, 34)  
sect14.x = \_W / 2 - topleft.contentWidth/ 2 - 134  
sect14.y = \_H / \_H + sect14.contentHeight / 2 + 94  
sect14:rotate ( -73 )  
sect14:setFillColor(255, 0, 0)  
sect14.alpha = .75  
physics.addBody(sect14, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect14)  
  
local sect15 = display.newRect (0, 0, 12, 34)  
sect15.x = \_W / 2 - topleft.contentWidth/ 2 - 138  
sect15.y = \_H / \_H + sect15.contentHeight / 2 + 107  
sect15:rotate ( -78)  
sect15:setFillColor(255, 0, 0)  
sect15.alpha = .75  
physics.addBody(sect15, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect15)  
  
local sect16 = display.newRect (0, 0, 12, 34)  
sect16.x = \_W / 2 - topleft.contentWidth/ 2 - 140  
sect16.y = \_H / \_H + sect16.contentHeight / 2 + 120  
sect16:rotate ( -82 )  
sect16:setFillColor(255, 0, 0)  
sect16.alpha = .75  
physics.addBody(sect16, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect16)  
  
local sect17 = display.newRect (0, 0, 12, 34)  
sect17.x = \_W / 2 - topleft.contentWidth/ 2 - 141  
sect17.y = \_H / \_H + sect17.contentHeight / 2 + 133  
sect17:rotate ( -89 )  
sect17:setFillColor(255, 0, 0)  
sect17.alpha = .75  
physics.addBody(sect17, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect17)  
  
---------------------------------------------------------------------------------------------  
  
local sect18 = display.newRect (0, 0, 12, 34)  
sect18.x = \_W / 2 - topleft.contentWidth/ 2 - 7  
sect18.y = \_H - sect18.contentHeight / 2   
sect18:rotate ( 5 )  
sect18:setFillColor(255, 0, 0)  
sect18.alpha = .75  
physics.addBody(sect18, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect18)  
  
local sect19 = display.newRect (0, 0, 12, 34)  
sect19.x = \_W / 2 - topleft.contentWidth/ 2 - 20  
sect19.y = \_H - sect19.contentHeight / 2 - 1  
sect19:rotate ( 9 )  
sect19:setFillColor(255, 0, 0)  
sect19.alpha = .75  
physics.addBody(sect19, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect19)  
  
local sect20 = display.newRect (0, 0, 12, 34)  
sect20.x = \_W / 2 - topleft.contentWidth/ 2 - 33  
sect20.y = \_H - sect20.contentHeight / 2 - 3  
sect20:rotate ( 13 )  
sect20:setFillColor(255, 0, 0)  
sect20.alpha = .75  
physics.addBody(sect20, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect20)  
  
local sect21 = display.newRect (0, 0, 12, 34)  
sect21.x = \_W / 2 - topleft.contentWidth/ 2 - 46  
sect21.y = \_H - sect21.contentHeight / 2 - 6  
sect21:rotate ( 19 )  
sect21:setFillColor(255, 0, 0)  
sect21.alpha = .75  
physics.addBody(sect21, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect21)  
  
local sect22 = display.newRect (0, 0, 12, 34)  
sect22.x = \_W / 2 - topleft.contentWidth/ 2 - 59  
sect22.y = \_H - sect22.contentHeight / 2 - 11  
sect22:rotate ( 25 )  
sect22:setFillColor(255, 0, 0)  
sect22.alpha = .75  
physics.addBody(sect22, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect22)  
  
local sect23 = display.newRect (0, 0, 12, 34)  
sect23.x = \_W / 2 - topleft.contentWidth/ 2 - 71  
sect23.y = \_H - sect23.contentHeight / 2 - 17  
sect23:rotate ( 30 )  
sect23:setFillColor(255, 0, 0)  
sect23.alpha = .75  
physics.addBody(sect23, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect23)  
  
local sect24 = display.newRect (0, 0, 12, 34)  
sect24.x = \_W / 2 - topleft.contentWidth/ 2 - 82  
sect24.y = \_H - sect24.contentHeight / 2 - 24  
sect24:rotate ( 36 )  
sect24:setFillColor(255, 0, 0)  
sect24.alpha = .75  
physics.addBody(sect24, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect24)  
  
local sect25 = display.newRect (0, 0, 12, 34)  
sect25.x = \_W / 2 - topleft.contentWidth/ 2 - 92  
sect25.y = \_H - sect25.contentHeight / 2 - 32  
sect25:rotate ( 40 )  
sect25:setFillColor(255, 0, 0)  
sect25.alpha = .75  
physics.addBody(sect25, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect25)  
  
local sect26 = display.newRect (0, 0, 12, 34)  
sect26.x = \_W / 2 - topleft.contentWidth/ 2 - 101  
sect26.y = \_H - sect26.contentHeight / 2 - 40  
sect26:rotate ( 45 )  
sect26:setFillColor(255, 0, 0)  
sect26.alpha = .75  
physics.addBody(sect26, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect26)  
  
local sect27 = display.newRect (0, 0, 12, 34)  
sect27.x = \_W / 2 - topleft.contentWidth/ 2 - 109  
sect27.y = \_H - sect27.contentHeight / 2 - 49  
sect27:rotate ( 50 )  
sect27:setFillColor(255, 0, 0)  
sect27.alpha = .75  
physics.addBody(sect27, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect27)  
  
local sect28 = display.newRect (0, 0, 12, 34)  
sect28.x = \_W / 2 - topleft.contentWidth/ 2 - 117  
sect28.y = \_H - sect28.contentHeight / 2 - 59  
sect28:rotate ( 54 )  
sect28:setFillColor(255, 0, 0)  
sect28.alpha = .75  
physics.addBody(sect28, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect28)  
  
local sect29 = display.newRect (0, 0, 12, 34)  
sect29.x = \_W / 2 - topleft.contentWidth/ 2 - 124  
sect29.y = \_H - sect29.contentHeight / 2 - 70  
sect29:rotate ( 60 )  
sect29:setFillColor(255, 0, 0)  
sect29.alpha = .75  
physics.addBody(sect29, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect29)  
  
local sect30 = display.newRect (0, 0, 12, 34)  
sect30.x = \_W / 2 - topleft.contentWidth/ 2 - 130  
sect30.y = \_H - sect30.contentHeight / 2 - 82  
sect30:rotate ( 66 )  
sect30:setFillColor(255, 0, 0)  
sect30.alpha = .75  
physics.addBody(sect30, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect30)  
  
local sect31 = display.newRect (0, 0, 12, 34)  
sect31.x = \_W / 2 - topleft.contentWidth/ 2 - 134  
sect31.y = \_H - sect31.contentHeight / 2 - 94  
sect31:rotate ( 73 )  
sect31:setFillColor(255, 0, 0)  
sect31.alpha = .75  
physics.addBody(sect31, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect31)  
local sect32 = display.newRect (0, 0, 12, 34)  
sect32.x = \_W / 2 - topleft.contentWidth/ 2 - 138  
sect32.y = \_H - sect32.contentHeight / 2 - 107  
sect32:rotate ( 78)  
sect32:setFillColor(255, 0, 0)  
sect32.alpha = .75  
physics.addBody(sect32, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect32)  
  
local sect33 = display.newRect (0, 0, 12, 34)  
sect33.x = \_W / 2 - topleft.contentWidth/ 2 - 140  
sect33.y = \_H - sect33.contentHeight / 2 - 120  
sect33:rotate ( 82 )  
sect33:setFillColor(255, 0, 0)  
sect33.alpha = .75  
physics.addBody(sect33, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect33)  
  
local sect34 = display.newRect (0, 0, 12, 34)  
sect34.x = \_W / 2 - topleft.contentWidth/ 2 - 141  
sect34.y = \_H - sect34.contentHeight / 2 - 134  
sect34:rotate ( 89 )  
sect34:setFillColor(255, 0, 0)  
sect34.alpha = .75  
physics.addBody(sect34, "static", {density = 0, friction = 0, bounce = 0})  
plangroup:insert(sect34)  
  
end  
  
setup()  

Thanks in advance. [import]uid: 67933 topic_id: 14044 reply_id: 51736[/import]

For something like the spawning thing, this would do the top half (very roughly) - it’s not ideal but just something to play with if you felt like it;

[lua]local sect = {}
function spawnRects ()
for i = 1, 18 do
sect[i] = display.newRect (0, 0, 12, 34)
sect[i].x = _W / 2 - topleft.contentWidth/ 2 - i*12
sect[i].y = _H / _H + sect[i].contentHeight / 2 + i*8
sect[i]:rotate ( -i*5 )
sect[i]:setFillColor(255, 0, 0)
sect[i].alpha = .75
physics.addBody(sect[i], “static”, {density = 0, friction = 0, bounce = 0})
plangroup:insert(sect[i])
end
end
spawnRects()[/lua]

That’s based on your stuff but only loosely - anyway, I hope it helps :slight_smile:

The rectangle will get caught on edges it knicks up against, yeah - you could make it a ball or you could do a custom physics body rather than all the squares, maybe? [import]uid: 52491 topic_id: 14044 reply_id: 51856[/import]