为什么这个移动函数不能正常工作?(LUA/Corona SDK)

我正在为一个平台游戏的大学项目工作。

我做了一个库,其中包含一个创建新敌人对象的函数和另一个将该敌人来回移动的函数。

由于如果我创建2个或以上的敌人,这个最后的移动函数只对最后一个敌人起作用,所以这个最后的移动函数正给我带来一些意想不到的麻烦。

这是这个函数:

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就是库名称。

最后 n 个敌人是可以正确地移动的,而前 n-1 个只能向左移动(在创建敌人函数中,我默认定义了这个动作)。

我做错了什么?

[编辑]这是我用来创建敌人的函数(未声明为局部变量的变量在库的开头声明)

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
点赞