Game Development Reference
In-Depth Information
Coding in Construct 2
For all of our visual programming examples, we will be typing them in pseudo-code for
easier understanding. This code will not work, but it will give you an idea about the con-
cepts of programming. So, let's use an example of moving something to the right. The code
might look something like the following line of code:
GameObject.Move.Right;
This works, but we haven't set up a speed for the object. Right now, either the default speed
will be the speed of the object and the object will move too fast for the human eye to see,
or the compiler might get an error. If you misspell a word or make some kind of syntax er-
ror, the game might not run. So, we might have to update our code as follows:
GameObject.Speed = 10;
GameObject.Move.Right;
Notice how there is a semicolon at the end of each line. The semicolon tells the computer
to read the next line. However, if you look at the code, we haven't told the computer to
check for a button being pressed. If we add that code, it might be something similar to the
following code:
if (RightArrow.Pressed) {
GameObject.Speed = 10;
GameObject.Move.Right;
}
As per the preceding line of code, if the right arrow is pressed then the GameObject will
move to the right. This is called an if statement, and all it does is check for a condition to
be true. In this case, if the right arrow is pressed then the GameObject will move to the
right; however, if the right arrow is not pressed then nothing will happen. Now, let's add the
logic for the left arrow being pressed. The code is as follows:
if (RightArrow.Pressed) {
GameObject.Speed = 10;
GameObject.Move.Right;
}
if (LeftArrow.Pressed) {
GameObject.Speed = 10;
Search WWH ::




Custom Search