Corona Simulator crashes. Problem with objectCount and physics.

Only been doing this a few months.

I am trying to display 8 different objects to create a room, all using the same png file.

I tried displaying normally:

    (local platform2

          platform2 = display.newImageRect( “platform.png”, 700, 20)

          platform2.x = display.contentHeight - 100

          platform2.y = display.contentHeight - 10

          physics.addBody( platform2, “static”)

and it worked fine for all 8.

but I am trying to condense this, as doing that for each one seems to long.

So I tried the following and it crashes every time I try to save.

Thus it will not display anything.

It can take the first 2 or 3 platforms but than it crashes

I am probably doing this the hard way.

Thank you for any assistance.

      local platform = {}

           local objectCount = 8

     – Platform hovering on the right

           for i = 1, objectCount do

              platform[i] = display.newImageRect(“platform.png”, 100, 20)

              platform[1].x = 330

              platform[1].y = 160

     --Platform hovering on the left

           for i = 2, objectCount do

              platform[i] = display.newImageRect(“platform.png”, 100, 20)

              platform[2].x = 140

              platform[2].y = 160

     --Platform on the top(Ceiling)

           for i = 3, objectCount do

              platform[i] = display.newImageRect(“platform.png”, 600, 20)

              platform[3].x = 250

              platform[3].y = 0

     --Right Wall

            for i = 4, objectCount do

              platform[i] = display.newImageRect(“platform.png”, 320, 20)

              platform[4].x = 520

              platform[4].y = 160

              platform[4].rotation = 90

       --Left Wall 

             for i = 5, objectCount do

                platform[i] = display.newImageRect(“platform.png”, 100, 20)

                platform[5].x = -40

                platform[5].y = 50

        --Tilted platform on the left

             for i = 6, objectCount do

                platform[i] = display.newImageRect(“platform.png”, 200, 20)

                platform[6].x = 30

                platform[6].y = 250

                platform[6].rotation = 35

        --Tilted platform on the right

             for i = 7, objectCount do

                platform[i] = display.newImageRect(“platform.png”, 200, 20)

                platform[7].x = 450

                platform[7].y = 250

                platform[7].rotation = -35

        --Platform hovering on the bottom(Floor)

             for i = 8, objectCount do

                platform[i] = display.newImageRect(“platform.png”, 700, 20)

                platform[8].x = -100

                platform[8].y = -10

      physics.addBody(platform[1], “static”)

      physics.addBody(platform[2], “static”)

      physics.addBody(platform[3], “static”)

      physics.addBody(platform[4], “static”)

      physics.addBody(platform[5], “static”)

      physics.addBody(platform[6], “static”)

      physics.addBody(platform[7], “static”)

      physics.addBody(platform[8], “static”)

end

end

end

end

end

end

end

end

It seems you have nested loops. In each loop you have the same variable i. Why you don’t try like that

local coords = { {330, 160}, ... } local sizes = { {100, 20}, ... } local w, h, x, y  local platform = {} local objectCount = 8 for i = 1, objectCount do               x = coords[i][1]               y = coords[i][2]               w = sizes[i][1]               h = sizes[i][2]                               platform[i] = display.newImageRect("platform.png", w, h)               platform[i].x, platform[1].y = x ,y               physics.addBody(platform[i], "static") end 

           

  

Thank you very much ldurniat

I have been trying it as you suggest but it is unfamiliar to me and says “attempt to index ‘?’(a number value)”

at line 141.

local coords = { {330, 160}, 140, 160}

local sizes = { {100,20}, 100, 120}

local w, h, x, y

local platform = {}

local objectCount = 2

for i = 1, objectCount do 

           x = coords[i][1]  ---------------this is line 141

           y = coords[i][2]

           w = sizes[i][1]

           h = sizes[i][2]

           platform[i] = display.newImageRect(“platform.png”, w,h)

           platform[i].x, platform[1].y = x, y

           physics.addBody(platform[i], “static”)

end

In my code coords  and sizes are tables which elements are also tables. So they need to be rewrite as follow

local coords = { {330, 160}, {140, 160} } local sizes = { {100,20}, {100, 120} }

It should work now. You can read more about tables on Corona documentation.

Yes it works now. Awesome. I will read up on tables. Thank you for the help.

It seems you have nested loops. In each loop you have the same variable i. Why you don’t try like that

local coords = { {330, 160}, ... } local sizes = { {100, 20}, ... } local w, h, x, y  local platform = {} local objectCount = 8 for i = 1, objectCount do               x = coords[i][1]               y = coords[i][2]               w = sizes[i][1]               h = sizes[i][2]                               platform[i] = display.newImageRect("platform.png", w, h)               platform[i].x, platform[1].y = x ,y               physics.addBody(platform[i], "static") end 

           

  

Thank you very much ldurniat

I have been trying it as you suggest but it is unfamiliar to me and says “attempt to index ‘?’(a number value)”

at line 141.

local coords = { {330, 160}, 140, 160}

local sizes = { {100,20}, 100, 120}

local w, h, x, y

local platform = {}

local objectCount = 2

for i = 1, objectCount do 

           x = coords[i][1]  ---------------this is line 141

           y = coords[i][2]

           w = sizes[i][1]

           h = sizes[i][2]

           platform[i] = display.newImageRect(“platform.png”, w,h)

           platform[i].x, platform[1].y = x, y

           physics.addBody(platform[i], “static”)

end

In my code coords  and sizes are tables which elements are also tables. So they need to be rewrite as follow

local coords = { {330, 160}, {140, 160} } local sizes = { {100,20}, {100, 120} }

It should work now. You can read more about tables on Corona documentation.

Yes it works now. Awesome. I will read up on tables. Thank you for the help.