Moving a Background Image and/or Displaying Sprites Above Images

Yeah, it seems like gfx.sprite.update() clears the screen when it's called, so I guess it isn't possible to draw images behind sprites without making the background itself a sprite. I reimplemented the background as a sprite, and it seems to work correctly. I wasn't really sure what you meant by the default position showing the bottom of the image, so I just used the top left as the origin, but you could change the center to setCenter(0, 1) if you want.

--main imports
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

--declarations
local gfx <const> = playdate.graphics
local baseY = 0

--setup func
local function setup()
	
	--board image
	backgroundImage = gfx.image.new("images/background")
	backgroundSprite = gfx.sprite.new(backgroundImage)
	backgroundSprite:setZIndex(-32768)
	backgroundSprite:setCenter(0, 0)
	backgroundSprite:add()
	assert( backgroundImage )

	--star test
	local testStar = gfx.image.new("images/testStar")
	testStarDisplay = gfx.sprite.new(testStar)
	
end

function scrollBoard()
	
	local change, acceleratedChange = playdate.getCrankChange()
	baseY += change

	if acceleratedChange > 15 then
		acceleratedChange = 15
	end

	if acceleratedChange < -15 then
		acceleratedChange = -15
	end

	baseY += acceleratedChange
	
	if baseY < 0 then baseY = 0 end
	backgroundSprite:moveTo(0, baseY)

end	

--calling setup functions
setup()

--main update func
function playdate.update()

	gfx.sprite.update()
	
	scrollBoard()

	if playdate.buttonIsPressed(playdate.kButtonA) then
		print("pressed")
		testStarDisplay:moveTo(200, 120)
		print("after move")
		testStarDisplay:add()
		print("after add")
		testStarDisplay:setZIndex(32767)
		print("after zindex")
	end

	playdate.drawFPS(0, 0)
end
1 Like