【问题标题】:LUA ERROR running the app on android在 android 上运行应用程序的 LUA 错误
【发布时间】:2016-05-14 19:10:08
【问题描述】:

我是一个没有太多经验的新程序员;作为一个好的菜鸟,您将看到的下一个代码行很可能会显示大量错误或不精确,因此我提前为自己辩解。 所以,情况如下: 最近我正在与一种新的编程语言(显然对我来说是新的)进行交互,即 LUA​​(使用 corona sdk)。我接触了编写简单代码的语言。 现在我正在尝试执行一个程序,该程序只需显示两个受重力影响的矩形。当我尝试使用 CORONA 在我的 PC 上模拟应用程序时,它可以正常工作;在为 android OS 构建它之后,将它安装在我的手机上并运行它,结果是一条错误消息,如下所示:

这是错误信息:

"main.lua.35: ERROR: table expected. 如果这是一个函数调用,你可能使用了 '.'而不是 ':' "

这是代码:

local physics = require("physics")
physics.start()

local _W = display.contentWidth
local _H = display.contentHeight

local platform = display.newRect(
    --x =
     _W/2,
    --y = 
     _H/2,
    --width = 
     100,
    --height = 
     100
)

platform.surfaceType = "rectangle"  

local myImage = display.newImage("icon.png",_W/2,50)

local iconCollision = {
    friction = 0.0, --attrito
    bounce = 0.0, --rimbalzo (forse)
    isSensor = true --collisione con altri oggetti
}

physics.addBody(platform,"static") 
physics.addBody(myImage,"dynamic",iconCollision) --this is line 35

platform.collision = onCollision

local function onGyroscopeDataReceived( event )
    local deltaRadians = event.zRotation * event.deltaTime
    local deltaDegrees = deltaRadians * (180/math.pi)
end

local function dragImage(event)
    local t=event.target
    print(event.phase)
    if(event.phase == "moved") then 
        t.x = event.x
        t.y = event.y
    end
end

local function onCollision( event )
        if ( event.phase == "began" ) then
            print("toccato")
        end
end

if system.hasEventSource( "gyroscope" ) then
    Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived )
end

Runtime:addEventListener("collision",onCollision)
myImage:addEventListener("touch",dragImage)

我会非常感谢那些愿意帮助我的人。

【问题讨论】:

  • 错误信息完全解释了它。它说它期待一个表作为函数参数(实际上是第一个参数),它建议用: 替换.。这是因为tab:func(arg1, arg2)tab.func(tab, arg1, arg2) 的快捷方式。程序会自动插入表格作为函数的第一个参数。
  • 尝试调用physics:addBody(platform, "static")physics:addBody(myImage, "dynamic", iconCollision) 甚至physics.addBody(phisycs, platform,"static")physics.addBody(physics, myImage,"dynamic",iconCollision),它应该可以工作。
  • platform.collision = onCollision必须在函数体onCollision之后
  • addEventListener 的第二个参数必须是在其中定义函数的对象(而不是函数本身)。见docs and examples。您不能使用独立功能,例如onCollisiondragImage。这些函数的第一个参数必须是 selfonCollision(self, event)。您可以指定一个包含此功能的对象,例如:Runtime:addEventListener("collision", platform)
  • 顺便说一句,您的第 35 行是完全正确的。您可以向 Corona 团队发送错误报告:错误消息错误地指向导致错误的行。

标签: lua coronasdk


【解决方案1】:

我想event.zRotation 可能是null。在 iOS 上,您可以使用适当的UIRequiredDeviceCapabilities。在building.settings 中添加此UIRequiredDeviceCapabilities = { ["gyroscope"]=true }。在 Android 上设置 usesFeatures = { { name="android.hardware.sensor.gyroscope", required=true } }

【讨论】:

    【解决方案2】:

    在磁盘上查找图片的名称,“icon.png”,大小写有关系,在Android设备上可能会导致错误,在模拟器中不显示。 如果它不加载 myImage:addEventListener 会显示一个错误,也许是你看到的那个。 还尝试排除代码片段,然后在设备上运行程序。我发现这种方式与模拟器不同。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2021-07-11
      • 2018-07-29
      相关资源
      最近更新 更多