Problem with enemy pathfinding

Hi, i’m new at using this game framework, so as you read in the title, i was creating various enemy’s to follow my player but after a while of having reached it they stop and stop chasing it but what I want is that they chase it infinitely, here you have my code:

local function distanceBetween( pos1, pos2 )
    if (skullVariants ~= nil) then
        local factor = { x = pos2.x - pos1.x, y = pos2.y - pos1.y }
        return mSqrt( ( factor.x * factor.x ) + ( factor.y * factor.y ) )
    end
 end
local function _move ()
    if (skullVariants ~= nil and player ~= nil) then
        local totDist = distanceBetween(skullVariants, player)
        local travTime = 1000 * totDist / 500
        transition.to ( skullVariants, {x=player.x, y=player.y - 32, time=travTime, 0} )
    end
end

Runtime:addEventListener("lateUpdate", _move )

Try this:

local funcition isValid( obj )
    if( obj == nil or 
        obj.removeSelf == nil or 
        type(obj.removeSelf) ~= "function" ) then 
        return false 
    end
    return true
end

local function onComplete( self )
    if ( isValid(player)) then
        local totDist = distanceBetween(self, player)
        local travTime = 1000 * totDist / 500
        transition.to ( skullVariants, { x = player.x, y = player.y - 32, 
                                         time = travTime, 
                                         onComplete = self } )
    end
end

skullVariants.onComplete = onComplete

skullVariants:onComplete()

The problem seems to be that you are setting up a new transition for skullVariants on each frame without canceling any existing transitions.

Like @roaminggamer suggests, waiting for the transition to complete before starting a new transition is one way of solving it.

If you instead want to allow the enemy to change direction more often, you could use a recurring timer and cancel the existing transition before starting a new one. I don’t think an enter frame listener is the way to go here, because that would more or less make the transition meaningless since a new transition will be created every frame. Instead use timer.performWithDelay(someDelay, _move, 0). You could also check if the player has moved since the last transition was started, and if not just allow any existing transition to continue.