How to respawn objects at random side and random alternately using factory?

I need some help again from you guys out there!..i actually wanted to respawn objects at random side(top or bottom) and also randomize it alternately.
For example: “respawn a blue circle once on top and after that respawn a red circle twice on bottom” or
"respawn a blue circle trice on bottom and after that respawn a red circle once on top" or “respawn red circle once on bottom and after that respawn blue circle once also on bottom”

I actually managed to respawn objects using the factory component…but i don’t know how to do this one so i hope you can help me…thanks in advance! :’)

1 Like

Two options:

  1. One factory for each type of object. Randomize which factory id to use. See the Getting Started tutorial and the platform spawning for an example: http://www.defold.com/tutorials/getting-started/#_step_6_ground_physics_and_platforms and “Spawning platforms”

  2. Use one factory and have logic in the spawned game object’s init to choose the look of the object. You can pass properties to spawned object. See
    http://www.defold.com/ref/factory/#factory.create

2 Likes

@sicher thanks for the idea…and im sorry i forgot to say one important thing – my logic is when an object is re spawned,…it will actually go to it’s opposite direction (i.e. when a red circle is re spawned from top, it will go directly to bottom…)…i actually use the first option that you gave to me and modify it a little bit,…but still failed…i cant apply the full logic on it…any help again is really appreciated…thanks!

So when an object goes outside the top of the screen it should be spawned at the bottom again?

function update(self, dt)
	local pos = go.get_position()
	-- move up 10 pixels/second
	pos.y = pos.y + 10 * dt
	-- if position is outside top edge of screen then move it to the bottom
	if pos.y > your_screen_height_here then
		pos.y = 0
	end
	go.set_position(pos)
end
1 Like