Why this movement function is not working properly? (LUA/Corona SDK)

47 Views Asked by At

I'm workin on a university project for a platform videogame.
I made a library with a function to create a new enemy object and another function to move this enemy back and forth.
This last movement function is giving me some unexpected trouble because if I create 2+ enemies it works only for the last one.
This is the function:

function M.moveTerra(terra)

if  terra.x < terra.limitSx then
        terra:setSequence("terraRight")
            terra:play()
            terra:setLinearVelocity(200,0)
    end
    if terra.x > terra.limitDx then
        terra:setSequence("terraLeft")
            terra:play()
            terra:setLinearVelocity(-200,0)
    end
end

Runtime:addEventListener("enterFrame", M.moveTerra)

M is the library name, of course.
The last n-enemy moves correctly, the previous n-1 moves to the left only (in the create enemy function I defined this movement by default).
What I'm doing wrong?

[Edit] This is the function I use to create enemies (the vars not declared as local are declared at the beginning of the library)

function M.new(a,b)

local terraOpt = {numFrames = 16, width = 250, height = 100 } 
    local terraSheet = graphics.newImageSheet("map/nemicoTerra.png", terraOpt)

    local terraSeqs =   { 
                    {count = 8,
                     start = 1,
                     name = "terraRight", 
                     loopCount = 0, 
                     loopDirection = "forward",
                     time = 1000
                    },
                    {count = 8,
                     start = 9,
                     name = "terraLeft", 
                     loopCount = 0, 
                     loopDirection = "forward",
                     time = 1000
                    }
                }
    
                    
    terra = display.newSprite(terraSheet,terraSeqs) 
    local terraShape = {-125,-50,125,-50,-125,50,125,50} 
    physics.addBody(terra,"kinematic",{friction = 1.0, bounce=0.0,density=0.3, shape=terraShape, isSensor=true}) 
    terra.isFixedRotation=true
    terra.type = "terra"
    terra.x = a
    terra.y = b
    terra.limitSx = a-200
    terra.limitDx = a+200
    
    terra:setSequence("terraLeft")
    terra:play()
    terra:setLinearVelocity(-200,0)
    
    return terra
end
0

There are 0 best solutions below