Can you use an image table for a sprite?

Hello all. When the Playdate was announced, the concept and the growing community inspired me to make something, even though I'd never coded before.

After six months of work (you read that right), I had an animated player sprite that could walk back and forth between two rooms, and a dialogue system that barely worked. Exciting! Another year or two, and who knows - maybe I'd have three rooms!

I spent an enormous amount of time and effort producing something that an amateur could have made in a day. In spite of the community's positivity and assistance, I was deeply discouraged. I dropped the project.

Fast forward one year. My physical Playdate arrives. Against my better judgement, I dredge up that old project. It would be cool to see it on the little screen, at least.

But disaster has struck again! When I try to compile and run it now, the Simulator gives me this error message:


CoreLibs/sprites.lua:44 bad argument #1 to 'newfunc' (image or tilemap)
stack traceback:
[C]: in upvalue 'newfunc'
CoreLibs/sprites.lua:44 in field 'new'
player.lua:11: in function 'createPlayer'
player.lua:34: in main chunk
stack traceback:
[C]: in function 'import'
main.lua:9: in main chunk


Here is some of the relevant code from player.lua. The 11th line is "Player = gfx.sprite.new(mp)"


function createPlayer()
	local mp = gfx.imagetable.new('images/templatev2')
	Player = gfx.sprite.new(mp)
	Player:setImage(mp:getImage(DOWN))
	Player:setCollideRect(0,20, 21, 36)
	Player:moveTo(190, 180)
	Player:add()
	function Player:collisionResponse(other)
		if other:isa(door) or other:isa(door2) then
			return "overlap"
		else
			return "slide"
		end
	end
end

To troubleshoot, I used a static image for the Player instead. I got rid of the image table and any code that referenced it, and the game compiled and ran correctly.

I'm extremely confused, because last year, I didn't have this problem, and I haven't changed the code at all. Can you no longer use an imagetable in gfx.sprite.new(image)?

It's worth noting that I have since built a new PC. I reinstalled and reconfigured the Playdate SDK and Visual Studio (I write in Lua).

1 Like

Hi Kempson!

Disclaimer: I'm a beginner programmer, but will do my best!

I've run into this issue before and helped another person on the Playdate Squad Discord who had a similar issue.

Not exactly sure what could be the issue on your side (seems like in image issue, but you mentioned it's worked before) as I'm more familiar with object oriented programming for sprite tables. If you go the OOP route, you can use the examples in the SDK like Level 1-1 or FlippyFish (pasted below for the Level 1-1 example). Hoping this solves your issue and if not, feel free to include what the new error message is. Happy to help.

function Player:init()
	
	Player.super.init(self)

	self.playerImages = playdate.graphics.imagetable.new('img/player')
	self:setImage(self.playerImages:getImage(1))
	self:setZIndex(1000)
	self:setCenter(0.5, 1)	-- set center point to center bottom middle
	self:moveTo(102, 210)
	self:setCollideRect(2,0,16-4,32)
	
	self.position = Point.new(102, 210)
	self.velocity = vector2D.new(0,0)
end

If you create a separate minimal dummy project with imagetables with the same error. Feel free to send it over and I can help troubleshoot it for you.

Hey, I really appreciate your help, and I may try your method in the future, but I actually solved the problem thanks to another dev on a Discord:

playdate.graphics.sprite.new(image) only accepts an image or a tilemap now. You might notice I was trying to pass in an imagetable in my original code.

I made a tilemap then used playdate.graphics.tilemap:setImageTable(table) to set the imagetable AS the tilemap. Now it works! Confusing and unnecessary, but it works!

I appreciate your thorough response, though. You know this stuff better than I do! If I run into future problems with it, I may take you up on your offer.

function createPlayer()
	mptable = gfx.imagetable.new('secondaryimages/templatenew')
	local mp = gfx.tilemap.new()
	mp:setImageTable(mptable)
	Player = gfx.sprite.new(mp)
	Player:setImage(mptable:getImage(DOWN))
	Player:setCollideRect(0,20, 21, 36)
	Player:moveTo(190, 180)
	Player:add()
	function Player:collisionResponse(other)
		if other:isa(door) or other:isa(door2) then
			return "overlap"
		else
			return "slide"
		end
	end
end

Dude walking around

2 Likes