1

我正在使用最近每日构建的Corona SDK(版本 2001.562)为现有应用程序添加陀螺仪支持。不幸的是,我似乎无法获得event-handling陀螺仪触发的功能。该应用程序在iPod touch 版本 4.3.3上运行。

我将陀螺仪连接到一个事件处理程序,如下所示:

if system.hasEventSource("gyroscope") then
    feedbackFile = io.open(system.pathForFile("log.txt", system.DocumentsDirectory), "a");
    feedbackFile:write((os.clock()-startupTime).."\tgyroscope on\n");
    io.close(feedbackFile);
    Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived )
else
    feedbackFile = io.open(system.pathForFile("log.txt", system.DocumentsDirectory), "a");
    feedbackFile:write((os.clock()-startupTime).."\tgyroscope off\n");
    io.close(feedbackFile);
end

当我在设备上启动应用程序,然后关闭它并下载资源文件时,我发现其中log.txt包含带有 atimestamp和“陀螺仪开启”的行。目前很好!

关于事件处理功能:

local function onGyroscopeDataReceived(event)

    feedbackFile = io.open(system.pathForFile("log.txt", system.DocumentsDirectory), "a");
    feedbackFile:write((os.clock()-startupTime).."\tgyroscope reading delta="..event.deltaRotation..",x="..event.xRotation..",y="..event.yRotation..",z="..event.zRotation.."\n");
    io.close(feedbackFile);
end

这行信息永远不会出现在log.txt文件中!

请指教。提前致谢!

4

2 回答 2

2

问题是 event.deltaRotation 不存在。您可能是指 event.deltaTime。

然后当你连接一个 nil 值时,Lua 会抛出一个错误,你的编写代码永远不会完成。(当您在设备上遇到 Lua 错误时,最新的每日构建现在会打印出一条消息。)

该文档显示了如何计算您自己的 deltaDegrees 或 deltaRadians:http: //developer.anscamobile.com/reference/index/events/gyroscope/eventxrotation

于 2011-08-18T00:55:44.057 回答
0

只是一个疯狂的猜测,但可能永远不会调用您的侦听器 --- 我注意到您的 onGyroscopeDataReceived 函数是本地的。如果是这种情况,那么您需要确保在调用 addEventListener 之前声明了变量。

于 2011-08-17T23:44:17.633 回答