The error occurs in Map class responsible for drawing all the tiles to the screen. It was working fine, so long i did not change the conversion of reading map data from the file. I made additional notation to the file so each tile is now represented with two characters (tile code handled by Tile class and item code handled by Item class). The exemplar file may look like this:

So I try to keep the loading simple for now, and write loading map data from file into RAM in Map's constructor:

File.foreach(f_map).with_index do |line, y|
  line.each_char.lazy.each_slice(2).map(&:first).with_index do |char, x|
    if char != ?\n and char.to_s != ' '
      @tiles[[x, y]] = Tile.new(x * @tile_size, y * @tile_size,
                                @tileset, char)
      @width = x
  @height = y

And the draw function in Map reminds the same:

def draw
  for y in 0..@width
    for x in 0..@height
      @tiles[[x, y]].draw

Then I get error when calling .draw:

[..]lib/map.rb:41:in `block (2 levels) in draw': undefined method `draw' for nil:NilClass (NoMethodError)

I did spend already few hours trying to solve this problem and get no clue what can be wrong. The data seems to bee good, here is state of @tiles[[0, 0]] inside Map.draw before calling Tile.draw:

@tiles[[0, 0]]
=> #<Tile:0x0000558e053b2688
   @img=#<Gosu::Image:0x0000558e053b3380 @__swigtype__="_p_Gosu__Image">,
    [#<Gosu::Image:0x0000558e053b3380 @__swigtype__="_p_Gosu__Image">,
     #<Gosu::Image:0x0000558e053b3330 @__swigtype__="_p_Gosu__Image">,
     #<Gosu::Image:0x0000558e053b32e0 @__swigtype__="_p_Gosu__Image">,
     #<Gosu::Image:0x0000558e053b3268 @__swigtype__="_p_Gosu__Image">,
     #<Gosu::Image:0x0000558e053b3218 @__swigtype__="_p_Gosu__Image">],

Maybe explaining what the .draw actually do will be useful as well. So Tile.draw simply calls it's instance of @spirite.draw (Spirite class). The draw function in Spirite is pretty simple:

@img.draw(@x, @y, @zorder, 1, 1, @color, :add)

And @img is simply something like:

@texture_pack = Gosu::Image.load_tiles(
  "../data/graphics/texturepack_dungeon.png", 32, 32 )
@img = @texture_pack[@type.to_i]

Well, I find the problem now. In Map.draw method, in stead of:

for y in 0..@width
  for x in 0..@height

I should write

for y in 0..@height
  for x in 0..@width

That's why there was not correct amount of iterations trough tiles, so simply I overflow the stack.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.