player movement question

I made a on-screen d-pad that is linked to a sprite character. All the buttons are working and the character moves in the right direction when the corresponding button is pressed. My question is how do I make it so when I’m holding down the button the character moves in that direction and doesn’t stop. Right now the character just moves once while I’m holding down the button.

if ( event.phase == "began" )  then

        if ( touchOverButton ~= nil ) then

            if not ( buttonGroup.touchID ) then

                buttonGroup.touchID = event.id

                buttonGroup.activeButton = touchOverButton

                if ( buttonGroup.activeButton.ID == "left" ) then    

                    capguy.x = capguy.x - dpadSpeed

                elseif ( buttonGroup.activeButton.ID == "right" ) then

                    capguy.x = capguy.x + dpadSpeed

                elseif ( buttonGroup.activeButton.ID == "up" ) then

                    capguy.y = capguy.y - dpadSpeed

                elseif ( buttonGroup.activeButton.ID == "down" ) then

                    capguy.y = capguy.y + dpadSpeed

You can find one such implementation from https://www.solar2dplayground.com/. You can find sample code by selecting “Knight” project.

1 Like