buttonJustPressed/Released registering mulitple times

In my game I'm having an issue where buttonJustPressed and buttonJustReleased registers 3 times when first pressed.

To keep things simple I've created this stripped down code to show the problem. You can see in this gif that the gameState variable jumps from 1 to 4, but every press of the button after that just adds 1.

It this just a bug with the Simulator, or am I doing something wrong?

playdate-20220302-180827

import "CoreLibs/graphics"

gfx = playdate.graphics
gameState = 1

function buttonCheck()
    if playdate.buttonJustPressed("a") then
      gameState = gameState + 1
      gfx.setColor(gfx.kColorWhite)
      gfx.fillRect(0,0,400,240)
    end
end


function playdate.update()

  if gameState == 1 then
    gfx.drawTextAligned("THIS IS GAME STATE " ..gameState , 200, 100, kTextAlignment.center)
    buttonCheck()
  end

  if gameState == 2 then
    gfx.drawTextAligned("THIS IS GAME STATE " ..gameState , 200, 100, kTextAlignment.center)
    buttonCheck()
  end

  if gameState >= 3 then
    gfx.drawTextAligned("THIS IS GAME STATE " ..gameState , 200, 100, kTextAlignment.center)
    buttonCheck()
  end

end

Try this:

  if gameState == 1 then
    gfx.drawTextAligned("THIS IS GAME STATE " ..gameState , 200, 100, kTextAlignment.center)
    buttonCheck()
  elseif gameState == 2 then
    gfx.drawTextAligned("THIS IS GAME STATE " ..gameState , 200, 100, kTextAlignment.center)
    buttonCheck()
 else if gameState >= 3 then
    gfx.drawTextAligned("THIS IS GAME STATE " ..gameState , 200, 100, kTextAlignment.center)
    buttonCheck()
  end

playdate.buttonJustPressed() will keep returning the same result in a single update.

1 Like

Awesome Nic. That solved it. Thanks so much! :grin:

1 Like

...well it solved it in the stripped down code, but I still have an issue in my game. Hmm.

EDIT: Got it all sorted now!

1 Like