This example shows how to dynamically spawn game objects with a factory component.
The “carrotfactory” factory component uses the file carrot.go as prototype, meaning that it produces game objects that are all copies of that file.
function init(self) msg.post(".", "acquire_input_focus") end function on_input(self, action_id, action) if action_id == hash("click") and action.pressed then local pos = go.get_position() pos.x = pos.x + 100 local carrot_id = factory.create("#carrotfactory", pos) go.animate(carrot_id, "position.x", go.PLAYBACK_ONCE_FORWARD, 720, go.EASING_LINEAR, 3) end end
function init(self) self.t = 2 end function update(self, dt) self.t = self.t - dt if self.t < 0 then go.delete() end end
tin the current script component (
twith delta time (elapsed since last call to
tis below 0, delete the current game object ("." is shorthand for that).
If you want to play with these examples, you can get the project on Github.
Do you want to see more examples? Why not write a few yourself and submit a pull request? We love contributions.