In the app that I am currently experimenting with I would like to display a random subset of 20 sprite animations out of a possible 100+. The way I have gone about it thus far is this
-
I have a root level images folder with subfolders holding the individual sprite imagery for each of the 100+ animations.
-
A second root level atlases folder is being used to store atlases - at the moment only one in which I create multiple animation groups that refer to the appropriate images/animation_subfolder/image***.png
-
A third root level prototypes folder is being used to store GO prototypes. At present I have just the one prototype there with one sprite object and a script to animate it
-
In main.collection in addition to a background game object with a suitable sprite I have another game object containing a factory that points to the prototype created above
-
The script sibling of that factory has the following code
function init(self) factory.load("#factory",afterLoadFactory); end function afterLoadFactory(this,factoryName,result) local gameWidth = tonumber(sys.get_config("display.width")) local gameHeight = tonumber(sys.get_config("display.height")) for i=1, 20 do local x1 = math.random(gameWidth) local y1 = math.random(gameHeight) local pos = vmath.vector3(x1, y1, 1) local id = factory.create("#factory", pos, nil,attribs, 1) local anim = "AnimOne" if (i % 2 == 0) then anim = "AnimTwo" end msg.post(msg.url(nil, id, "sprite"), "play_animation", { id = hash(anim) }) end
end
where AnimOne and AnimTwo are animation sequences defined in the atlas created earlier. This works - I get 10 AnimOne sprites and 10 AnimTwo sprites on my screen when I run this.
My questions
- Does this seem like a reasonable approach
- At a later stage I would like to be able to use not just one factory but one of many factories to spawn the UI content based on several criteria. While it does not seem inpossible to have multiple factories within main.collection it is not clear to me that doing so does not entail imposing an unwarranted memory burden on the system
Finally two unrelatatedquestion -
- I assume that the id returned by factory.create is an autogenerated affair that is guaranteed to be globally unique?
- Can I reference a dynamically created sprite in this way via its ID and assign it one or more additional attributes? In my case I would like to “tell” it about other, friendly, sprites that have been generated in its neighborhood by communicating their IDs as they get created by the factory.