sprite:setRotation will rotate the sprite’s image if it has one. Yours doesn’t - it’s drawing a polygon directly to the screen instead:
function playerShip:draw()
gfx.drawPolygon(16,0, 24,16, 32,16, 16,32, 0,16, 8,16)
end
If you want to use sprites with images and use sprite:setRotation, you could do something like:
function myGameSetUp()
local ship_image = gfx.image.new(32, 32)
gfx.pushContext(ship_image)
gfx.drawPolygon(16,0, 24,16, 32,16, 16,32, 0,16, 8,16)
gfx.popContext()
playerShip = gfx.sprite.new()
playerShip:setImage(ship_image)
playerShip:moveTo(200,120)
playerShip:add()
function playerShip:update()
playerShip:setRotation(playerShip:getRotation() + playdate.getCrankChange())
-- to keep things tidy, shift the rest of your input handling from playdate.update to here
end
end
Or if you want to retain the polygon drawing (it'll look cleaner and run faster):
function myGameSetUp()
playerShip = gfx.sprite.new()
playerShip.polygon = geom.polygon.new(16,0, 24,16, 32,16, 16,32, 0,16, 8,16, 16,0)
playerShip.polygon:translate(184, 104)
playerShip.rotate_transform = geom.affineTransform.new()
playerShip:moveTo(200,120)
playerShip:add()
function playerShip:update()
-- rotate the transform
playerShip.rotate_transform:rotate(playdate.getCrankChange(), playerShip.x, playerShip.y)
-- apply the transform to the ship polygon
playerShip.rotate_transform:transformPolygon(playerShip.polygon)
-- reset the transform so that crank change doesn’t accumulate over time
playerShip.rotate_transform:reset()
end
function playerShip:draw()
gfx.drawPolygon(playerShip.polygon)
end
end
But this is half in and half out of the sprite system (you need playerShip:draw() in playdate.update()).
So it might be better to combine the two - give the sprite an image, then draw the ship polygon to that image instead of directly to the screen:
function myGameSetUp()
playerShip = gfx.sprite.new()
playerShip.polygon = geom.polygon.new(16,0, 24,16, 32,16, 16,32, 0,16, 8,16, 16,0)
playerShip.rotate_transform = geom.affineTransform.new()
playerShip:moveTo(200,120)
playerShip:add()
function playerShip:update()
-- rotate the transform
playerShip.rotate_transform:rotate(playdate.getCrankChange(), 16, 16)
-- apply the transform to the ship polygon
playerShip.rotate_transform:transformPolygon(playerShip.polygon)
-- reset the transform so that crank change doesn’t accumulate over time
playerShip.rotate_transform:reset()
-- draw the ship polygon to the sprite's image
local img = gfx.image.new(32, 32)
gfx.pushContext(img)
gfx.drawPolygon(playerShip.polygon)
gfx.popContext()
playerShip:setImage(img)
end
end