Spawn game object


Setup

spawn

The “carrotfactory” factory component uses the file carrot.go as prototype, meaning that it produces game objects that are all copies of that file.

carrot

Scripts

bunny.script

function init(self)
	local pos = go.get_position() -- <1>
	go.animate(".", "position.x", go.PLAYBACK_LOOP_PINGPONG, pos.x + 600, go.EASING_INOUTSINE, 6)
end

function on_message(self, message_id, message, sender)
	if message_id == hash("trigger_response") then -- <2>
		if message.enter then -- <3>
			msg.post("#sprite", "disable") -- <4>
		else
			msg.post("#sprite", "enable") -- <5>
		end
	end
end

--[[
1. Get the current position, then animate the position's x component
   looping in a ping-pong manner against an offset of 600.
2. The physics engine has detected that this game object contains
   collision object components that have collided with a trigger.
3. The `message` data table contains a field `enter` that is set
   to `true` when the trigger event signals that the trigger shape
   was entered. On exiting the trigger, this field is `false`.
4. Disable the sprite when the trigger is entered
5. Enable the sprite again on exit.
--]]

carrot.script

function init(self)
	self.t = 2 -- <1>
end

function update(self, dt)
	self.t = self.t - dt -- <2>
	if self.t < 0 then
		go.delete() -- <3>
	end
end

--[[
1. Store a value `t` in the current script component (`self`).
2. Decrease `t` with delta time (elapsed since last call to `update()`).
3. If `t` is 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.