Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:46:07+00:00 2026-06-01T04:46:07+00:00

I have a MapTile class which holds a tile sprite and the tile’s attribute.

  • 0

I have a MapTile class which holds a tile sprite and the tile’s attribute. I want to create a 2D array which holds for example 100 tiles in a 10×10 grid. I have drawn a tile map using a plain old 2D array holding only the tile sprite and that worked fine. However, now when I assign a tile sprite to the 2D Array named mapData containing MapTile classes and using mapData[i][j].tileSprite = tileNum, every element in the column is assigned the tileNum value. I have tried everything I could think of to get this to work. I am a C++ programmer new to Ruby.

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)

mapData[1][j].tileSprite = tileNum  #Now every tileSprite in column 1 is tileNum

Solution

Changed the array2D method to

def array2D(width,height)
a = Array.new(width) { MapTile.new(10,0)}
a.map! { Array.new(height) { MapTile.new(10,0) } }
return a
end

Thanks Michael!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:46:08+00:00Added an answer on June 1, 2026 at 4:46 am

    You are using the same object for each array element. Try Array.new(width) { MapTile.new(123,0) } instead.

    From the docs:

    # only one copy of the object is created 
    a = Array.new(2, Hash.new)
    
    # here multiple copies are created 
    a = Array.new(2) { Hash.new }
    

    BTW: There are some things in your code that are rather unidiomatic Ruby, you may want to run that by the Code Review Stack Exchange.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have yet to find a good reference on this topic. For this example
To begin with, I have a database that holds map data for a game
Ok, so I have a 2D Array that is initialised with values from a
I was wondering the best way to load a big array which stores map
Have finally got a responsive site working (of a fashion). What I want to
I'm relatively new to Objective-C and I have an enum with a corresponding array
Have just started using Visual Studio Professional's built-in unit testing features, which as I
basically i am reading image bytes from file i have a loop which has
I have a high-resolution image that I want to use as a tiled map
I have a large non-map image that I want to allow people to view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.