function - 为什么这个运动功能不能正常工作?(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
解决方案
推荐阅读
- numpy - 使用matplotlib在.npy文件中绘制二维numpy数组?
- ruby-on-rails - Spree vs Solidus for Shopify 类似的应用程序
- android - 错误:URI 的目标不存在:Flutter 中的“package:nanoid/generate.dart”
- java - BottomNavigation 在带有组件导航的片段中不起作用
- python-3.x - 并行 Pytest 夹具处理
- javascript - 在 owl carousel 中的项目之间添加空间
- c# - “序列没有元素”异常。错误统一,WebGL
- vscode-settings - 如何清理 AppData\Roaming\Code\Service Worker 中的 CacheStorage?
- transactions - 在事务中封装 Drop 和 Add 约束
- javascript - 我正在尝试下载一个包含 PDF 的流,我从 .NET Web API 作为客户端浏览器的提示接收回该 PDF