SpriteSheet not updating quick enough on Dpad

Hello I’ve been having this problem for a while now, I have a normal dpad on bottom left of screen and it has 4 buttons. The problem I am having is when I click on a button and still on screen slide finger to next button so (right to up) for movement the character will do this “slide” or “stuck” on the first image of the animation for about 0.5 of a second until it starts doing the normal animation fine (doing his normal 6 image animation), but he moves during the entire time so its like he is sliding on the screen.

It looks very sloppy and not fluid as it should be, also sometimes just normally pressing the dpad he may do a slight slide for 0.5 of a second, and will work fine after (doing his normal 6 image animation).

Any help would be greatly appreciated, I also tried to change the timer on each animation to 400 to 100, down to 10 to see even if that would fix it, but it still is choppy for the 10 one (no change on simulator), making it seem like there is something wrong with the functions.

Here is the movement code, just giving the basic code for how I’m doing the buttons and movement, left out like x,y coordinates and spriteSheet setup (6 x 4 images, 6 down images, 6 left, 6 right, 6 up, first image of every section is the beginning movement image ** The image it is getting stuck on for 0.5 seconds ** )

There is lots of movement code because I was trying to solve all the error checking possibilities, the buttons are all in a group that is static to the screen :

Please point out if I am doing anything wrong, or something is not needed, etc. Learning lua as quick as I can :slight_smile:

– Point in rect, using Corona objects rather than a list of coordinates
local function pointInRect(point, rect) return (point.x <= rect.contentBounds.xMax) and (point.x >= rect.contentBounds.xMin) and (point.y <= rect.contentBounds.yMax) and (point.y >= rect.contentBounds.yMin) end
 

– Quick function to make all buttons uniform
local function newButton(parent) local b = display.newRoundedRect(parent, 0, 0, 38, 36, 10) b:setFillColor(0, 0, 0) return b end

– Create the four buttons and position them
m.l = newButton(m); m.l.x, m.l.y = -30, 0; m.r = newButton(m); m.r.x, m.r.y = 30, 0; m.u = newButton(m); m.u.x, m.u.y = 0, -30; m.d = newButton(m); m.d.x, m.d.y = 0, 30

– Touch listener for D-pad controls
function m:touch(event)
    if not global.allowTouches then return false end
    
    if event.target.isFocus or “began” == event.phase then

        m.prevResult = m.result

        – Set result according to where touch is
            if pointInRect(event, m.l) then m.result = “l”    
                --player:setFrame(1) --set frames!!
                player:setSequence(“moveLeft”)
                md.result = “l”
                player:play()
                
                 
        elseif pointInRect(event, m.r) then m.result = “r”
            player:setSequence(“moveRight”)
            md.result = “r”
            player:play()
            
            
        elseif pointInRect(event, m.u) then m.result = “u”    
            player:setSequence(“moveUp”)
            md.result = “u”
            player:play()
            
            
        elseif pointInRect(event, m.d) then m.result = “d”
            player:setSequence(“moveDown”)
            md.result = “d”
            player:play()
        elseif not pointInRect(event, m) then m.result = “n”
            --this fixes the sliding off dpad
            --md.result = “n”
            --player:play()
            
        end
    end

     – Just a generic touch listener
    if “began” == event.phase then
        display.getCurrentStage():setFocus(event.target)
        event.target.isFocus = true
    elseif event.target.isFocus then
        if “ended” == event.phase or “cancelled” == event.phase then
            display.getCurrentStage():setFocus(nil)
            event.target.isFocus = false
            m.result = “n” – stops him from moving in the direction
            --md.result = “n”
            if md.result == “r” then
                player:setSequence(“stillRight”)
                player:play()
            elseif md.result == “l” then
                player:setSequence(“stillLeft”)
                player:play()
            elseif md.result == “u” then
                player:setSequence(“stillUp”)
                player:play()
            elseif md.result == “d” then
                player:setSequence(“stillDown”)
                player:play()
            end
            md.result = “n”
            bkgRight.alpha = 0.7
            bkgLeft.alpha = 0.7
            bkgDown.alpha = 0.7
            bkgUp.alpha = 0.7
            
        end
        
    end
     – Did the direction change?
    if m.prevResult ~= m.result then m.changed = true end

    return true
    
end

–Game Loop --------------------------

local function gameLoop(event)
    

    – Set player velocity according to movement result
        if m.result == “l” then player:setLinearVelocity(-player.speed, 0)
            bkgLeft.alpha = 1
            bkgRight.alpha = 0.7
            bkgDown.alpha = 0.7
            bkgUp.alpha = 0.7
    elseif m.result == “r” then player:setLinearVelocity(player.speed, 0)
        bkgLeft.alpha = 0.7
        bkgRight.alpha = 1
        bkgDown.alpha = 0.7
        bkgUp.alpha = 0.7
    elseif m.result == “u” then player:setLinearVelocity(0, -player.speed)
        bkgLeft.alpha = 0.7
        bkgRight.alpha = 0.7
        bkgDown.alpha = 0.7
        bkgUp.alpha = 1
    elseif m.result == “d” then player:setLinearVelocity(0, player.speed)
        bkgLeft.alpha = 0.7
        bkgRight.alpha = 0.7
        bkgDown.alpha = 1
        bkgUp.alpha = 0.7
    elseif m.result == “n” then player:setLinearVelocity(0, 0)
        bkgLeft.alpha = 0.7
        bkgRight.alpha = 0.7
        bkgDown.alpha = 0.7
        bkgUp.alpha = 0.7
    end
 

    – reset animation
    if m.changed then
        m.changed = false
        --if m.result ~= “n” then            
                
        if m.result == “n” then
            --player:setSequence(“still”)
            --player:play()
            if md.result == “r” then
                player:setSequence(“stillRight”)    
                player:play()
            elseif md.result == “l” then
                player:setSequence(“stillLeft”)
                player:play()
            elseif md.result == “u” then
                player:setSequence(“stillUp”)
                player:play()
            elseif md.result == “d” then
                player:setSequence(“stillDown”)
                player:play()
            end
        end
    end

—End of game loop --------------------

Runtime:addEventListener(“enterFrame”, gameLoop)

m:addEventListener(“touch”)

– Set up animations
player:setSequence(“stillDown”)
player:play()
 

I can’t see anything there that would cause that problem per se.

The best way to find these bugs is to try to isolate the problem. I’d be inclined to remove your touch event listener, which uses m to communicate with the enterFrame event, and replace it with a bit of dummy code that uses a timer to change direction periodically. If you do that and the animation character is still broken, then you know it’s the animation. Then comment out chunks of code until it behaves itself.

It looks to me like your code is a bit tangled ; you seem do be doing player:setSequence() in your touch listener and your game loop, which is probably not a good idea. It is possible the core of your problem may be all these calls to the animation system is causing a lot of confusion. Try to separate out the different parts of the code a bit.

A bit of clarification the stillDown, etc anything with still means stop animation standing still on down if he press d and lets go of button, or still holding down and goes off of button which is the error checking for both parts. The animation isn’t broken it just takes about 0.5sec for it to start up (1 sprite movement) and work properly when i hold down finger and keep holding down finger to next button, if i press the screen and take my time on the buttons and let go of button to go to next button everything works perfect most the time. But I do see how all the animation calls could be causing a problem, will try to fiddle around with that.

The 2 seperate still movement categories was to fix the button releases/holding down, etc. I will try to isolate the problem more and try the timer test. But still very confused on how to get this program to work properly.

EDIT: I just deleted both the still animation parts (stop animation and stand still), it doesn’t affect the sliding part in the movement animation that I been having problems with. It’s like it takes a while for corona to recheck the function if holding down button to move to next button before it stops and starts again?

I can’t see anything there that would cause that problem per se.

The best way to find these bugs is to try to isolate the problem. I’d be inclined to remove your touch event listener, which uses m to communicate with the enterFrame event, and replace it with a bit of dummy code that uses a timer to change direction periodically. If you do that and the animation character is still broken, then you know it’s the animation. Then comment out chunks of code until it behaves itself.

It looks to me like your code is a bit tangled ; you seem do be doing player:setSequence() in your touch listener and your game loop, which is probably not a good idea. It is possible the core of your problem may be all these calls to the animation system is causing a lot of confusion. Try to separate out the different parts of the code a bit.

A bit of clarification the stillDown, etc anything with still means stop animation standing still on down if he press d and lets go of button, or still holding down and goes off of button which is the error checking for both parts. The animation isn’t broken it just takes about 0.5sec for it to start up (1 sprite movement) and work properly when i hold down finger and keep holding down finger to next button, if i press the screen and take my time on the buttons and let go of button to go to next button everything works perfect most the time. But I do see how all the animation calls could be causing a problem, will try to fiddle around with that.

The 2 seperate still movement categories was to fix the button releases/holding down, etc. I will try to isolate the problem more and try the timer test. But still very confused on how to get this program to work properly.

EDIT: I just deleted both the still animation parts (stop animation and stand still), it doesn’t affect the sliding part in the movement animation that I been having problems with. It’s like it takes a while for corona to recheck the function if holding down button to move to next button before it stops and starts again?