Jump to content

? mapY array


hasya
 Share

Recommended Posts

Hello dear html5 cracks,

so im following this tutorial about a javascript tile based theme and i came across this line:

 

tile = (engine.currentMap[mapY] && engine.currentMap[mapY][mapX]) ? engine.currentMap[mapY][mapX] : {ground: 0};

now as i understand it checks wether there is information in the array or not, if there is none it draws a black tile.

now i tryed writing :

tile = (engine.currentMap[mapY] && engine.currentMap[mapY][mapX]) ? engine.currentMap[mapY][mapX] : {ground: 0};

and it wont work anymore. now to my question why cant i just check if there is a tile at mapY,mapX?

 

thx already,

 

 

whole code bit

engine.map.draw = function(){   var i, j, tile;   var mapX = 0;   var mapY = 0;   var iMax = engine.screen.tilesX + engine.viewport.overflowTile;   var jMax = engine.screen.tilesY + engine.viewport.overflowTile;   for(j = -engine.viewport.overflowTile; j<jMax; j++)   {      for(i = -engine.viewport.overflowTile; i<iMax; i++)      {         mapX = i + engine.viewport.x;         mapY = j + engine.viewport.y;         tile = (engine.currentMap[mapY] && engine.currentMap[mapY][mapX]) ? engine.currentMap[mapY][mapX] : {ground: 0};         engine.tile.draw(i, j, tile);      }   }};
Link to comment
Share on other sites

This is because the code is a little hacky. If you take away the first statement the engine will crash, because there is a possibility you tried to check an element of 'undefined' ie:

 

array[0][0] is OK, but maybe:

 

array[30] is undefined, so you can't legally write array[30][0], because the interpreter will complain that you tried to access [0] of 'undefined', which is obviously nonsensical. If you have the first statement the interpreter doesn't bother looking at the second one; it's already evaluated as false.

 

Does that make sense?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...