문제를 해결하는 방법을 고칠 수 없으므로 상태 클래스 변수에서 10을 벗어나므로 오류라고합니다. 여기 정의되지 않은 메소드`- '에 대해 nil : 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
@he 뒤에 .to_i를 넣으려고했습니다. 이 같은 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