Skip to content
Download

Collision

Collision uses the sprite collision box, set in the sprite editor. Two instances collide when their boxes overlap. Instances with no sprite do not collide, and angle is ignored.

Register in onSpawn.

Function What it does
onCollide(object, fn) Calls fn(other) on every step with contact.
onTouchStart(object, fn) Calls fn(other) once, when the contact starts.
onTouchEnd(object, fn) Calls fn(other) once, when the contact ends.
function onSpawn(self)
self:onTouchStart("Coin", function(moeda)
moeda:destroy()
self.moedas = self.moedas + 1
end)
end
Function What it does
touching(object) The instance of the object touching now, or nothing.
collidesAt(object, x, y) true if there would be contact with this instance at (x, y).
findAt(object, x, y) The instance that would be touching at that position, or nothing.
placeFree(x, y) true if no solid blocks that position.
collisionBox() The collision box in the world: x, y, width and height.
if self:touching("Espinho") then restart() end
if self:placeFree(self.x + 2, self.y) then self.x = self.x + 2 end
  • mask replaces the collision box with that of another sprite; flipX mirrors the box together with the drawing.
  • onCollide runs after the movement of the step. An instance that spawns touching fires onTouchStart on the first step.
  • C++: the functions accept the class (onCollide<Coin>, collidesAt<Wall>(x, y)) or the name in quotes; with the name, the callback receives Entity&.
  • To move and stop at walls, use moveAndStop: Movement.