Remove All Sprites At Once / Reset Game

I have completed the core functions of my game and want the player to be able to restart the game easily. I have two ideas on how this can be accomplished but I have not had very much luck with either one:

  1. Call some sort of restart function/method. I have been looking for a clear cut way to just restart my game similar to loading it in the simulator but I have not been able to find anything that makes sense to me. What I would want is something like your traditional reset button on a classic NES console, something like that that is executed when the crank is turned. I think this is the preferred way to go.

OR

  1. I could just remove all the sprites, animations, etc. in my game and call the initialize function I have that would readd the sprites, set the variables again, etc. This would function the same way as when the game is started initially but would have to reset everything to their default values since the player could have changed them by playing the game (or not if they crank right away, either way).

I am stuck on how to remove all my sprites at once, here is what I have so far:

    -- if the player turns the crank to 6 or 12 o'clock then...
if ((crankTurns == 1) or (crankTurns == -1)) then
	
           -- it's supposed to remove all versions of a particular sprite but only removes those the 
           -- player has touched
	self:remove()
	-- print crankTurns for testing purposes
            print(crankTurns)

end

I set crankTurns equal to this: crankTurns = playdate.getCrankTicks(2)
so a player could crank in either direction and "blow up the game" so it restarts at 6 and 12 o'clock. How would I remove all the sprites that I have instead of removing one at a time? Sprites only drop when they come into contact with the player when I test it right now. I thought calling self:remove() would remove ALL copies/versions of the sprite but that is not the case here. I'm looking for a solution to #1 or #2 above but understanding both would be ideal.

I believe we are planning to add a function that will allow you to restart your game from code, but I can't say yet exactly when that will land in an SDK release.

In the meantime, to remove all sprites at once, you can use the function playdate.graphics.sprite.removeAll().

What you have observed is correct, calling sprite:remove() will only act on the calling sprite. In general when using colon syntax like self:function() you should expect the function to only operate on the self object.

If you don't want to remove ALL sprites, but do want to remove a specific group of sprites, your best bet would probably be to track that group of sprites yourself in a table, then call playdate.graphics.sprite.removeSprites(spriteArray).

3 Likes

Thanks Dan, works like a charm now. If it helps someone in the future here is what I ended up with to remove all the sprites, it gets triggered when the crank hits 6 o'clock or 12 o'clock:

function playdate.cranked(change, acceleratedChange)

crankTurns = playdate.getCrankTicks(2)

if crankTurns == 1 or crankTurns == -1 then

	print(crankTurns)
	print(playdate.graphics.sprite.spriteCount())
	playdate.graphics.sprite.removeAll()
	playdate.graphics.clear()
	print(playdate.graphics.sprite.spriteCount())

end

end

The spriteCount parts show the before and after so I know it worked (not that I doubted you, it's just nice to see it for myself). Thank you again.

Great! I'm glad you got it working :slight_smile: