I prefer not to use FSMs for movement, but it can work in certain cases. One of the things FSMs are great for is animations, which is what the linked example would easily tie into. The key thing to notice about the code you are working from is that lateral motion is considered in every one of their states using their function agent:getMovement(). Even in the ...
Most games use quadtree structures to manage the colisions, you should read about it. Anyways, if you are having lag after X levels is possible that you are storing all the objects and checking them out every loop, and not just the object in the current scene or level.
I asked just this question on SO a while ago, same language aswell, and no one didn't really care, so when I found the solution I didn't really bother to put it up there, but now more people are interested and I am glad for that, so now I will post the answer here. Here's my original code, it's the exact same thing as yours, just a bit more clear of what ...
Only top voted, non community-wiki answers of a minimum length are eligible