Making a sum with lodash _.sum, _.reduce, and vanilla javaScript alternatives

Creating a sum from an array, more often then not, is a fairly trivial matter with javaScript as it can quickly be done with a native array method like reduce. However in some cases it might be nice to have methods that make quick work of trivial tasks such as this by allowing me to just call a single method for this and move forward with a project that much faster.

Making a native sum method might not be so hard, however if lodash is there to work with in a project then that can be called to quickly create a sum for an array of numbers. In this post I will be writing about _.sum, _.sumBy, _.reduce when it comes to working in a project in which lodash is there to work with. However because lodash seems to be on its way out there is also looking into vanilla js alternatives when creating a sum from an array of numbers.

Read More

A waves example using javaScript and threejs

So I wanted to start making some posts on threejs examples, rather that the usual posts on certain basic things here and there with just the core of what threejs alone is. One of the first ideas that came to mind was to make a waves example where I create an update a buffer geometry based on something like Math.cos.

In this post I will be writing about a module that makes use of a helper method that I made that can be used to create, or update an instance of buffered geometry that is a set of points that move in a wave like pattern. This buffer geometry instance can then be used with an instance of the THREE.Points constructor rather than the usual THREE.Mesh constructor, and when doing so it is just the position attribute of the buffer geometry instance that I need to worry about. At least that is what the plan was for the first version of this example, as I now have plans to create a revised revision of this module that will also work with mesh objects.

So this threejs example might be a good starting point when it comes to figuring out how to go about creating a custom geometry with a little javaScript code, and also how to work with the Buffer Geometry constructor. There is a whole lot to cover when it comes to this sort of thing, but I would say that the first step is to know how to create and update the position attribute of a buffer geometry and this will be the main focus of this example here.

Read More

Game visibility change, and having code run when tabs change in phaser ce

When making a phaser ce project by default the game will pause when it is no loger in focus by the player. In most cases this is fine, but depending on the nature of the game that is being made some times this might present a problem. In this post I will be writing about a boolean property in the state object call game.state.disableVisibilityChange that can be used to change this default behavior so the game continues to run even when not in focus.

In addition I will also be writing about the nature of setTimeout vs requestAnimationFrame. In some cases you might want to decouple some of your code away from phasers state loop if you want code to continue to run in the background.

Read More

Defense game phaser plug-in examples

So today I got around to making another example that involves phaser ce plugins. This time around the aim was to make a simple defense style game plugin. The process of even making a simple defense game can some times be a compacted one, a greate deal of logic needs to be in effect to govern things like when an enemy is to spawn, and what happens when it reaches a certain point, such as the side of the screen which is typical for most of these kinds of games. In this post I will be writing about a plugin that I made that contains much of the basic component of a simple defense style game.

Read More

Chaining functions in javaScript with lodash or not

So when working out a javaScript project it may often be a good idea to chain functions together so that what is returned by one method becomes an argument value for another, and so on until some kind of desired end result is obtained.

In native javaScript this is not so hard, when calling one prototype method of one instance of something, what is returned can also have any of its prototype methods called and so forth.

In lodash there is the _.chain method that can be used to create what are called explicit method chain sequences. In addition there is also the main function of lodash, that when called can be used to create implicit method chain sequences that work in a similar fashion to what you may all ready be familiar with when it comes to chaining vanilla js native methods. There is yet even another option that can be deployed as a way to create a chain of sorts that is the lodash flow method that works by passing an array of functions to use. It may be best actually to use a function such as flow over that of the lodash chain method for various reasons that one will get into when making custom lodash builds.

Although this is a lodash post I will be covering chaining with, and without lodash in this post in an effort to cover chaining in general.

Read More