CORONA:timer.cancel() 返回“尝试索引 nil 值”

2024-01-01

我试图取消在另一个“触摸事件”函数内的“触摸事件”函数中启动的计时器,如下所示:

local function startNewGame(event)
if(event.phase=="ended")then
    local function animationImmaginiOggetti()
      for i=1, 7 do
        transition.to(immaginiOggettiAvvioPartita[i],
                      { time = 200, delay = 0, xScale = 0, yScale = 0, alpha = 0})
      end
    end
    local function removeImmaginiOggetti()
        if immaginiOggettiAvvioPartita[1] then
            for i=1, 11 do
                immaginiOggettiAvvioPartita[i]:removeSelf()
                immaginiOggettiAvvioPartita[i] = nil
            end
        end
    end

    local tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1)
    local tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1)
end
end


local function replayGame(event)
    if(event.phase=="ended")then
        timer.cancel(tmrAIO)
        timer.cancel(tmrRIO)
    end
end

startBTN:addEventListener("touch", startNewGame)
replayBTN:addEventListener("touch", replayGame)

我的问题是电晕卷土重来

“文件: ?Attempt to index a nil value" on timer.cancel (tmrAIO).

我究竟做错了什么?


问题如下,变量tmrAIO and tmrRIO是函数局部的startNewGame,这意味着它们只能从定义的范围内访问startNewGame现在您正尝试从该函数外部访问它们,并且它们没有在该范围内定义,这就是为什么nil value.

解决方案:

local tmrAIO
local tmrRIO

local function startNewGame(event)
    if(event.phase=="ended")then
        local function animationImmaginiOggetti()
          for i=1, 7 do
            transition.to(immaginiOggettiAvvioPartita[i],
                          {time = 200, delay = 0, xScale = 0, yScale = 0, alpha = 0})
          end
        end
        local function removeImmaginiOggetti()
            if immaginiOggettiAvvioPartita[1] then
                for i=1, 11 do
                    immaginiOggettiAvvioPartita[i]:removeSelf()
                    immaginiOggettiAvvioPartita[i] = nil
                end
            end
        end

        tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1)
        tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1)
    end
end

local function replayGame(event)
    if(event.phase=="ended")then
        timer.cancel(tmrAIO)
        timer.cancel(tmrRIO)
    end
end

startBTN:addEventListener("touch", startNewGame)
replayBTN:addEventListener("touch", replayGame)

正如你所看到的,我宣布tmrAIO and tmrRIO范围之外startNewGame使它们可以在该文件内的任何位置访问。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CORONA:timer.cancel() 返回“尝试索引 nil 值” 的相关文章

  • Java:直接从同一类的另一个实例访问私有字段

    我正在写一个equals Object obj 一个类的函数 我发现可以访问私有字段obj来自来电者 因此 不要使用 getter Odp other Odp obj if other getCollection contains ftw
  • 如何在lua中获取shell脚本的返回码?

    我正在lua中执行一个脚本 os execute sh manager scripts update system sh f 我想获得脚本的输出 如果退出状态为 7 则返回 7 I tried local output os execute
  • 全局变量声明

    我是 Python 的初学者 并且已经处理过全局变量的概念 当我以为我理解了这个概念时 我看到了一段简短的代码 证明我错了 message global def enclosure message enclosure def local g
  • Erlang 如何睡觉(晚上?)

    我想在 Erlang 服务器上每隔几个小时运行一次小型清理过程 我知道计时器模块 我在教程中看到一个示例 使用链式计时器 睡眠命令来等待几天后发生的事件 我觉得这很奇怪 我知道 Erlang 进程与其他语言中的进程相比是独一无二的 但是进程
  • 如何终止Lua脚本?

    如何终止 Lua 脚本 现在我在 exit 方面遇到问题 我不知道为什么 这更像是一个 Minecraft ComputerCraft 问题 因为它使用了包含的 API 这是我的代码 while true do if turtle dete
  • 如何测试 UITextField 是否为零?

    我正在尝试制作我的应用程序的一部分 如果该人不更改我的 UITextField 中的空白文本 那么他 她将无法继续下一步 基本上 我想测试 UITextField 的 nil 文本 我已经使用了 if text 方法 但是如果用户单击 UI
  • Lua C API:初始化结构体 C 中的变量矩阵

    我正在尝试使用 Lua C API 创建一个用户数据 并在其中关联一个元表 我将在其中收集矩阵 我无法得到的是如何将初始化矩阵的每个分量设置为零 我按照我的描述编译我的 Lua 模块 C 代码here https stackoverflow
  • 如何让我的 add 命令找到第一个变量和第二个变量的值,然后将它们加在一起?

    vars values function open file lex file end function lex file local data io open file r for char in data lines do Print
  • json_encode 返回 NULL?

    由于某种原因 项目 描述 返回NULL使用以下代码 这是我的数据库的架构 CREATE TABLE staff id int 11 NOT NULL AUTO INCREMENT name longtext COLL
  • 在 Awesome-wm 中为特定应用程序设置窗口布局

    如何配置很棒 以便它可以启动两个窗口对齐的新应用程序 如下所示 xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx 其中 x 是 pidgin 中的对话窗口 是好友列表窗口 一般来说 我想指定右窗口的宽度
  • 如何使用 c# 编写几个精确的计时器(精确到 10 毫秒间隔)

    我已经开始使用 C VS2010 Net Fw 4 0 进行桌面应用程序开发 涉及多个计时器 起初 我使用的是系统定时器为了通过 USB 将数据发送到数据总线 我的观点是 我需要以几个特定的 时间间隔发送不同的周期性二进制消息 例如 10m
  • 通过 NULL 指针访问类成员

    我正在尝试 C 发现下面的代码非常奇怪 class Foo public virtual void say virtual hi std cout lt lt Virtual Hi void say hi std cout lt lt Hi
  • Java,将 null 分配给对象和仅声明之间有什么区别

    之间有什么区别 Object o null and Object o 仅声明 有人可以回答我吗 这取决于您声明变量的范围 例如 局部变量没有default values在这种情况下你将不得不分配null手动 在这种情况下实例变量分配 nul
  • 结构体如何存储在内存中?

    我有一个struct iof header在我的代码中 我确定它的宽度是 24 字节 我执行 sizeof iof header 它返回 32 字节宽 问题1为什么是 32 字节宽而不是 24 字节宽 问题2包括其成员在内 结构体如何存储在
  • 当没有数据时,空 json 对象而不是 null -> 如何使用 gson 反序列化

    我正在尝试使用 Google 的 gson 库解析 json 数据 但 json 数据表现不佳 当一切正常时 它确实看起来像这样 parent child one some String child two 4711 child one应该
  • 在出错之前如何检查 nilClass 的未定义方法?

    我目前正在使用以下内容 20 p Status p 但是 我仍然收到以下错误 ActionView TemplateError undefined method status for nil NilClass on line 20 of a
  • 使用 rpy2 将 NULL 从 Python 转换为 R

    在 R 中经常NULL值用作默认值 使用 Python 和 RPy2 如何显式提供NULL争论 None不可兑换 NotImplementedError 字符串 NULL 只会被转换为字符串 并在执行过程中导致错误 采取以下示例 使用tsi
  • 如何在 Lua - Lightroom 插件中使用 HMAC

    首先我要提的是我对 Lua 真的很陌生 如果你认为我的问题太愚蠢 请耐心等待 这是我的要求 我需要使用 HMAC sha256 进行 Lightroom 插件开发 因为我使用它是为了安全 我试图使用这个但没有运气https code goo
  • .net 应用程序中的内存泄漏

    我正在 VB net 2005 中开发一个桌面应用程序 该应用程序包含一个间隔为 1 分钟的计时器 每次计时器计时 就会执行一组函数 大部分与数据库相关 最初 应用程序运行良好 在进程 任务管理器 中 每次调用计时器时 CPU 使用率都会达
  • 如何从 f# 返回一个空元组到 c#? [复制]

    这个问题在这里已经有答案了 我有这个类型正确的 C 函数 static System Tuple

随机推荐