I am currently making a game on Defold and this was where I was pushed by the website to find technical support. I am trying to get collision messages from an enemy game object in my game – I have already got it working for my player game object so I thought it would be easy – but am having some problems. This is the code for the enemy I want to get collision messages:
function init(self)
self.speed=250
self.direction = 1
new_enemy1(self)
end
function new_enemy1(self)
-- spawn enemy1
self.enemy1_table = {}
local position
local enemy_id
-- set position of enemy on screen
for y = 500,290,-70 do
for x = 100,550,75 do
pos = vmath.vector3(700,600,0)
enemy_id = factory.create("#enemy1_factory", pos)
-- hold reference to enemy
table.insert(self.enemy1_table, enemy_id)
end
end
end
function update(self, dt)
local movement = vmath.vector3(self.direction * self.speed * dt, 0, 0)
-- Update the position of each enemy in the enemy1_table
for _, enemy_id in ipairs(self.enemy1_table) do
local enemy_pos = go.get_position(enemy_id)
go.set_position(enemy_pos + movement, enemy_id)
end
end
function on_message(self, message_id, message, sender)
print("Received message:", message_id)
-- Handle collision
if message_id == hash("contact_point_response") then
print("Collision Message:")
print(" Normal:", message.normal)
print(" Distance:", message.distance)
print(" Other:", message.other)
local newpos = go.get_position() + message.normal * message.distance
go.set_position(newpos)
end
end
I attached a picture of where the code is placed in main.collection. I made the enemy1 game object in the main folder in the assets panel which I also have a screenshot of. I have the mask and Group set up for both the enemy and the boundary I want to receive a collision message at. I will link a screenshot of both. The enemy is spawned with a factory and I have it moving to the right and it just moves past the boundary and nothing is written in the console. I will add a screenshot of what I see on the console.
https://imgpile.com/i/CXTpZl - console
https://imgpile.com/i/CXT3n8 - boundary collision object
https://imgpile.com/i/CXTgkS - enemy collision object
https://imgpile.com/i/CXT6xb - Assets panel
https://imgpile.com/i/CXTEo3 - main.collection outline panel