Vous êtes sur la page 1sur 4

Basic Variables

That's right! Not too hard. There are several parts to a variable, so let's go through them one by one. As before, here is how we declare a variable. 1 private var userLives = 3; * 'userLives' is the unique name for our variable container and is what we will use to access the variable in the future. * var is short for variable. *Don't worry too much about the word 'private' for now. It simply means that this variable cannot be used by other classes (files) of code. I'll explain more about that later. Always include the word private for now. * Notice that the line ends in a semicolon! This is very important. The semicolon is like the period of a sentence. It tells the compiler, 'this line of code is done, move on to the next line.' Good. Did you notice the funny capitalization? userLives

It's called "camel case." When we name a variable, it is a standard naming convention to make the first word lowercase, but capitalize every important word after that, with no spaces of course. If the variable name is only one word, just make that whole word lowercase. Camel case is optional, but since it is standard practice, I make it required in this game. Here are some examples from this very game that I've used!

lessonHintArray levelProgressDisplay currentLevelExp userHealth universe yellowArrow

Excellent. Do you remember what I said about semicolons earlier? Missing semicolons can be nasty because you spend time trying to fix correct code without noticing the missing semicolon, don't forget them! It's time to declare your first variable! Declare a variable named myFirstVariable and set it equal to 1. Here is the syntax to help you out: private var variableName = value;

The power of variables is the ability to change their values. You should remember from Algebra that a variable can represent any number. The same is true for programming.

On a side note, Oh My Gosh, we are using algebra in real life!

Anyway, take a look at the code below. This is how we change the value of a variable.

Anyway, take a look at the code below. This is how we change the value of a variable.

1 2 3 4 5 6 7 8 9 10

private var myNumber = 50;

//right now, myNumber equals 50

myNumber = 300;

//Now, myNumber equals 300

myNumber = 0;

11 //now, myNumber equals 0 and will stay 0 until we change it again!

To pass this section, using the above as a guide: change the value of myNumber to 123. Do not worry about writing the code line number at the beginning.

It is important to notice that we only had to use the words 'private var' once, when we declared the new variable at the start. After that, we just had to type:

variableName = newValue;

// (don't forget the semicolon)

Here is another example:

1 private var userHealth = 100; 2 3 userHealth = 10;

Just like that, userHealth is down to 10. Good job. If you use the words 'private var' on the same variable twice, it will cause an error. You now know how to reassign a new value to a variable. Now for some flexibility.

What if we want to take the variable's current value, which could be anything, and add a number like 5 to it? Check out this code:

1 private var myNumber = 20; 2 3 myNumber = myNumber + 5; 4

5 //myNumber is now equal to 25! 6 7 myNumber = myNumber + 5000; 8 9 //myNumber is now equal to 5025!

You are doing fine rookie. This section is getting a little long, so let's stop here. In this section you learned some important material. You learned:

1. What a variable is, and how to declare a new private variable and give it an initial value.

2. How to change the value of a variable and give it a different value.

3. How to add a number to the value of a variable.

4. How to capitalize the name of a variable.

5. That the semicolon is the period of a coding sentence, and that it is found at the end of a lot of lines of flash! DO NOT FORGET IT!

Vous aimerez peut-être aussi