I'm developing on Mac. I'm using playdate.graphics.sprite:alphaCollision(anotherSprite) in a collision loop to determine if two sprites actually touch.
The code looks like this:
local hits = ball:overlappingSprites()
for key, other in pairs(hits) do
if not ball:alphaCollision(other) then
print("Not an alpha collision!")
goto continue
end
-- remove the other sprite...
::continue::
end
As far as I can tell this is the intended way of using this functionality. However when I run my game, it looks like this (gif from simulator):
When the ball touches the square enemy it should collide. It's picked up on collision rects, but it's failing the check for alpha collision, the log is spammed with Not an alpha collision! which is unexpected.
What if you add an else that prints that there is an alpha collision?
Are you able to get alpha collisions working at all? This code recognizes alpha collisions, for me.
import "CoreLibs/graphics"
local CHARACTER_SIZE <const> = 14
local PLAYER_X <const> = 200 + CHARACTER_SIZE
local PLAYER_Y <const> = 120
local ENEMY_X <const> = 200 - CHARACTER_SIZE
local ENEMY_Y <const> = PLAYER_Y
local MOVE_SPEED <const> = 1
local REPORT_X <const> = 0
local REPORT_Y <const> = 0
local playerImage <const> = playdate.graphics.image.new(CHARACTER_SIZE, CHARACTER_SIZE)
local playerSprite <const> = playdate.graphics.sprite.new(playerImage)
local enemyImage <const> = playdate.graphics.image.new(CHARACTER_SIZE, CHARACTER_SIZE)
local enemySprite <const> = playdate.graphics.sprite.new(enemyImage)
playdate.graphics.pushContext(playerImage)
playdate.graphics.fillCircleInRect(0, 0, CHARACTER_SIZE, CHARACTER_SIZE)
playdate.graphics.popContext()
playerSprite:setCollideRect(0, 0, CHARACTER_SIZE, CHARACTER_SIZE)
playerSprite:moveTo(PLAYER_X, PLAYER_Y)
playerSprite:add()
playdate.graphics.pushContext(enemyImage)
playdate.graphics.setDitherPattern(0.65, playdate.graphics.image.kDitherTypeBayer8x8)
playdate.graphics.fillRect(0, 0, CHARACTER_SIZE, CHARACTER_SIZE)
playdate.graphics.popContext()
enemySprite:setCollideRect(0, 0, CHARACTER_SIZE, CHARACTER_SIZE)
enemySprite:moveTo(ENEMY_X, ENEMY_Y)
enemySprite:add()
playdate.graphics.setColor(playdate.graphics.kColorClear)
function playdate.update()
local dx = 0
local dy = 0
if playdate.buttonIsPressed(playdate.kButtonLeft) then
dx -= MOVE_SPEED
end
if playdate.buttonIsPressed(playdate.kButtonRight) then
dx += MOVE_SPEED
end
if playdate.buttonIsPressed(playdate.kButtonUp) then
dy -= MOVE_SPEED
end
if playdate.buttonIsPressed(playdate.kButtonDown) then
dy += MOVE_SPEED
end
playerSprite:moveBy(dx, dy)
playdate.graphics.sprite.update()
local overlappingSprites <const> = playerSprite:overlappingSprites()
local alpha = false
for i = 1, #overlappingSprites do
if playerSprite:alphaCollision(overlappingSprites[i]) then
alpha = true
end
end
playdate.graphics.drawText(tostring(alpha), REPORT_X, REPORT_Y)
end