I feel like this has been talked about before, but I can't for the life of me fined a clear answer as to how to do it.
If I'm using tile maps, how do you accomplish infinite scrolling?
I feel like this has been talked about before, but I can't for the life of me fined a clear answer as to how to do it.
If I'm using tile maps, how do you accomplish infinite scrolling?
Look at the lua file 'level.lua' in the 'Level 1-1' example (on Windows: "...\PlaydateSDK\Examples\Level 1-1\Source\level.lua"), the relevant chunk is here:
-- moves the camera horizontally based on player's current position
function Level:updateCameraPosition()
local newX = floor(max(min(player.position.x - halfDisplayWidth + 60, maxX), minX))
if newX ~= -cameraX then
cameraX = -newX
gfx.setDrawOffset(cameraX,0)
playdate.graphics.sprite.addDirtyRect(newX, 0, displayWidth, displayHeight)
:
end
end
I've done a good amount of digging through level.lua already. I don't fully understand it, but I'll give this piece a shot.
The only thing this block does is move the whole screen... I don't understand how to use this.
Well, yes, 'moving the whole screen' is part of what it takes to 'do scrolling' .
Beyond that all I can say is 'look at the Level 1-1 example' - here's some simple X (and Y) camera/player movement/scrolling based on that example:
Camera code here:
function Level:updateCameraPosition()
local newX = floor(max(min(ThePlayer.position.x - halfDisplayWidth, maxX), minX))
local newY = floor(max(min(ThePlayer.position.y - halfDisplayHeight, maxY), minY))
if (newX ~= -cameraX) or (newY ~= -cameraY) then
cameraX = -newX
cameraY = -newY
gfx.setDrawOffset(cameraX,cameraY)
playdate.graphics.sprite.addDirtyRect(newX, newY, displayWidth, displayHeight)
end
end
Player code here:
function Level:movePlayer()
if ThePlayer.position.x > MAX_PLAYER_X then -- fell off of the world! Respawn at the beginning
ThePlayer.position.x = MAX_PLAYER_X
end
if ThePlayer.position.y > MAX_PLAYER_Y then -- fell off of the world! Respawn at the beginning
ThePlayer.position.y = MAX_PLAYER_Y
end
if ThePlayer.position.y < TILE_SIZE then -- fell off of the world! Respawn at the beginning
ThePlayer.position.y = TILE_SIZE
end
local collisions, len
if FirstMovePending == true then
FirstMovePending = false
ThePlayer:moveTo(ThePlayer.position.x, ThePlayer.position.y)
len = 0
else
ThePlayer.position.x, ThePlayer.position.y, collisions, len = ThePlayer:moveWithCollisions(ThePlayer.position)
end
ThePlayer:setOnGround(true)
for i = 1, len do
local c = collisions[i]
if c.other.isWall == true then
ThePlayer.velocity.x = 0
elseif c.other:isa(Coin) then -- player's collisionResponse returns "overlap" for coins
self:collectCoin(c.other)
elseif c.other:isa(Enemy) then
ThePlayer.velocity.x = 0 -- treat enemies like barriers instead of having them kill player
end
end
end