我正在使用 RubyGosu
和Chipmunk
gems 开发游戏。我在名为的文件中有以下类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 0...@hBullets.count
@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 `'