So, i have 1 level of my game is done. I have 179 more levels to make. 98% of the code in each level will be the same. So, my question is, would it be possible to some how store all the code that would be the same in one file, and just call it for each of the 180 level files. That way, if i need to update code in the level file, i don’t have to update 180 level files, just one, template/base file
Of course, this is the basis of all coding everywhere since the 80’s. In Lua some people use the Lua approximation of classes (see the latest blog entry) but I like to use (how I think of it, at least) libraries.
So, I simply create a library which has functions and can provide operations on tables and display objects when I need it to. Typically, it will look like this:
-- utils lib local lib = {} local function changeBackgroundColour( r, g, b ) display.setDefault( "background", r/255, g/255, b/255 ) end lib.changeBackgroundColour = changeBackgroundColour return lib
However, often you want to create a series of characters or some sort of object which can behave on it’s own and have a life of it’s own. In this case you should use a function which creates a self-contained set of logic but which has it’s own functions to operate just on that one object. You can even pass in other functions, from whatever calls it, to customise the behaviour of the object. The function, in this case, is called a closure and is quite straight forward:
local function createCharacter( parent, label, x, y, onAction ) local group = display.newGroup() group.name = label group.x, group.y = x, y function group:fire() -- fire a bullet or something end -- when the group is tapped the callback function passed in as 'onAction' will get called group:addEventListener( "tap", onAction ) return group end
I heartily recommend looking at your code as units as reading the Isue #49 of From The Forum on the blog.
You can also find a whole bunch of my own libraries which are structured the way I describe on the Code Exchange:
ok, so if i have code like
local function createCharacter( parent, label, x, y, onAction ) local group = display.newGroup() group.name = label group.x, group.y = x, y function group:fire() -- fire a bullet or something end -- when the group is tapped the callback function passed in as 'onAction' will get called group:addEventListener( "tap", onAction ) return group end
how do i call the code. is it:
local Character = createCharacter(parent, label, x, y, on action)
Yes, but the “on action” should be “onAction” - that actually should be the name of a function to call.
Take a look at any listener function (sometimes called a callback) in the API docs for plenty of examples.
Of course, this is the basis of all coding everywhere since the 80’s. In Lua some people use the Lua approximation of classes (see the latest blog entry) but I like to use (how I think of it, at least) libraries.
So, I simply create a library which has functions and can provide operations on tables and display objects when I need it to. Typically, it will look like this:
-- utils lib local lib = {} local function changeBackgroundColour( r, g, b ) display.setDefault( "background", r/255, g/255, b/255 ) end lib.changeBackgroundColour = changeBackgroundColour return lib
However, often you want to create a series of characters or some sort of object which can behave on it’s own and have a life of it’s own. In this case you should use a function which creates a self-contained set of logic but which has it’s own functions to operate just on that one object. You can even pass in other functions, from whatever calls it, to customise the behaviour of the object. The function, in this case, is called a closure and is quite straight forward:
local function createCharacter( parent, label, x, y, onAction ) local group = display.newGroup() group.name = label group.x, group.y = x, y function group:fire() -- fire a bullet or something end -- when the group is tapped the callback function passed in as 'onAction' will get called group:addEventListener( "tap", onAction ) return group end
I heartily recommend looking at your code as units as reading the Isue #49 of From The Forum on the blog.
You can also find a whole bunch of my own libraries which are structured the way I describe on the Code Exchange:
ok, so if i have code like
local function createCharacter( parent, label, x, y, onAction ) local group = display.newGroup() group.name = label group.x, group.y = x, y function group:fire() -- fire a bullet or something end -- when the group is tapped the callback function passed in as 'onAction' will get called group:addEventListener( "tap", onAction ) return group end
how do i call the code. is it:
local Character = createCharacter(parent, label, x, y, on action)
Yes, but the “on action” should be “onAction” - that actually should be the name of a function to call.
Take a look at any listener function (sometimes called a callback) in the API docs for plenty of examples.