Sprite Objects Still Displayed after a New One with the Same Name Is Made

Hi, I'm a beginner making a simple chess game on windows using the sdk, and am currently trying to implement a preview of where a piece could move. When a player clicks the A button, I have a class that makes a sprite for a dot which is then displayed where specified. The issue is that if the player changes their mind and decides they want to move a different piece instead and presses A on that piece, an instance/object is created with the same name and displayed, but the old one sticks around.

The simplest solution I've found is just to do :remove() on the dot right before the class's function is called to make it, which removes any preview dots that are being shown, but if none have been displayed yet(the first move of the game), dot1, for example, doesn't exist, and I just get an error that the value is nil, etc.

What would be the best course of action in this situation? Should I just set an image to all of the dots I might need ahead of time, even if this would kind of reduce the point of making a class at all?

Below is a selection of the code that I'm referring to, but here's the source code if needed.

Thanks!

local function init()
selDotImage = gfx.image.new("images/selDot")
	class('selDots').extends(gfx.sprite)

	function selDots:init(relPosX, relPosY)
		self:setImage(selDotImage)
	    self:setZIndex(1)
	    self:setCenter(0.0, 1.0)
	    self:moveTo(selectorSprite.x + relPosX, selectorSprite.y + relPosY)
	    self.relPos = relPosX .. " " .. relPosY
	end
end

function playdate.AButtonDown()
	local selectorPos = selectorSprite.x .. " " .. selectorSprite.y
	if wPawnA.pos == selectorPos then
		dot1 = selDots(0, -50)
		dot1:add()
		dot2 = selDots(0, -100)
		dot2:add()
	elseif wPawnB.pos == selectorPos then
		dot1 = selDots(0, -50)
		dot1:add()
		dot2 = selDots(0, -100)
		dot2:add()
	end
end

Set dots to nil at game initialisation and then only remove() if it's not nil?

How would you check if it's nil? Whenever I do if on it, the game crashes saying "attempt to index a nil value". Also by set dots to nil at game init do you mean the class selDots or dot1, dot2, etc.? Thank you!

For anyone who might come across the same issue, the fix that I've implemented is to at game init set a variable, APressedYet, for example to false. Then when the player presses the A button, I do:

if APressedYet then
    dot1:remove()
    dot2:remove()
    --etc. probably on to a total of 15 dots once every piece can move about?
end

This also resolves any future issue with needing the dots to disappear when the player confirms a spot for the piece to move to.

At the end of my AButtonDown callback function I just set APressedYet to true.

ASIDE: I'm using a callback just because I think it looks better in my code and I think it's maybe less
performance intensive than doing an if button down on every update but idk, I haven't found any negatives to using it over the if in the update func yet.)

That's the best thing I could come up with after embarrassingly being stuck on this for a day. Probably not the most elegant solution in the world, but it works!

ASIDE: I just couldn't get around how you essentially can't do anything with null variables, and one of my attempts included making a sprite for it, but setting the image to null, etc. and doing :remove() at the beginning, etc. with lots of different variations as I was trying to come up with a solution, but that still didn't work. I'm just glad I can move on from this now, with some kind of working solution, no matter how inefficient/inelegant it might be.

1 Like