I tried to call abs
for my math in the code but the terminal reports that abs is unidentified when it is supposed to be in the language itself.
The error message: customcode.rb:58:in <class:Survivor>: undefined method abs for nil:NilClass (NoMethodError)
vel_sum = (@vel_x).abs (@vel_y).abs\r
^^^^
This is the part of the code where the error lies
def acceleration(direction)
case direction
when :forwards
@vel_x = Math.sin(@sprite.rotate*Math::PI/180)
@vel_y -= Math.cos(@sprite.rotate*Math::PI/180)
when :backwards
@vel_x -= Math.sin(@sprite.rotate*Math::PI/180)
@vel_y = Math.cos(@sprite.rotate*Math::PI/180)
end
end
vel_sum = @vel_x.abs @vel_y.abs
if vel_sum > 2
@vel_x = @vel_x*2
@vel_y = @vel_y*2
end
vel_sum = @vel_x.abs @vel_y.abs
@vel_x and y should be integers so I honestly do not know why It does not work. I hope some passerby could help me with this one.
CodePudding user response:
The vel_sum...
code is outside of the def acceleration
method, therefore the program does not understand @vel_x and @vel_y when it is called.
def acceleration(direction)
case direction
when :forwards
@vel_x = Math.sin(@sprite.rotate*Math::PI/180)
@vel_y -= Math.cos(@sprite.rotate*Math::PI/180)
when :backwards
@vel_x -= Math.sin(@sprite.rotate*Math::PI/180)
@vel_y = Math.cos(@sprite.rotate*Math::PI/180)
end
vel_sum = @vel_x.abs @vel_y.abs
if vel_sum > 2
@vel_x = @vel_x*(2/vel_sum)
@vel_y = @vel_y*(2/vel_sum)
end
end
This fix should work..