5 min read
var playerName = "Jesse Freeman";
There are three basic types of "primitive" types:
- Strings – this is text and is always put into quotes
- Numbers – these are numbers and can be positive, negative, whole number, or fractions
- Booleans – this is a true or false statement. Also, you can use the value 0 for false or 1 for true as well.
The most common thing you would probably want to store in a game would be things like the player's name, their score, and whether the player is alive or not. Here are some examples:
//String var playerName = "Jesse Freeman"; //Number var score = 0; //Boolean var isAlive = true;
Arrays are similar to variables, but they can contain more than one value. Think of arrays as lists. In games, we could use arrays to keep track of items in the player's inventory. Here is how you set up an empty array:
var inventory = ;
Likewise, you can also create an array with items inside of it like so:
var inventory = ["item0", "item1", "item2"];
0. Here is an example of how we would get the value of the first item:
As you can see, we reference the array itself and use brackets to supply the index we want to access. Arrays also have specific actions called functions, which we'll learn about later. These functions allow you to add and remove items from the array. For example, we can use
push() to add a new item to the end of an array:
You can also get the total number of items in an array like this:
Notice that length doesn't have parentheses at the end. That's because it's a property of an array. The array's length value is part of the array itself and will always return how many items are in it. If we looked at the length, it would return
4 since we added "item4" above. However, remember that if you wanted to use this to find out the last value of the Array, you would need to subtract
1 from it since the starting index is
0. Here is how you get the last item in the array:
Arrays are incredibly versatile and are used to hold lots of different kinds of data structures. One of the most common is a table-like structure with rows and columns. This is the basic structure we would use for tilemaps. When we have an array with nested arrays for values, we call it a 2D Array. Here is an example:
var map = [ [1,1,1,1,1], [1,0,0,0,1], [1,0,1,0,1], [1,0,0,0,1], [1,1,1,1,1] ];
This simple map example uses two tile values:
0 for empty and `1' for solid. If we were to visualize this map, it would be a square room with a solid block of right in the middle. Accessing the value of these tiles is similar to how we did with our inventory array. Let's say that we want to get the center wall in our room. Here is how we would access it:
One way to understand how to access data in a 2D Array is to remember that the first index is the row and the second index is the column like this:
Remember that arrays can contain any primitive type. So a nested array might not necessarily store numbers. They could also have strings or even booleans.
Variables and arrays are the cornerstones of programming, and once you get your head around how they work, you can begin building event more complex data structures.
In the next post, we'll cover how to create objects and functions, so you can build more complex logic.
If you found this helpful, please give it a 👍 and leave a comment. You can also follow me on Twitter (@JesseFreeman).