How to make my Game Loop Smaller

Here is my game loop:
 

local function gameLoop() local box1X, box1Y = box1:localToContent( 0,0 ) local box2X, box2Y = box2:localToContent( 0,0 ) local box3X, box3Y = box3:localToContent( 0,0 ) local box4X, box4Y = box4:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box 1") elseif rectangle.y == box2Y+10 then print("Past box 2") elseif rectangle.y == box3Y+10 then print("Past box 3") elseif rectangle.y == box4Y+10 then print("Past box 4") end end 

It checks whether the box passes another box. I wont show the code for creating the box or buttons for moving the box because it would make my post to long and complicated.

Anyways, I remembered DRY Rule and saw that I keep on repeating:

if rectangle.y == box1Y+10 then print("Past box 1")

All boxes belong to the same display group (boxGroup) so I am pretty sure I can shorten this somehow.

I tried many times on how I can shorten it but couldnt find anyway that could make it work. Does anyone know how I can shorten it so I dont have to repeat the same code over and over…

You can use a table like this :

local boxesObjects = { box1, box2, box3, box4, } local function gameLoop() local rectangle -- Need to declare this object for i=1,#boxesObjects do local box1X, box1Y = boxesObjects[i]:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box " .. i ) end end end

By the way, you might run into trouble with your logic here. Unless you are setting your Y positions manually, it is highly unlikely that the rectangle.y and box.y will be _ exactly _ 10 points away from each other. It might be a good idea to code in a range.

I tried to add a range but it led to argument issues. 

It worked after I removed local rectangle. However, they are all already in a display group. If I were to add new object TO the display group, I would also need to add them to boxesObjects table. Is it possible to add all the objects in display group instead of adding them individually like you did here: box1, box2, box3, box4,…

BTW, here is the code for creating the boxes and adding them to the display group:

local boxGroup = display.newGroup() local box1 = display.newRect(0,0,124,88) box1.x=display.contentCenterX-98 box1.y=display.contentCenterY+219 local box2 = display.newRect(0,0,124,88) box2.x=display.contentCenterX-98 box2.y=display.contentCenterY+109 local box3 = display.newRect(0,0,124,88) box3.x=display.contentCenterX-98 box3.y=display.contentCenterY local box4 = display.newRect(0,0,124,88) box4.x=display.contentCenterX-98 box4.y=display.contentCenterX-30 boxGroup:insert(box1) boxGroup:insert(box2) boxGroup:insert(box3) boxGroup:insert(box4)

Something like this gives you much greater flexibility:

[lua]

local boxGroup = display.newGroup()

local boxesData = {

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = 219 },

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = 109 },

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = 0 },

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = - 30 },

}

local boxes = {}

for i = 1 , #boxesData, 1 do

      local boxData = boxesData[i]

      local box = display.newRect( 0 , 0 ,boxData.xSize, boxData.ySize)

      box.x=display.contentCenterX + boxData.xPos

      box.y=display.contentCenterY + boxData.yPos

      boxGroup:insert(box)

      boxes[#boxes+ 1] = box

end

[/lua]

You can then loop through the boxes table within your gameLoop and prevent lots of nested ifs and elses.

But again I have to add in 

{xSize = 124, ySize = 88, xPos = -98, yPos = 219}

everytime I want to create a new box. Is it possible to just add all the objects in a display group into a table like something like this:

local boxesObjects = { boxGroup[i] }

Well yes, but how much work is it to copy and paste one line and edit the parameters? Much less than copying and editing the entire group of code that creates a box through the box1, box2, box3, box4 method, remembering to insert it into boxGroup and then adjusting your gameLoop code to account for the new box.

With this method, you could add a million boxes and it’s all automatic. If you decide to change all your boxes to an image or a circle, you just rewrite one line of code instead of having to do it for every box. DRY (Don’t repeat yourself) is a fundamental principle of programming that I’ve really embraced in recent years :slight_smile:

You can loop through the children of a display group like this:

[lua]

for i=1,group.numChildren do

    local child = group[i]

   

end

[/lua]

I have function where it creates an object and adds it to the display group, depending on how long the person plays. over 100 objects could be created.

Anyways I tried it out and I got an error. Here is what I did:

local boxesObjects = { for i=1,boxGroup.numChildren do local child = boxGroup[i] end } local function gameLoop() for i=1,#boxesObjects do local box1X, box1Y = boxesObjects[i]:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box " .. i ) end end end 

What did I do wrong?

Nevermind, I did this and it worked:

local function gameLoop() for i=1,boxGroup.numChildren do local box1X, box1Y = boxGroup[i]:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box " .. i ) end end end

Good - what you did wrong in the one above was try and put code inside of a table declaration.

You can use a table like this :

local boxesObjects = { box1, box2, box3, box4, } local function gameLoop() local rectangle -- Need to declare this object for i=1,#boxesObjects do local box1X, box1Y = boxesObjects[i]:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box " .. i ) end end end

By the way, you might run into trouble with your logic here. Unless you are setting your Y positions manually, it is highly unlikely that the rectangle.y and box.y will be _ exactly _ 10 points away from each other. It might be a good idea to code in a range.

I tried to add a range but it led to argument issues. 

It worked after I removed local rectangle. However, they are all already in a display group. If I were to add new object TO the display group, I would also need to add them to boxesObjects table. Is it possible to add all the objects in display group instead of adding them individually like you did here: box1, box2, box3, box4,…

BTW, here is the code for creating the boxes and adding them to the display group:

local boxGroup = display.newGroup() local box1 = display.newRect(0,0,124,88) box1.x=display.contentCenterX-98 box1.y=display.contentCenterY+219 local box2 = display.newRect(0,0,124,88) box2.x=display.contentCenterX-98 box2.y=display.contentCenterY+109 local box3 = display.newRect(0,0,124,88) box3.x=display.contentCenterX-98 box3.y=display.contentCenterY local box4 = display.newRect(0,0,124,88) box4.x=display.contentCenterX-98 box4.y=display.contentCenterX-30 boxGroup:insert(box1) boxGroup:insert(box2) boxGroup:insert(box3) boxGroup:insert(box4)

Something like this gives you much greater flexibility:

[lua]

local boxGroup = display.newGroup()

local boxesData = {

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = 219 },

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = 109 },

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = 0 },

{xSize = 124 , ySize = 88 , xPos = - 98 , yPos = - 30 },

}

local boxes = {}

for i = 1 , #boxesData, 1 do

      local boxData = boxesData[i]

      local box = display.newRect( 0 , 0 ,boxData.xSize, boxData.ySize)

      box.x=display.contentCenterX + boxData.xPos

      box.y=display.contentCenterY + boxData.yPos

      boxGroup:insert(box)

      boxes[#boxes+ 1] = box

end

[/lua]

You can then loop through the boxes table within your gameLoop and prevent lots of nested ifs and elses.

But again I have to add in 

{xSize = 124, ySize = 88, xPos = -98, yPos = 219}

everytime I want to create a new box. Is it possible to just add all the objects in a display group into a table like something like this:

local boxesObjects = { boxGroup[i] }

Well yes, but how much work is it to copy and paste one line and edit the parameters? Much less than copying and editing the entire group of code that creates a box through the box1, box2, box3, box4 method, remembering to insert it into boxGroup and then adjusting your gameLoop code to account for the new box.

With this method, you could add a million boxes and it’s all automatic. If you decide to change all your boxes to an image or a circle, you just rewrite one line of code instead of having to do it for every box. DRY (Don’t repeat yourself) is a fundamental principle of programming that I’ve really embraced in recent years :slight_smile:

You can loop through the children of a display group like this:

[lua]

for i=1,group.numChildren do

    local child = group[i]

   

end

[/lua]

I have function where it creates an object and adds it to the display group, depending on how long the person plays. over 100 objects could be created.

Anyways I tried it out and I got an error. Here is what I did:

local boxesObjects = { for i=1,boxGroup.numChildren do local child = boxGroup[i] end } local function gameLoop() for i=1,#boxesObjects do local box1X, box1Y = boxesObjects[i]:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box " .. i ) end end end 

What did I do wrong?

Nevermind, I did this and it worked:

local function gameLoop() for i=1,boxGroup.numChildren do local box1X, box1Y = boxGroup[i]:localToContent( 0,0 ) if rectangle.y == box1Y+10 then print("Past box " .. i ) end end end

Good - what you did wrong in the one above was try and put code inside of a table declaration.