【问题标题】:Ruby Gosu crash error: undefined method `*' for nil:NilClassRuby Gosu 崩溃错误: undefined method `*\' for nil:NilClass
【发布时间】:2022-11-14 07:11:09
【问题描述】:
def update
        if button_down?(Gosu::KB_SPACE) && !@pressed
            @y -= 30
            @pressed = true
        elsif !button_down?(Gosu::KB_SPACE)
            @pressed = false
        end
        if @vel.nil?
            @vel *= 4
            @y += @vel * 0.05
        end
    end

每次我为@vel 添加一个运算符时,它都会给我这个错误。为什么?

尝试检查变量是否为 nil 以运行但对我没有用

【问题讨论】:

  • if @vel.nil? :之后的所有内容仅在@vel 等于 nil 时运行。然后你通过 @vel *= 4 继续,这是行不通的。 nil*4 - 您期望的结果是什么?你是说unless @vel.nil?

标签: ruby rubygems gosu


【解决方案1】:

条件 @vel.nil? 仅在 @velnil 时返回 true。你想要相反的,要么使用!@vel.nil?,要么使用unless关键字而不是@vel.nil?之前的if关键字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多