I am a C++ programmer new to Ruby and someone suggested that I post my code here since it doesn't quite match Ruby standards. Is there a more Ruby way of doing this?
I have created a 2D array full of MapTile class objects and then I draw the tiles to the screen by iterating through the 2D Array using a for loop. Here is some code.
class MapTile
attr_accessor :tileSprite, :attribute
def initialize(sprite, attr)
@tileSprite = sprite
@attribute = attr
end
def tileSprite
@tileSprite
end
def attribute
@attribute
end
end
def array2D(width,height)
a = Array.new(width, MapTile.new(123,0))
a.map! { Array.new(height, MapTile.new(123,0)) }
return a
end
@mapData = array2D(@mapSize,@mapSize)
for i in 0..@mapSize - 1
for j in 0..@mapSize - 1
mapData[i][j].tileSprite = tileNum
@tile.draw(mapData[i][j].tileSprite)
end
end
@mapSize.times { |i| some_operation }
– Can Hanhan Apr 1 '12 at 21:06