Keine Ergebnisse gefunden
Welcome to part three of our five part series on learning JavaScript for game developers. In part one we talked about Variables and in part two we covered Objects and Functions. In this part we’ll walk through how conditionals work in JavaScript to add more complex logic into your code.
ames are made up of states and we are constantly trying to determine what happens based on that state. Is the game running? Is the player alive? Does the player have a weapon? If the player has a weapon can they can shoot? In order to test these states we will need to use conditionals. There are three main types of conditionals I’ll go over here. The most basic is an if/else statement:
if(player.isAlive == true){
// do something
}
Here we are testing to see if the player is alive. By using the double equal sign with are making sure the player’s isAlive property value is equal to true. If the player is alive we can run some code. But what happens if the player is not alive. For this we can make use of an else statement.
if(player.isAlive == true){
// do something
}else{
// do something else
}
Here you can see we are still testing if the player is alive. Now we can execute different code such as end the game if the player is not alive. Likewise you can add as many if statements as you like this:
f(player.isAlive == true){
// do something
}else if(player.isAlive == false{
// do something else
}else{
// do nothing
}
Another interesting way to take advantage of conditionals is by using something called a switch statement. It’s similar to an if/then statement but allows you to test a variety of values without the need for adding operators such as equals, less than, etc. Here is how you create a simple switch statement:
switch(letter){
case "g":
//do something
Break:
default:
// do something
Break;
}
As you can see the switch statement is designed to test a single value against a case. If we were to pass in a string "g" it would pass the first case. We always add a break at the end of a case to keep other cases from executing. Finally we can use a default case to run something if no value is matched. Switches are very common in games and a great example would be getting the value of a letter in a game of scrabble:
getLetterValue(letter) {
switch (letter) {
case "g": case "d":
return 2;
break;
case "m": case "b": case "c": case "p":
return 3;
break;
case "y": case "f": case "v": case "w": case "h":
return 4;
break;
case "k":
return 5;
break;
case "j": case "x":
return 8;
break;
case "q": case "z":
return 10;
break;
default:
return 1;
break;
}
}
Here you can see that I am assigning values to different letters. The most common value is always going to be 1 so that becomes my default return value. From there I set up cases for each letter going in order of value. You can add additional cases to a single test as you can see from each one of these score tests I wrote.
There is one additional conditional I want to go over quickly called a ternary operator. It’s similar to an if/then statement but all on one line. Here is an example:
var playerState = (player.isAlive == true) ? "Alive" : "Dead";
In this example we simply test to see if the player’s isAlive property is set to true. We do this test on the left hand side of the question mark. If the statement is true than playerState’s value will be set to "Alive" which is on the left hand side of the colon. If the value was false, the statement would return "Dead" which is on the right hand side of the colon. Here is a quick cheat sheet for how to write ternary operators:
condition ? value if true : value if false;
These are very useful when you don’t need to write out full if statements or are trying to clean up values into strings and need to do it inline of a variable.
- Jesse Freeman (@jessefreeman)