Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Define Images Correctly
Started by andrew2212 Oct 27 2018 05:23 AM

5 replies to this topic
image display

Best Answer nick_sherman , 27 October 2018 - 07:53 AM

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:

 

 
local background = display.newImageRect(...)
local title = display.newImageRect(...)
local kitchenSink = display.newImageRect(...)
 
-- 3 variables, 1 per image
 

 

You can do:

 

 
local images = {}
 
images.background = display.newImageRect(...)
images.title = display.newImageRect(...)
images.kitchenSink = display.newImageRect(...)
 
-- always 1 variable
 

 

 

To create images via a function you would do:

 

 
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)
 

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

andrew2212

[GLOBAL: userInfoPane.html]
andrew2212
  • Enthusiast

  • 50 posts
  • Corona SDK

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)


[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,610 posts
  • Corona SDK

  Best Answer

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:

 

 
local background = display.newImageRect(...)
local title = display.newImageRect(...)
local kitchenSink = display.newImageRect(...)
 
-- 3 variables, 1 per image
 

 

You can do:

 

 
local images = {}
 
images.background = display.newImageRect(...)
images.title = display.newImageRect(...)
images.kitchenSink = display.newImageRect(...)
 
-- always 1 variable
 

 

 

To create images via a function you would do:

 

 
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)
 



[TOPIC: post.html]
#3

andrew2212

[GLOBAL: userInfoPane.html]
andrew2212
  • Enthusiast

  • 50 posts
  • Corona SDK

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.


  • nick_sherman likes this

[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,286 posts
  • Corona SDK

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 } )

Edited by roaminggamer, 27 October 2018 - 08:30 AM.


[TOPIC: post.html]
#5

andrew2212

[GLOBAL: userInfoPane.html]
andrew2212
  • Enthusiast

  • 50 posts
  • Corona SDK

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!!



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,286 posts
  • Corona SDK

@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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: image, display