【发布时间】:2017-10-02 09:24:27
【问题描述】:
我不知道如何修复它,所以它会从健康类变量中删除十个,因为它说这是一个错误。
/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>'
这是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 放在 @health 之后,如下所示:
@health.to_i -= 10
但它只是带来另一个错误说:
undefined method `to_i=' for nil:NilClass (NoMethodError)
【问题讨论】:
-
我认为您的问题实际上是
x, y, health = YAML.load_file("Storage/info.yml")我假设这应该是@x, @y, @health = YAML.load_file("Storage/info.yml")实例变量而不是局部变量。
标签: ruby methods undefined libgosu