Hello Corona forums!
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:
-
Is it the right way to do something like that, or can this be done in the simpler/more optimised fashion?
-
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?
Cheers!
Wojtek