Multiple Game Object Creation


Thanks for any help you can provide, I am fairly new to defold. I am in the process of creating a game wherein I will need to randomly select from about 70 game objects in order to display two of the objects on screen, accompanied by a particular sound. What would be the best means of accomplishing this? A factory seems to make sense, but I cannot figure out whether it is best accomplished via a regular factory, a collection factory, or some other method.


I would suggest that you use a single game object type and factory, spawn two instances and then update the sprites (as opposed to creating 70 different game objects with individual factories).

You update a sprite image using sprite.play_flipbook("#myspriteurl", "animate_id")


Do you think the same basic method could be used to change played audio?


Which sound should be played? A sound per randomly selected game object? Or a single sound based on the combination of selected game objects?

Something like this:

local animals = {
	{ anim = "bird", sound = "tweet" },
	{ anim = "cow", sound = "moo" }
	-- and more

local function create_gameobjects()
	local id1 = factory.create("#myfactory")
	local id2 = factory.create("#myfactory")
	local animal1 = animals[math.random(1, #animals)]
	local animal2 = animals[math.random(1, #animals)]
	sprite.play_flipbook(msg.url(nil, id1, "mysprite"), animal1.anim)
	sprite.play_flipbook(msg.url(nil, id2, "mysprite"), animal2.anim)"animalsounds#" .. animal1.sound)"animalsounds#" .. animal2.sound)

All sounds as components on a game object with id animalsounds.