我無法弄清楚如何解決這個問題,因此它需要遠離健康類變量,因爲它表示它是一個錯誤。未定義的方法' - '爲零:NilClass(NoMethodError)
/home/will/Code/Rubygame/objects.rb:61:in `attacked': undefined method `-' for nil:NilClass (NoMethodError)
from ./main.rb:140:in `update'
from /var/lib/gems/2.3.0/gems/gosu-0.10.8/lib/gosu/patches.rb:140:in `tick'
from /var/lib/gems/2.3.0/gems/gosu-0.10.8/lib/gosu/patches.rb:140:in `tick'
from ./main.rb:197:in `<main>'
下面是主要代碼:
def update
@player.left if Gosu::button_down? Gosu::KbA
@player.right if Gosu::button_down? Gosu::KbD
@player.up if Gosu::button_down? Gosu::KbW
@player.down if Gosu::button_down? Gosu::KbS
if Gosu::button_down? Gosu::KbK
@player.shot if @player_type == "Archer" or @player_type == "Mage"
if @object.collision(@xshot, @yshot) == true
x, y, health = YAML.load_file("Storage/info.yml")
@object.attacked #LINE 140
end
end
end
而這裏就是@ object.attacked導致:如果需要
def attacked
puts "attacked"
@health -= 10 #LINE 61
@xy.insert(@health)
File.open("Storage/info.yml", "w") {|f| f.write(@xy.to_yaml) }
@xy.delete_at(2)
if @health == 0
@dead = true
end
end
而且YAML文件:
---
- 219.0
- 45.0
- 100.0
我試着把@to_i放在@he ALTH這樣的:
@health.to_i -= 10
,但它只是帶來了另一個錯誤說:
undefined method `to_i=' for nil:NilClass (NoMethodError)
我覺得你的問題其實是'X,Y,健康= YAML.load_file(「存儲/ info.yml」)'我假設這應該是'@x,@y,@health = YAML.load_file(「Storage/info.yml」)'實例變量而不是局部變量。 – engineersmnky