The main way to change our game’s variables using Java is using a concept called operators.Operators in Java are just like mathematical operators and many of them are the same symbol. So if you coped with junior school math you will have no problem with this tutorial. We will start by looking at a list of some of the most commonly used Java operators.
[widgets_on_pages id=”udemy_advert_java_1″][widgets_on_pages id=”udemy_code_details”]
Java operators
Here is a list of the most useful Java operators that allow us to do things with our game’s variables. One of which we already know about.
The assignment operator which we have already seen (
=): This makes the variable to the left of the operator the same as the value to the right. For example:
hiScore = score;
or
score = 100;
The addition operator +: This adds the values on either side of the operator. It is usually used in conjunction with the assignment operator, such as:
score = aliensShot + wavesCleared;
or
score = score + 100;
Notice that it is perfectly acceptable to use the same variable on both sides of an operator.
The subtraction operator
-: This subtracts the value on the right side of the operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps:
lives = lives - 1;
or
aliensRemaining = aliensRemaining - numberAliensDestroyed;
The division operator /: This divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this:
hitPoints = hitPoints / swordLevel;
or
recycledValueOfBlock = originalValue / .9;
Obviously, in the previous example, the variable
recycledValueOfBlock will need to be of the type
float to be multiplied by the value of .9.
The multiplication operator
*: Perhaps unsurprisingly, multiplies variables and numbers, such as:
answer = 10 * 10;
or
biggerAnswer = 10 * 10 * 10;
The increment operator ++: This is a really neat way to add 1 to the value of one of our game’s variables.
This:
myVariable = myVariable + 1;
is the same as this:
myVariable ++;
The decrement operator --: You guessed it, a really neat way to subtract 1 from something.
playerHealth = playerHealth -1;
is the same as:
playerHealth --;In the next tutorial, If you would like to practice what you have learnt in this and the previous tutorial about game variables try the mini-project Checking for conditions in our game we will try using some declarations, assignments and operators. When we bundle these elements together into some meaningful syntax, we call it an expression.
[widgets_on_pages id=”udemy_advert_java_3″][widgets_on_pages id=”udemy_code_details”]
Umm, there’s a typo when demonstrating “the subtraction operator.” Below it you put “Alies” instead of “Aliens.”
Thanks, Thomas!. Fixed that.
Hi John, I recently started learning game programming(20 minutes ago to be precise). The content here is really easy to understand for a beginner like me with no exposure to Java or game programming. Thank you for this.
I was reading through this section and found this:
“Obviously, in the previous example, the variable recycledValueOfBlock will need to be of the type float to be multiplied by the value .9.”
I think it should be “type float to be divided by the value of .9.”.
Why don’t you use
score += 100;
which means “score = score + 100″?
Hi Lazer thanks for the useful comment.