首页 > 解决方案 > 为什么这个运动功能不能正常工作?(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-enemy 正确移动,前一个 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

标签: functionluaprojectcoronasdk

解决方案


推荐阅读