Need help debugging my code for movement (video provided)

Hello all, I am new to Corona, so I figured I would post this in the newbie questions.

I have been learning how to program as I discover Lua and Corona. The forum is very helpful when it comes to finding answers and I appreciate that from you all.

I have managed to put together a piece of code that has worked just fine for moving my characters around the screen. Unfortunately, I have come across a bug in the code if I move the characters to rapidly. In the video shown you can see that if I try to move my character again before they finish their initial movement it can cause a glitch at times. Instead of trying to explain it in text I’ll just let you watch the video to see how the character jumps around.

Video: https://youtu.be/c8UeSQIRMIA

(at about 0:22 - 0:30 I stop trying to glitch the game and show you how the characters should move)

I show the code in my video, but in case you cannot read it I’ll post it here.

function moveChar(event) if event.phase == "ended" and activeChar ~= nil then -- calculate distance to help move at constant speed local function distBetween(x1, y1, x2, y2) local xFactor = x2 - x1 local yFactor = y2 - y1 local dist = math.sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end -- actual movement if event.phase == "ended" and activeChar ~= nil then if event.y \< 200 then event.y = 200 -- this is top boundry probably will change once I make universal for all apps end if event.y \> 700 then event.y = 700 -- this is bottom boundry probably will change once I make universal for all apps end transition.moveTo (activeChar, {x=event.x, y=event.y, time= distBetween(activeChar.x, activeChar.y, event.x, event.y)/.15}) activeChar = nil end end end -------------------------- -- Select Character -------------------------- --Red function charTouched\_R(event) -- print() if event.phase == "began" then activeChar = angryRed depthSort(charGroup) -- need to find a better place to put this end end --Green function charTouched\_G(event) if event.phase == "began" then activeChar = crazyGreen depthSort(charGroup) -- need to find a better place to put this end end

And of course I have my event listeners

angryRed:addEventListener( "touch", charTouched\_R ) crazyGreen:addEventListener( "touch", charTouched\_G ) background:addEventListener( "touch", moveChar )

Any help in fixing my code so the bug gets squashed and an improvement to my movement would be amazing

Thanks and I hope my first post is a sufficient one :slight_smile:

Hi @EthanDunlap,

Welcome to Corona! The issue you’re seeing is caused because you’re sometimes stacking up multiple transitions, so a new transition may begin before the previous one has finished, and then you get two transitions “fighting against each other” while they both attempt to resolve. The best solution is to cancel any running transition before you begin another one. You can do this by either assigning the transition to a known “handle” variable and then using that to both assign each transition and cancel the previous one (if there is one). Another option is to use the “tag” assignment and assign a specific tag name to a transition, then you can cancel a running transition by passing that tag name to transition.cancel()… basically the same idea as the handle method, just possibly a bit easier since you don’t have to worry about the handle assignment.

https://docs.coronalabs.com/api/library/transition/cancel.html

Take care,

Brent

Thanks Brent, i’ll get to work on figuring that out.

bug squashed! I simply added transition.cancel(activeChar) after the first if then conditional statement on line 46 and it worked. I’ve been messing with it in the tester for a few minutes and have not been able to re-create the bug.

Thanks again for the help, I’m having a blast with Corona :slight_smile:

Hi @EthanDunlap,

Welcome to Corona! The issue you’re seeing is caused because you’re sometimes stacking up multiple transitions, so a new transition may begin before the previous one has finished, and then you get two transitions “fighting against each other” while they both attempt to resolve. The best solution is to cancel any running transition before you begin another one. You can do this by either assigning the transition to a known “handle” variable and then using that to both assign each transition and cancel the previous one (if there is one). Another option is to use the “tag” assignment and assign a specific tag name to a transition, then you can cancel a running transition by passing that tag name to transition.cancel()… basically the same idea as the handle method, just possibly a bit easier since you don’t have to worry about the handle assignment.

https://docs.coronalabs.com/api/library/transition/cancel.html

Take care,

Brent

Thanks Brent, i’ll get to work on figuring that out.

bug squashed! I simply added transition.cancel(activeChar) after the first if then conditional statement on line 46 and it worked. I’ve been messing with it in the tester for a few minutes and have not been able to re-create the bug.

Thanks again for the help, I’m having a blast with Corona :slight_smile: