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