8 great A* pathfinding resources

If you’re writing a game that incorporates both a terrain of some sort and enemies that walk around, the chances are you’ll need some version of pathfinding to control the enemy movement patterns. The thing is, pathfinding is serious business–it’s not easy to grasp. To make things worse, there aren’t many good pathfinding resources–most of the articles that turn up on Google and such only serve to confuse you further. That’s why I’ve taken the time to compile this list of pathfinding resources that I’ve found to be actually helpful, as opposed to confusing or just plain wrong. Enjoy!
Continue reading 8 great A* pathfinding resources

A revolutionary and amazing new way to loop through arrays

…well, revolutionary and amazing to me, anyhow. Like most Javascript programmers, I usually use a for loop to iterate through my arrays, a method which typically works pretty nicely and doesn’t really have many drawbacks. However, I recently discovered another method which takes a tad less typing while maintaining the same functionality–the forEach loop. It’s quite handy and you don’t have to declare a bunch of variables to loop through an array.

The forEach loop behaves pretty much like your standard for loop, except that it’s a function that you call only on arrays. It looks like this:

myArray.forEach(function(element, index, array) {
console.log(element); // element is the current element in the array
console.log(index); // index is the position of that element in the array
console.log(array); // this is the array the forEach loop was called on
});

The above code passes an anonymous function into the forEach method, but you can use any function you want. Within that function, element refers to the current element in the array, index is the index of the current element of the array, and array is the array the forEach function was called on (note: you don’t have to pass in all the variables; just make sure you pass them in in the correct order). The forEach loop is a pretty handy alternative to the standard for loop and something I’ll definitely be turning to in the future.

The only drawback is that the forEach loop is relatively new, so you’ll have to provide a fallback for older browsers if you’re worried about compatability. Mozilla’s documentation provides an extensive article on the forEach loop and elaborates on a method to provide that fallback for older browsers I was talking about earlier.

Javascript’s little-known do-while loop

Many Javascript programmers are familiar with the while loop, which executes iterates until a given condition evaluates to false. It’s a pretty handy construct and can provide the fastest loops in the Javascript world (if used properly). However, there is a drawback. If the given condition evaluates to false immediately, the while loop will not run even once. This is because the while loop’s condition is evaluated before executing the code within the loop. “Oh well,” you say, “if that’s the case, then too bad, right?” Wrong.

Introducing: the do-while loop

The do-while loop is a lesser-known cousin of the while loop that few Javascript programmers know about. Unlike the while loop, it executes its statement exactly once before evaluating its condition. The do-while loop looks like this:

do {
statement;
} while (condition);

Because the statement of a do-while loop is executed once before the condition is evaluated, you can do interesting stuff like this:

do {
// statement
} while (false);

If we were using an ordinary while loop, the above statement would fail. However, because the statement of a do-while loop is executed before the condition is evaluated, the above code runs once and then fails.

Too long; didn’t read

Functionally, the do-while loop is the equivalent of this while loop:

while(true) {
// statement

if(condition == false) {
break;
}
}

Top 8 Javascript learning tools

Javascript is a complex and multi-faceted language, and can be hard to learn properly. I’ve found quite a few invaluable tools that have helped me along my Javascript-learning journey, but some of them have been a little hard to dig up and others aren’t very well-known. To save you the pain, I’ve scrounged around and pulled together a list of what I’ve found to be the most useful Javascript learning tools. Enjoy!
Continue reading Top 8 Javascript learning tools

9 useful techniques to optimize your Javascript

Javascript is an infamously easy language to program in. However, just because something works doesn’t mean you’re doing it right. Oftentimes, programmers will write a script to provide functionality they need and then leave it as soon as it’s working. Don’t be like those guys: read this tutorial on Javascript optimization (or at least skim it!) and apply the techniques listed here in your code. Your users will thank you.
Continue reading 9 useful techniques to optimize your Javascript