CollisionResponse for objects

Hi, having some trouble with adding collisionsResponses to objects. I've been looking at the examples but am not sure what missing.

Here's example code. The expected behaviour would be for the two objects to print "test" to log, however nothing is happening. A pointer to the missing step would be most appreciated, thanks.

import "CoreLibs/sprites"
import "CoreLibs/graphics"
spr  = playdate.graphics.sprite
class("testObj").extends(spr)
function testObj:init(x,y)
    self:moveTo(x,y)
    self:setSize(20,20)
    self:setCollideRect(0,0,20,20)
    self:add()
end
function testObj:collisionResponse(other)
    print("test")
    return "overlap"
end
testObj(20,20)
testObj(15,20)
function playdate.update()
    spr.update()
end

The first thing that I see is you should use function playdate.update() ... end with . not :. I haven't used sprites yet so I can't comment on that, but hopefully this gets you closer!

corrected, wrote that quick just to focus on the issue. still not printing however

Do you have to call testObj.super.init(self, ...) with the playdate.graphics.sprite initialization args?

don't think so, since setting the xy/size and not using image. adding does not fix.

hmmm looking more at the examples and docs, seems moveWithCollisions is necessary to trigger collisions, or checkCollisions.

import "CoreLibs/sprites"
import "CoreLibs/graphics"
spr  = playdate.graphics.sprite
class("testObj").extends(spr)
function testObj:init(x,y,dx)
	testObj.super.init(self)
	 self:moveTo(x,y)
	 self:setSize(20,20)
	 self:setCollideRect(0,0,20,20)
	 self:add()
	 self.dx=dx
end
function testObj:update()
	self:moveWithCollisions(self.x+self.dx,self.y)
end
function testObj:collisionResponse(other)
	 print("test")
	 return "overlap"
end
testObj(0,20,1)
testObj(15,20,-1)
function playdate.update()
	 spr.update()
end
2 Likes