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
– 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()