Where should I insert sprites required table in Composer

Hi guys,

I have specific question, and would be glad to receive a suggestion.

I am requiring following in level.lua at top of my code:

local SpritesPlayers = require("spritePlayers") -- spritePlayers.lua is a lua file containing bunch of Players which are in form of sprites (returned as a table)

I am using it like this in scene:create :

local sprite\_info = SpritesPlayers[math.random(#SpritesPlayers)]

Previously I had a problem that when I went back to menu.lua level.lua memory was not cleared due to fact that I did not deleted “SpritesPlayers”, which took over some memory.

I solved that with following code at level.lua scene:hide  :

for i = #SpritesPlayers, 1, -1 do table.remove ( SpritesPlayers, i )     SpritesPlayers[i]=nil     end

Now I have a problem that when I go back from menu.lua to level.lua I receive an error:

“bad argument #1 to math.random (interval is empty)…”

Moving “local SpritesPlayers = require(“spritePlayers”)” from top to scene:create or scene:show does not work…

How can I solve this snag (maybe change name of SpritesPlayers xx on every menu-level-menu navigation)?

Waiting your reply.

Thanks!  :slight_smile:

Ivan

The require will only happen once, so I would make it as visible to the module as possible by requiring it at the top of the file.

Rob

Hi Rob,
This thing bugged me whole weekend, and it all started as a memory bug :slight_smile:

I understand it better now, thanks to Johnatan Beebe :slight_smile:

https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

Thanks.
Ivan

The require will only happen once, so I would make it as visible to the module as possible by requiring it at the top of the file.

Rob

Hi Rob,
This thing bugged me whole weekend, and it all started as a memory bug :slight_smile:

I understand it better now, thanks to Johnatan Beebe :slight_smile:

https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

Thanks.
Ivan