Bleh, this is even more confusing... When I look at the collision example file it has
local function updatePlayer(dt, player)
local speed = player.speed
local dx, dy = 0, 0
-- Skipping the button stuff
dx = player.velocityX * dt
dy = player.velocityY * dt
if dx ~= 0 or dy ~= 0 then
local actualX, actualY, cols, cols_len = player:moveWithCollisions(player.x + dx, player.y + dy)
for i=1, cols_len do -- This is just like my collision loop
local col = cols[i] -- Just like me getting the sprite1
-- not trying to be physics-accurate
if col.normal.x ~= 0 then -- hit something in the X direction
-- if we were sliding we'd just want to set our velocityX to zero, but we're bouncing, so...
if col.other:isa(Player) then
-- collided with another player, so transfer some of our velocity to that player
col.other.velocityX = col.other.velocityX + (player.velocityX * 0.2) -- How they getting other.velocityX???
player.velocityX = -(player.velocityX * 0.8)
player.velocityX = player.velocityX + (-col.other.velocityX * 0.2)
When I try using other it just gives me an error like "other" does not exist. O....O what the heck????
- OOOK I've figured something out.
When I was making my class, I was using local variables at the top, initializing them in the constructor and using them throughout the class.
NOW I see that these local variables are only visible to the class, kind of like using private. If I just do something like car.color = "red" without using any type of local variable. I'm able to see it when I do the collision check.
(code tangent ahead O....O)
I'm not sure why this is. I'd think that anything not set to private (like local) would be global and all could see it. Doesn't make sense to me yet how a variable like (car.color) can be accessed when it's not even initialized in the file up front. I'd expect it would go (color not found) or something since I'm trying to use something that isn't even defined.
(k, I'm done)
If you're using a class like I am.
local pd <const> = playdate
local gfx <const> = pd.graphics
local car = nil
car = gfx.sprite.new(base)
car.color = "red" -- Since you're doing .color, you can access it later
local collisions = gfx.sprite.allOverlappingSprites()
local length = #collisions
for i = 1, length do
local collisionPair = collisions[i]
local sprite1 = collisionPair
local sprite2 = collisionPair
print(sprite2.color) -- should now print red
I'll now change my variables to just be in the init, and see how that goes.