Points continue to go up even after sprite is deleted?

,

Building a rhythm game with a point system. The way I did it is collision based so if the up arrow is colliding with a high up bar and you press up then it will only give you 1 point vs if the bar is perfect height then you get 4 points. After that the up arrow sprite is deleted. However I found that even after the arrow sprite is deleted for some reason you can still get some points for a short ammount of time by continuously pressing the up button. Not sure why this is happening and if someone could help me find a solution I would be greatful! Using windows on sdk!

for k=1,1 do
    if collisionbar:alphaCollision(upArrowSprite[k]) == true and playdate.buttonJustPressed( playdate.kButtonUp ) then
        upArrowSprite[k]:remove()
        score += 1
    end
end

I suspect this is intended. Calling remove() only removes the sprite from the display list, so any collision functions are still going to work normally.

Two suggestions:

  1. Use setVisible() instead of remove() and check isVisible() before you make the collision check.

  2. Set a property on the sprite object when you add/remove it e.g.

for k=1,1 do
    if upArrowSprite[k].isActive                            -- skip inactive (removed) sprites
    and playdate.buttonJustPressed( playdate.kButtonUp )
    and collisionbar:alphaCollision(upArrowSprite[k]) then  -- optimization: do collision check last, so you're not checking removed sprites
        upArrowSprite[k]:remove()
        upArrowSprite[k].isActive = false                   -- set flag
        score += 1
    end
end

I tried that and score never went up?

Ah hrm, which option did you try?

If you used #2, I just noticed the for-loop isn't using the length of the upArrowSprite array. This was in your snippet, so not sure if that's intentional? Specifically for k=1,1 do should probably be for k=1,#upArrowSprite do

Also probably not the problem, but another optimization:

if playdate.buttonJustPressed( playdate.kButtonUp ) then        -- only check input once
    for k=1,1 do
        if upArrowSprite[k].isActive                            -- skip inactive (removed) sprites
        and collisionbar:alphaCollision(upArrowSprite[k]) then  -- optimization: do collision check last, so you're not checking removed sprites
            upArrowSprite[k]:remove()
            upArrowSprite[k].isActive = false                   -- set flag
            score += 1
        end
    end
end

Posting the whole Lua file would also help me understand the larger context :slight_smile:

nevermind I figured out what you were saying! For anyone else with this problem its:
first initialize as true in the starting stage:
upArrowSprite1[k].isActive = true
then:
for k=1,1 do
if upArrowSprite1[k].isActive and cllisionbar:alphaCollision(upArrowSprite1[k]) == true and playdate.buttonJustPressed( playdate.kButtonUp ) then
upArrowSprite1[k]:setVisible(false)
upArrowSprite1[k].isActive = false
score += 1
end
end

1 Like