help: workflow with random tile generation

Hello Corona forums! :slight_smile:

I’d like to ask a question about my methodology. I’m new to Corona and LUA but have some hobbyist background in programming (mainly simple stuff, never truly grasped Object Oriented Programming idea).

I’m in the middle of making an endless runner game and came across a problem. My code started to be difficult to read so i’ve decided to split it to some external modules. But the thing is, with the workflow i created it’s difficult to do that (or at least i think so).

Here’s the fragment of the code:

function drawPavements( max ) for a = 1, max, 1 do local numGen = math.random(3) local newPavement if(numGen == 1) then newPavement = display.newImage("data/pavementA\_01.png") elseif(numGen == 2) then newPavement = display.newImage("data/pavementA\_02.png") elseif(numGen == 3) then newPavement = display.newImage("data/pavementA\_03.png") end --newPavement.blendMode = "multiply" newPavement.anchorX, newPavement.anchorY = 0,1 newPavement.x, newPavement.y = -500, -500 newPavement:setFillColor( math.random(60,120)/255, math.random(60,120)/255, math.random(60,120)/255 ) pavementList:insert(newPavement) end end function placePavementTile(level, scroll) local numGen = math.random(pavementList.numChildren) --random from pavementList length local object = pavementList[numGen] --assign to "object" pavementList:remove(numGen) --remove the tile from pavementList if (level == "A") then object.objType = "levelA" --set coordinates of the extracted tile object.x, object.y = scroll, 670 --put the extracted tile at the end of the pavementsLevelA pavementsLevelA:insert(object) elseif (level == "B") then object.objType = "levelB" object.x, object.y = scroll, 400 pavementsLevelB:insert(object) elseif (level == "C") then object.objType = "levelC" object.x, object.y = scroll, 130 pavementsLevelC:insert(object) end end function generateLevelA(tiles) local scr = -200 for a=1, tiles, 1 do scr = pTileSize + scr placePavementTile("A", scr) end end function updateAllPavementTiles() --UPDATE ALL PAVEMENT TILES --LEVEL A if (pavementsLevelA.numChildren \> 0) then r\_tileX, r\_tileY = updatePavementTiles("A", screenLimit) if r\_tileX ~= nil then placePavementTile("A", 5\*pTileSize + r\_tileX - speed) end end --LEVEL B if (pavementsLevelB.numChildren \> 0) then updatePavementTiles("B", screenLimit) end --LEVEL C if (pavementsLevelC.numChildren \> 0) then updatePavementTiles("C", screenLimit) end end pavementList = display.newGroup() pavementsLevelA = display.newGroup() pavementsLevelB = display.newGroup() pavementsLevelC = display.newGroup() drawPavements(36) generateLevelA(8) Runtime:addEventListener( "enterFrame", updateAllPavementTiles )

There are 3 “levels” on which the character can jump.

I’m juggling “pavementList” elements between “pavementLevelA, B and C” so if they exit the screen limit they return to the original list - “pavementList” so, they can be used again in the random process of generating one tile at the beginning of each “level”.

So the questions are:

  1. Is it the right way to do something like that, or can this be done in the simpler/more optimised fashion?

  2. With that code in mind, how can i split it to other modules, so that for example i have “drawPavements()” in “drawing.lua” module with other drawing stuff, “updatePavements()” in “gameLoop.lua” module, and “placePavements()” in placement.lua. - I.E. how to share the 4 display groups between those modules?

Any hints? :slight_smile:

Cheers!

Wojtek

Those are two pretty big questions to parse. I think your first question is if the above the most optimized way to accomplish the endless runner logic. If that is your question, here are some resources for endless runner logic within Corona:

http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-side-scroller-from-scratch/

https://coronalabs.com/blog/2010/12/10/ghosts-vs-monsters-open-source-game-in-corona-sdk/ (see the cloud code for solid repeating code info)

There is more than one way to accomplish this, so what you find useful might not be what another individual would come up with. If you aren’t seeing memory leaks and you’re happy with how the behavior appears, then you’re good to go! I’ll say this, it’s much easier to just have a gameloop checking for how far away your “ground” physical objects are from the bounds of the device, and then moving them back to the “beginning” of the level.

The second link is Ghosts vs. Monsters, which is a full game with storyboard and external module implementations, which is a great template to show the best way to move forward with a lot of interesting logic in Lua (parallax, repeat code, sprite interactions, collisions, game loops, storyboard utilization).

Hey Panc!

Thanks for the reply! I’ve done that Side Scroller tutorial on “tutplus”, in fact my code is extended from that exact tutorial :slight_smile: Also, thanks a lot for that second link, i’ll definetely look throught that code and learn something useful.

I did some research as well, dig here on forums and the web. Finally I found the key words “object pooling”, googled it and found a great deal of info, plus eventually stumbled upon this awesome tutorial:

http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

So now I somewhat “have an idea” how to slowly walk into an Object Oriented world :slight_smile:

Cheers!

Wojtek

Those are two pretty big questions to parse. I think your first question is if the above the most optimized way to accomplish the endless runner logic. If that is your question, here are some resources for endless runner logic within Corona:

http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-side-scroller-from-scratch/

https://coronalabs.com/blog/2010/12/10/ghosts-vs-monsters-open-source-game-in-corona-sdk/ (see the cloud code for solid repeating code info)

There is more than one way to accomplish this, so what you find useful might not be what another individual would come up with. If you aren’t seeing memory leaks and you’re happy with how the behavior appears, then you’re good to go! I’ll say this, it’s much easier to just have a gameloop checking for how far away your “ground” physical objects are from the bounds of the device, and then moving them back to the “beginning” of the level.

The second link is Ghosts vs. Monsters, which is a full game with storyboard and external module implementations, which is a great template to show the best way to move forward with a lot of interesting logic in Lua (parallax, repeat code, sprite interactions, collisions, game loops, storyboard utilization).

Hey Panc!

Thanks for the reply! I’ve done that Side Scroller tutorial on “tutplus”, in fact my code is extended from that exact tutorial :slight_smile: Also, thanks a lot for that second link, i’ll definetely look throught that code and learn something useful.

I did some research as well, dig here on forums and the web. Finally I found the key words “object pooling”, googled it and found a great deal of info, plus eventually stumbled upon this awesome tutorial:

http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

So now I somewhat “have an idea” how to slowly walk into an Object Oriented world :slight_smile:

Cheers!

Wojtek