Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Unknown Error- Can't compare array with fixnum?

Anonymous

Guest

http://www.fiendfiles.com/1165846976.rar

It says it failed to compare an Array with a Fixnum. This is very confusing to me, because the error is occurring on a line that says end - in my Main, no less! If anyone could please help me figure out why I'm getting this absurd error, it would be greatly appreciated! Thank you! :thumb:
 
Take a look at Player class:
Code:
  def initialize
    ...
    @levels = {'guns' => 1, 'jets' => , 'bombs' => 1, 'shield' => 1}
    ...
  end
...
  def update
    ...
    for i in @levels
      i = [i, 4].min
    end
    ...
  end
...

For each iteration, i is actually an index of the hash you made. In other words, i is {'guns' => 1} (but i is now ['guns', 1] after Ruby converted it for you), which is not exactly comparable to a Fixnum when Array#min is called. Truthfully, I have no idea what you're trying to do with that #min method.

You have other errors and numerous scripting errors. It seems that you've misunderstood some things in Ruby. I can't help fix everything for the moment, but here are some things I like to point out. I would also suggest reviewing Ruby tutorials on syntaxes.

Code:
class Colour < Color
  attr_accessor :black
  attr_accessor :white
  attr_accessor :desperate
  def black
    return Color.new(0, 0, 0)
  end
  def white
    return Color.new(255, 255, 255)
  end
  def desperate(percent)
    return Color.new(255 - (255 * percent), 255, 255 * percent)
  end
end

@back_sprite.bitmap.fill_rect(0, 0, 104, 36, Colour.white)
# => Error!

attr_accessor applies both to WRITING and READING. You don't need to specify another reading method if you're using _accessor. If you just want to create special return cases, use attr_writer instead. Also, you DO NOT NEED to specify any attributes to create methods that return instance variables.

You're trying to call Colour as if it was a Module, yet you only specified it as a Class. It's better if you change Colour to Module instead, and removing the attributes as well:
Code:
module Colour
  def self.black
    return Color.new(0, 0, 0)
  end
  def self.white
    return Color.new(255, 255, 255)
  end
  def self.desperate(percent)
    return Color.new(255 - (255 * percent), 255, 255 * percent)
  end
end

@back_sprite.bitmap.fill_rect(0, 0, 104, 36, Colour.white)
# => No error; do stuff.

Next stuff:
Code:
  #--------------------------------------------------------#
  attr_accessor :guns
  attr_accessor :jets
  attr_accessor :bombs
  attr_accessor :shield
  attr_accessor :score
  attr_accessor :fuel
  attr_accessor :speed
  def guns; return @levels['guns']; end
  def jets; return @levels['jets']; end
  def bombs; return @levels['bombs']; end
  def shield; return @levels['shield']; end
  def score; return @score; end
  def fuel; return @fuel; end
  def speed; return @speed; end
  #--------------------------------------------------------#

Again, attr_ is not required to create methods that return instance variables.

Code:
  attr_reader  :score
  attr_reader  :fuel
  attr_reader  :speed

  def guns; return @levels['guns']; end
  def jets; return @levels['jets']; end
  def bombs; return @levels['bombs']; end
  def shield; return @levels['shield']; end
 

Anonymous

Guest

Thanks! I didn't expect a response so quickly, or one that was so detailed! I'll be sure to check out the things you mentioned, and I'll try to be a bit more knowledgeable about syntax and other things in the future. Thank you for your help!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top