Spawning an object every second to float from one side of the screen to the other

Hello all,

Firstly, this community is fantastic, thanks for all the code and help over the last few months i’v been learning.

I have a question regarding the best way to spawn an object so that it floats from one side of the screen to the other. I would also like the program to spawn the same object each second creating a train of the same objects floating across the screen.

Any suggestions?

Thanks for your help,

Alola

Not in front of a computer now. My suggestion is this:

timer.delay(1.0, function()
  local id = factory.create("#myfactory", vmath.vector3(0, math.random(0, 640), 0))
  go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, 1136, go.EASING_LINEAR, 1.0, 0, function() 
      go.delete(id)
  end)
end)
5 Likes

I am more of a newbie than I thought. I can’t get it to work at all. It throws up errors that tell me it can’t create collections ect.

I’m sure your code is completely fine but I don’t think I even know how to make a factory. Is there a unified way to make an efficient factory that i’m unaware of?

Thanks.

Please share the errors. It will make it easier for us to help.

The Simple Spawning example shows how to use a factory. And the Manual.

Great! I’ll check it out. To be honest I should have just downloaded the Sidescroller game and taken a look at how it was done.

Thanks again,

Alola

Welcome to Defold! I cannot recommend the manual enough if you want to learn Defold as a beginner (or at any point, really): the documentation is excellent, and comes with many great examples, like the one @britzl shared for factories.

Check through the documentation, run through some examples, and ask questions here if you get stuck. Good luck!

3 Likes

I took a step back and analysed the Side scroller tutorial game, did some experimenting and implemented your code where I think it needed to be. Everything seems to be working in good order. Thanks for your help, you are a huge asset to this community.

1 Like

Thank you for your contribution. I had a look at everything that has been suggested. I feel very welcome in this community as a result. Hopefully one day I can give back and contribute in a meaningful way.

Kind regards,

Alola

I combined your code with the Side scroller tutorial games code and edited it down to this. Feel free to make a modification to allow it to run better.

local frequency = 0.5
local min_y = 60
local max_y = 300

function init(self)
	self.timer = 0.5/frequency
	-- make the game deterministic
end

function update(self, dt)
	self.timer = self.timer - dt
	if self.timer <= 0 then
		self.timer = 0.5/frequency
		local p = go.get_position()
		local id = factory.create("#star_factory", vmath.vector3(0, math.random(0, 0), 0))
		go.animate(id, "position.x", go.PLAYBACK_ONCE_BACKWARD, 1280, go.EASING_LINEAR, 5.0, 0, function() 
			go.delete(id)
		end)

			if math.random() then

		end
	end
end

2 Likes

Happy to hear that you got it working! We released a new version of Defold this Monday and in that version (1.2.130) we included a native timer implementation. It is recommended to use the timer instead of manually doing the work in an update function like in your example.