Define Images Correctly

Hi I am rewriting my game and one of the big challenges I face is with images.  Most of my screens have at least 50 to 60 images which takes up a lot of my 200 max variables and a lot of space in the code.  There has to be a better way no?  I tried with a function but it doesnt work, any ideas?  Maybe both ways I do things are wrong!!!  Any help is immensely appreciated.

Currently I make  images like below:

local dW = display.contentWidth local dH = display.contentHeight local cX = display.contentCenterX local cY = display.contentCenterY local allObjects = {} bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) local background = display.newImageRect(bgGroup, "background.png", dW\*3, dH ) local bottomBackground = display.newImageRect(bgGroup, "bottomBackground.png", dW\*.75, dW\*.22) local title = display.newImageRect(bgGroup, "title.png", dW\*.456, dW\*.19) table.insert(allObjects,background) table.insert(allObjects,bottomBackground) table.insert(allObjects,title) background.x=cX ; background.y=cY bottomBackground.x = cX ; bottomBackground.y = dH\*.85 title.x = cX ; title.y = dH\*.26

Function im trying:

I guess its not working too well because of the way i am passing variables to createImage function, in particular “varName” and “dispGroup”.  In some of my game I create images with the local in the function and it works but this is done in a strategic way using a loop.  These are more of static images and I need to call them later so defining the local within the function is not too strategic.  I get I can call the images by a number in a table if I do define it within the function but this is hard to keep track of.

--writing overall idea real quick to give general concept local dW = display.contentWidth local dH = display.contentHeight local cX = display.contentCenterX local cY = display.contentCenterY local allObjects = {} local background, bottomBackground, title bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) local function createImage(varName, dispGroup, imageLoc, xSize, ySize, xLoc, yLoc) varName = display.newImageRect(dispGroup, imageLoc, xSize, ySize ) table.insert(allObjects,varName) varName.x=xLoc ; varName.y=yLoc end createImage(background, bgGroup, "background.png", dW\*3, dH, cX, cY) createImage(bottomBackground, bgGroup, "bottomBackground.png", dW\*.456, dW\*.19, cX, dH\*.85)

Firstly, you get around the max variables problem by storing everything in a table, using keys instead of numerically as you currently do.

Instead of:

[lua]

local background = display.newImageRect(…)

local title = display.newImageRect(…)

local kitchenSink = display.newImageRect(…)

– 3 variables, 1 per image

[/lua]

You can do:

[lua]

local images = {}

images.background = display.newImageRect(…)

images.title = display.newImageRect(…)

images.kitchenSink = display.newImageRect(…)

– always 1 variable

[/lua]

To create images via a function you would do:

[lua]

local images = {}

local function createImage(dispGroup, imageLoc, xSize, ySize, xLoc, yLoc)

  local i = display.newImageRect(dispGroup, imageLoc, xSize, ySize )

  i.x = xLoc

  i.y = yLoc 

 return i

end

images.background = createImage(bgGroup, “background.png”, dW*3,dH, cX,cY)

images.bottomBackground = createImage(bgGroup, “bottomBackground.png”, dW*0.456, dW *0.19, cX, dH*0.85)

[/lua]

Hey Nick, WOOOW I love this, so incredible.  I only store images in a table so I have a way of destroying them after.  This is soooo much better and cleaner, specifically referring to ex. 2 and 3 I love it.  This leaves me with the same destroy process but much cleaner way of making images.  I really appreciate this post.

Tip: If you were an SSK user, this code:

local dW = display.contentWidth local dH = display.contentHeight local cX = display.contentCenterX local cY = display.contentCenterY local allObjects = {} bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) local background = display.newImageRect(bgGroup, "background.png", dW\*3, dH ) local bottomBackground = display.newImageRect(bgGroup, "bottomBackground.png", dW\*.75, dW\*.22) local title = display.newImageRect(bgGroup, "title.png", dW\*.456, dW\*.19) table.insert(allObjects,background) table.insert(allObjects,bottomBackground) table.insert(allObjects,title) background.x=cX ; background.y=cY bottomBackground.x = cX ; bottomBackground.y = dH\*.85 title.x = cX ; title.y = dH\*.26

would become this:

local allObjects = {} local bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) local newImageRect = ssk.display.newImageRect allObjects[#allObjects+1] = newImageRect( bgGroup, centerX, centerY, "background.png", { w = w \* 3, h = h } ) allObjects[#allObjects+1] = newImageRect( bgGroup, centerX, h \* 0.85, "bottomBackground.png", { w = w \* 0.75, h = w \* 0.22 } ) allObjects[#allObjects+1] = newImageRect( bgGroup, centerX, h \* 0.26, "title.png", { w = w \* 0.456, h = w \* 0.19 } )

Hey roaminggamer nice to hear from you again :)  .  I saw you posted somewhere else a couple days ago a little about ssk but didnt know it was yours.  Will take a look into this, this weekend.  Thanks as always.  BTW just started to recode with the modular approach, working pretty cool so far!!

@andrew2212,

Be warned,  SSK is (in some places) a little messy and even ugly.  It has this history:

  • It started as SSK for Torque back in about 2002
  • I brought the overall concept, but none of the code to Corona in 2012 as SSK 1
  • It went through a period of shrinking, growing, and splitting into discrete libraries.  This was a nightmare to maintain.
  • Corona 1.0 transitioned to 2.0 and I recombined SSK 1 to a single base.
  • All this time, there was NO documentation.
  • I revamped the project, documented it HERE, and re-released it as SSK 2 lite and SSK 2 Pro (both paid)
  • Less than one year later, I decided not enough people were able to use it and went back to the free model, removing lite and putting the Pro version back on GitHub.
  • Since then I have continued to add to it and extend it, primarily for myself.

At this point, the docs are out-of-date, that is, there are more sub-libraries and features than I have documented.  

Having said all that, I use this for all my paid, free, and personal projects.  It is very solid and highly functional  If you invest the time to start using it and learning it, I think your time will be re-paid 10X.

Firstly, you get around the max variables problem by storing everything in a table, using keys instead of numerically as you currently do.

Instead of:

[lua]

local background = display.newImageRect(…)

local title = display.newImageRect(…)

local kitchenSink = display.newImageRect(…)

– 3 variables, 1 per image

[/lua]

You can do:

[lua]

local images = {}

images.background = display.newImageRect(…)

images.title = display.newImageRect(…)

images.kitchenSink = display.newImageRect(…)

– always 1 variable

[/lua]

To create images via a function you would do:

[lua]

local images = {}

local function createImage(dispGroup, imageLoc, xSize, ySize, xLoc, yLoc)

  local i = display.newImageRect(dispGroup, imageLoc, xSize, ySize )

  i.x = xLoc

  i.y = yLoc 

 return i

end

images.background = createImage(bgGroup, “background.png”, dW*3,dH, cX,cY)

images.bottomBackground = createImage(bgGroup, “bottomBackground.png”, dW*0.456, dW *0.19, cX, dH*0.85)

[/lua]

Hey Nick, WOOOW I love this, so incredible.  I only store images in a table so I have a way of destroying them after.  This is soooo much better and cleaner, specifically referring to ex. 2 and 3 I love it.  This leaves me with the same destroy process but much cleaner way of making images.  I really appreciate this post.

Tip: If you were an SSK user, this code:

local dW = display.contentWidth local dH = display.contentHeight local cX = display.contentCenterX local cY = display.contentCenterY local allObjects = {} bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) local background = display.newImageRect(bgGroup, "background.png", dW\*3, dH ) local bottomBackground = display.newImageRect(bgGroup, "bottomBackground.png", dW\*.75, dW\*.22) local title = display.newImageRect(bgGroup, "title.png", dW\*.456, dW\*.19) table.insert(allObjects,background) table.insert(allObjects,bottomBackground) table.insert(allObjects,title) background.x=cX ; background.y=cY bottomBackground.x = cX ; bottomBackground.y = dH\*.85 title.x = cX ; title.y = dH\*.26

would become this:

local allObjects = {} local bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) local newImageRect = ssk.display.newImageRect allObjects[#allObjects+1] = newImageRect( bgGroup, centerX, centerY, "background.png", { w = w \* 3, h = h } ) allObjects[#allObjects+1] = newImageRect( bgGroup, centerX, h \* 0.85, "bottomBackground.png", { w = w \* 0.75, h = w \* 0.22 } ) allObjects[#allObjects+1] = newImageRect( bgGroup, centerX, h \* 0.26, "title.png", { w = w \* 0.456, h = w \* 0.19 } )

Hey roaminggamer nice to hear from you again :)  .  I saw you posted somewhere else a couple days ago a little about ssk but didnt know it was yours.  Will take a look into this, this weekend.  Thanks as always.  BTW just started to recode with the modular approach, working pretty cool so far!!

@andrew2212,

Be warned,  SSK is (in some places) a little messy and even ugly.  It has this history:

  • It started as SSK for Torque back in about 2002
  • I brought the overall concept, but none of the code to Corona in 2012 as SSK 1
  • It went through a period of shrinking, growing, and splitting into discrete libraries.  This was a nightmare to maintain.
  • Corona 1.0 transitioned to 2.0 and I recombined SSK 1 to a single base.
  • All this time, there was NO documentation.
  • I revamped the project, documented it HERE, and re-released it as SSK 2 lite and SSK 2 Pro (both paid)
  • Less than one year later, I decided not enough people were able to use it and went back to the free model, removing lite and putting the Pro version back on GitHub.
  • Since then I have continued to add to it and extend it, primarily for myself.

At this point, the docs are out-of-date, that is, there are more sub-libraries and features than I have documented.  

Having said all that, I use this for all my paid, free, and personal projects.  It is very solid and highly functional  If you invest the time to start using it and learning it, I think your time will be re-paid 10X.