【问题标题】:Chingu input property on a Chingu::GameWindow causes exceptionChingu::GameWindow 上的 Chingu 输入属性导致异常
【发布时间】:2012-02-15 05:05:29
【问题描述】:

这段代码在 Chingu 中导致了一个非常奇怪的异常,这是一个依赖于 Gosu 2D 游戏框架的 ruby​​ 游戏库:

class StopSignAttack < Chingu::Window

  def initialize
    super(1000, 1000, false)
    self.input = { :escape => :close }
    self.caption = "Stop Sign ATTACK!!!"
    push_game_state(Start.new)
  end

end

Chingu::Window 的 input 属性的值应该只是让游戏窗口在按下退出键时关闭。按下退出键时窗口会关闭,但会出现以下异常:

/Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:63:in `block in dispatch_input_for': undefined method `button_down?' for nil:NilClass (NoMethodError)
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:62:in `each'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:62:in `dispatch_input_for'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:148:in `block in intermediate_update'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:148:in `each'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:148:in `intermediate_update'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:133:in `update'
    from stop_sign_attack.rb:24:in `<main>'

我试过在Chingu::Window中实现close方法,调用超类close方法:

  def close
    super.close
  end

按 ESC 键时出现此异常:

stop_sign_attack.rb:23:in `close': undefined method `close' for nil:NilClass (NoMethodError)
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:19:in `rescue in block (2 levels) in <class:Window>'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:12:in `block (2 levels) in <class:Window>'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:26:in `show'
    from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:26:in `show'
    from stop_sign_attack.rb:28:in `<main>'

感谢任何想法。

【问题讨论】:

    标签: ruby


    【解决方案1】:

    您遗漏了一些允许他人调试的信息。例如,您正在推送一个名为 Start 的游戏状态,但您的示例中没有声明 Start 类。但你还没有分享那部分代码。我创建了以下示例项目来测试你在那里得到了什么:

    require 'chingu'
    
    # A simple GameState-example
    class Start < Chingu::GameState
    end
    
    class StopSignAttack < Chingu::Window
    
      def initialize
        super(1000, 1000, false)
        self.input = { :escape => :close }
        self.caption = "Stop Sign ATTACK!!!"
        push_game_state(Start.new)
      end
    
    end
    
    StopSignAttack.new.show
    

    这工作得很好,当我按下退出时,没有任何异常退出。

    【讨论】:

    • 你的观点很好,但我承认我的故障排除还没有那么深入。我正在寻找意见。
    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多