Preloading (loading) local images and spritesheets

Hello community! I’m in the final phase of my game production and I have one simple question. How can I load LOCAL images and spritesheets before my game starts? I have already managed to make loading screen that loads images and spritesheets which are used in the whole program/code.

This question refers to loading variables that are images and spritesheets which appear in local functions.

For an example:

function someFunctionThatIsFrequentlyCalled(self, event)     if (event.other.name == "eventName") then         local someObject = sprite.newSpriteSheet("someLocalSpriteSheet.png", 40, 40)         ...         ...         ...         ...     end end  

So, how can I load “someObject” that represent sprite sheet image in my loading part of game (before start of the game)?

You can load any sprite, images you want, and hide them by using object.isVisible = false, or putting them out of the visible area.

Then, when you need them just change their position or isVisible to true

Thanks, but that variable which contains image/spritesheet isn’t a global one… So is it really possible to load it in ‘loading function’? I know about that isVisible trick but thanks :wink:

Are you saying that you have a loading function in main.lua, which loads all globals objects (which I would say you shouldn’t have but that’s another discussion), and you want to ALSO load in any objects which get used in local functions?

If the answer is yes, well first I’d ask why as you are potentially loading in images which may never be used? Why load enemy_boss_level10.png if the user never reaches level 10?

however if you really need to do it, then try following this tutorial to have “zero” global objects, while still having them accessible from any lua file:

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Using this method, you could have a single Lua file which loads all of your images, and then require that in each class. Then when you want a particular image or spritesheet just fetch it from that LUA file.

--myImages.lua local myImages = { gameSpriteSheet = newspritesheet(foo, foo, foo), menuSpriteSheet = newspritesheet(blah, blah, blah) } return myImages

Then in your loading function just load this file.

Thank you for your reply. Just to clear this up - no, I don’t have all images in loading function. Loading function just waits for images (which are being used in current ‘level’) to be ‘loaded’. 

Thanks for this one, I will go through it.

You can load any sprite, images you want, and hide them by using object.isVisible = false, or putting them out of the visible area.

Then, when you need them just change their position or isVisible to true

Thanks, but that variable which contains image/spritesheet isn’t a global one… So is it really possible to load it in ‘loading function’? I know about that isVisible trick but thanks :wink:

Are you saying that you have a loading function in main.lua, which loads all globals objects (which I would say you shouldn’t have but that’s another discussion), and you want to ALSO load in any objects which get used in local functions?

If the answer is yes, well first I’d ask why as you are potentially loading in images which may never be used? Why load enemy_boss_level10.png if the user never reaches level 10?

however if you really need to do it, then try following this tutorial to have “zero” global objects, while still having them accessible from any lua file:

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Using this method, you could have a single Lua file which loads all of your images, and then require that in each class. Then when you want a particular image or spritesheet just fetch it from that LUA file.

--myImages.lua local myImages = { gameSpriteSheet = newspritesheet(foo, foo, foo), menuSpriteSheet = newspritesheet(blah, blah, blah) } return myImages

Then in your loading function just load this file.

Thank you for your reply. Just to clear this up - no, I don’t have all images in loading function. Loading function just waits for images (which are being used in current ‘level’) to be ‘loaded’. 

Thanks for this one, I will go through it.