The lodash _.flatten method for flatting down a multi dimensional array

So some of the methods in lodash can come in handy, and really do help to save time with certain projects where I might make lodash part of the stack. Todays post on lodash is one of those lodash methods that I might actually use now and then which is the _.flatten method. However when it comes to just working with native javaScript these days there is also the flat array prototype in native javaScript.

The _.flatten, and also _.flattenDeep methods are one of many methods that help with the task of working with arrays of arrays, which are a kind of multidimensional array in javaScript. Flatten can be used to flatten down an array of arrays into a single array, thus making it a method that can be thought of as a reversal of _.chunk. With that said the lodash chunk method will do the inversion of flatten which is to break an array of elements down into an array of arrays of elements.

So then taking a moment to play around with flatten and chunk methods in lodash is one way to go about seeing why these two methods can often come in handy. So lets look at a few examples when it comes to working with arrays of arrays, and the lodash flatten method in javaScript.

Read More

The lodash _.join method, and Array.join

So with lodash as well as with plain old vanilla js there are the methods _.join in lodash, and Array.prototype.join when it comes to native javaScript. After taking a look at the source code for lodash 4.17.15 it would appear that the lodash _.join method is just one of several methods in lodash that is just a wrapper for a native javaScript method in this case the join method in the array prototype. This might seem pointless, but it does help to keep things consistent when it comes to just referencing native javaScript methods from within lodash, it also will come into play often when chaining lodash methods.

In any case this is join method is a method that comes up a lot when working out all kinds of solutions for problems when working in a javaScript programing environment, with, or without lodash as part of a collection of libraries to work with. The join method can be used to join all the elements of an array together with a given separator between each element, furnishing a string from those array elements. It can be thought of as the opposite of _.split, or the split string prototype method that can be used to split a string down into an array of elements with a given separator.

In any case this post will outline some examples of joining the elements of a javaScript array together into a string using the join method inside and outside of lodash. In the process of doing so I will also end up touching base on a lot or related topics as well surrounding the use of arrays and strings in general.

Read More

Animation keyframes with phaser

When making animations for a phaser 2 game, or with animation in general actually the concept of a key frame is important. A key frame can be described as a frame when an animation begins, or ends, and as such it is a beginning state from which an animation will progress from, and then back to in the case of a looping animation. It can also be the two frames at which an animation begins at a starting state, and ends at an ending state when dealing with some kind of non-looping animation sequence. In this post I will be writing about key frames when using the phaser 2 game framework, giving some examples of this important animation concept when making sprite sheets for a phaser game.

Read More

Removing false values from an array with lodash _.compact and native javaScript.

For today I will be writing another one of my quick little posts on lodash, when I do so it is often on the topic of a specific method in lodash, and for today that method is the lodash compact method. My approach with writing content on lodash is that when I write a post on a certain lodash method, I am not just writing about lodash, but a certain way to preform a certain task often involving arrays, array like objects, or collections in terms of objects in general. So under that light I think it is a good idea to write some content on the topic of the compact method and any additional lodash methods and native javaScript features that will come up while in the process of doing so.

The main idea about the _.compact method is that it can be used to quickly remove false values away from an array. With that said the compact method is one of the array rather than collection methods in lodash, so then the compact method is not a replacement for other lodash methods like that of the lodash filter method. Also there are other array method options in lodash such as the lodash remove method that allows for a little more flexibility when it comes to defining what should not be in the new array that is to be cerated. Covering the method by itself is not that involved, but it can branch off into some additional topics when it comes to doing the same with just plain old vanilla js.

Read More

Making animations in phaser with the animation manager

When making a animation from the ground up with javaScript by itself the process often might involve one or more sprite sheets that is brought into the project by way of an external image file, or generated from code. Once I have my sheets I then devise some kind of system to get the proper frames, from the proper animations, from the proper sheets. This process can be time consuming, and is one of the many reasons why it is a good idea to just work in a framework such as phaser 2 to one extent or another. In phaser sprite sheets can be added into a project from an external file via the asset loader, or generated with javaScript and added into the cache. Once I have a sprite sheet animations can be made by way of the animation manager of a sprite. In this post I will be writing about using the animation manager with asset less sprite sheet solution.

Read More