mhomami / Tank

0 stars 0 forks source link

Convert loop to `map()` call #1

Closed andyg0808 closed 2 years ago

andyg0808 commented 2 years ago

https://github.com/mhomami/Tank/blob/74e115e25ae04a530c524b3d3756b83eb56cb745/js/script.js#L32-L37

Instead of looping over an array and writing to it, I'd suggest using the map() function:

 const textureArray = bitsImages.map(image => PIXI.Texture.from(image));

map() runs a function on each element of an array. Here, we're passing a short arrow function which just takes the element and returns the result of passing it to PIXI.Texture.from.

By using a map(), we can express that we want "every entry in bitsImages to be converted into an entry in textureArray." It also eliminates a bunch of the extra bookkeeping work (like keeping track of the index) that we'd have to do if we wrote the loop by hand.

mhomami commented 2 years ago

Magic, thank you! I'm committing the changes you suggested. I intend to use a spritesheet to animate this as it will be cleaner than declaring (and storing) 11 different .png files but I'm leaving that for another time. Keen to get some other functionality in first.