English subtitles

← Tile Packet - HTML5 Game Development

Get Embed Code
5 Languages

Showing Revision 6 created 05/24/2016 by Udacity Robot.

  1. >> And so here's that code in practice, the first thing we do is loop through
  2. each of the tileSets images, checking the firstgid against the tileIndex. Once
  3. we find the correct one, we'll break out knowing that i is the proper index.
  4. Once we find this, we can set the pkt.img value to equal the image of the atlas,
  5. and then we go through and do a little bit of math to determine what the tile
  6. offset is. Now, recall that the tile index is actually added to the firstgid for
  7. this array. So, 167 as a tile index, is actually atlas with gid1. Tile 166. We
  8. remedy this by actually subtracting the tileIndex from the firstgid, giving us
  9. localIdx. From there, we can do a little simple math, given the localIdx and the
  10. numXTiles horizontally for this atlas to give us to give us our x and y indexes.
  11. Once we have the indexes, we can multiply them by the tileSize to give us our px
  12. and py. Finally, return pkt, and we're good to go.