0

I'm making a 2D isometric game where the tile map is held in a 2D array. Here's what a 16*16 map looks like just so you get the idea:

enter image description here

Before the game loop begins the size and contents of the map array must be initialized and I have noticed that this is one of the biggest bottlenecks when loading the program. If the map is 5000*5000 it can take about 7 seconds on my relatively high end computer (which i figure seems pretty slow).

Is this more a problem with the speed of javascript or is my code doing something wildly inefficient?

The array which holds the tile contents is initialized like so:

// Create Array to represent tile contents
mapArray = new Array(mapWidth);
for (var i = 0; i < mapWidth; i++) {
    mapArray[i] = new Array(mapHeight);
    for (var j = 0; j < mapHeight; j++) {
        mapArray[i][j] = new Tile("water", 0, false);
    }
}

and the objects which that array hold is the tile class below:

// Tile class
function Tile(type, height, mouseOver) {
    // Add object properties like this
    this.type = type;
    this.height = height;
    this.mouseOver = mouseOver;
}
2
  • 2
    Consider that you are loading 5000*5000 = 25,000,000 tile objects into memory. That's a lot! It may make more sense to initialize objects that are local to the current player's location. Think MineCraft, where only the nearest 144x144 area is loaded at any one time. Commented May 24, 2014 at 18:10
  • Hmmm, that is food for thought. Thank you. I suppose exactly what I want to do with the game engine will ultimately determine if the method you describe above is best. For example, if I want to implement some soft of mini-map overview, that would require that the whole map was already initialized in order to view it. Commented May 24, 2014 at 18:20

1 Answer 1

1

If you're registering individual event listeners to each tile, that could certainly slow down your program. Consider cutting down the number of listeners by only listening to mouseover/mousemove events on the container, and then deriving the tile from the (x, y) coordinates of the mouse.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the mouse over variable is actually unused and I'm already doing it how you suggest but I suppose that also means it's a variable which is being declared 25,000,000 times more than it needs to be! I'll remove that...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.