How to make factory objects collectable?

How can I make factory objects(for example coins) collectable? I tried to find it out in tutorial but I couldn’t. Here is my factory code

local frequency = 0.8
local black_prob = 0.3
local min_y = 150
local max_y = 650
function init(self)
	self.timer = 1/frequency
end
function update(self, dt)
	self.timer = self.timer - dt
	if self.timer <= 0 then
		self.timer = 1/frequency
		local pos = go.get_position()
		pos.y = math.random(min_y, max_y)
		local spider = "#orange_factory"
		if math.random() < black_prob then
			spider = "#black_factory"
		end
		factory.create(spider,pos)			
	end
end

And here is hero code

local max_height = 670
local min_height = 150
local max_speed = 300
function init(self)
	msg.post(".", "acquire_input_focus")
	self.speed = -200
end
function update(self, dt)
	local pos = go.get_position()
	pos.y = pos.y + self.speed * dt
	if pos.y > max_height then
		pos.y = max_height
	elseif pos.y < min_height then
		pos.y = min_height
	end
	go.set_position(pos)
	self.speed = -200
end
function on_input(self, action_id, action)
    if action_id == hash("up") then
		self.speed = max_speed
		if action.pressed then 
			msg.post(go.get_id(), hash("play_animation"), {id = hash("anim")})
		end
	end
end

Add physics trigger on coin. Let the collision with the hero trigger a message to the hero, or a function in the coin script. Then handle as best fit.

1 Like

See http://www.defold.com/tutorials/getting-started/#_step_9_coins_to_collect

1 Like