Ruby attr_accessor 未被读取

发布于 2024-12-22 13:28:13 字数 1255 浏览 1 评论 0原文

我正在使用 GosuChipmunk gems 使用 Ruby 开发游戏。我在名为 HeroBullets.rb 的文件中有以下类:

require 'gosu'

class HeroBullets
  attr_accessor :y
  def initialize(window)
    @x = 20
    @y = 0
  end
end

我知道需要从另一个文件 Physics.rb 访问此类,该文件处理所有 Chipmunk< /代码> 代码。

在顶部我有:

require 'chipmunk'

load 'HeroBullets.rb'

class Physics
   attr_accessor :play_area 

def initialize(window)

    @hBullets = Array.new(25)
    @hBullets << HeroBullets.new(window)
    @hBullets << HeroBullets.new(window)
end

再往下有:

  def fire_arrow(y)
    for i in [email protected]
      @bullet = @hBullets[i]
      if(@bullet.y == y)
        @hBullets[i].active = true
      end
    end
  end

我得到的错误是:

Physics.rb:112:in block in fire_arrow': undefined methody' for nil:NilClass 
(NoMethodError) from Physics.rb:110:in each' from Physics.rb:110:infire_arrow'
from FileManager.rb:90:in fireHero' from .../lib/main.rb:90:inupdate' from .../lib/main.rb:129:in `'

I am developing a game with Ruby using the Gosu and Chipmunk gems. I have the following class in the file named HeroBullets.rb:

require 'gosu'

class HeroBullets
  attr_accessor :y
  def initialize(window)
    @x = 20
    @y = 0
  end
end

I know need to access this class from another file, Physics.rb which handles all the Chipmunk code.

At the top I have:

require 'chipmunk'

load 'HeroBullets.rb'

class Physics
   attr_accessor :play_area 

def initialize(window)

    @hBullets = Array.new(25)
    @hBullets << HeroBullets.new(window)
    @hBullets << HeroBullets.new(window)
end

And further down there is:

  def fire_arrow(y)
    for i in [email protected]
      @bullet = @hBullets[i]
      if(@bullet.y == y)
        @hBullets[i].active = true
      end
    end
  end

The Error I get is:

Physics.rb:112:in block in fire_arrow': undefined methody' for nil:NilClass 
(NoMethodError) from Physics.rb:110:in each' from Physics.rb:110:infire_arrow'
from FileManager.rb:90:in fireHero' from .../lib/main.rb:90:inupdate' from .../lib/main.rb:129:in `'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋凉 2024-12-29 13:28:13

问题是,如果 @hBullets 有 10 个元素,@hBullets.count 将输出 10,但 @hBullets[10]< /code> 不起作用,因为数组的索引从 0 开始,而不是从 1 开始。第十个元素将位于 @hBullets[9] 中。您收到错误消息是因为您尝试访问的元素为 nil,而不是因为“attr_accessor 未被读取”。

话虽如此,Ruby 提供了更简单的方法来迭代数组。我会这样重写您的代码:

def fire_arrow(y)
  @hBullets.each do |bullet|
    bullet.active = true if bullet.y == y
  end
end

您的代码的另一个问题是您像这样初始化一个新数组:

@hBullets = Array.new(25)

这将创建一个包含 25 个元素的数组,这些元素全部为 nil。您应该从一个空数组开始:

@hBullets = Array.new

或者:

@hBullets = []

The problem is that if @hBullets has 10 elements, @hBullets.count will output 10, but @hBullets[10] does not work, because the index of an array starts at 0 not at 1. The tenth element will be in @hBullets[9]. You get the error message because the element you are trying to access is nil, not because "attr_accessor is not being read".

That being said, Ruby offers much easier ways to iterate over an array. I would rewrite your code like this:

def fire_arrow(y)
  @hBullets.each do |bullet|
    bullet.active = true if bullet.y == y
  end
end

Another problem with your code is that you initialize a new array like this:

@hBullets = Array.new(25)

This creates an array with 25 elements that are all nil. You should start with an empty array instead:

@hBullets = Array.new

Or:

@hBullets = []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文