hasya Posted November 6, 2014 Share Posted November 6, 2014 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 bitengine.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); } }}; Quote Link to comment Share on other sites More sharing options...
Bananaoomarang Posted November 6, 2014 Share Posted November 6, 2014 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.