Variables in JavaScript

Construct 2 exports to HTML5, and this means that a lot of the functionality in the game is written in JavaScript. In JavaScript, the type of variable is not defined until you initialize it. While this makes programming in JavaScript much easier, it has its pros and cons. The biggest con is that sometimes you forget which variables are of what type, which leads to unexpected results when you run the app.

Examining JavaScript code

JavaScript is a very popular web language. It is much easier to code in than most other programming languages.

Let's take a look at some examples of JavaScript code.

var score;
var timescale = 1.2;
var name = "John Bura";
var playerName;

You will notice that all of the variables are simply called var instead of Int, Bool, String, or Real. This means that, until you initialize the variable, var can be whatever you want it to be.

So in this case, score is not a number—it is simply a variable. However, timescale is a number variable because we have initialized it as such with the = sign. Similarly, var name is a string because we initialized it, while var playerName is simply a variable. Their nature doesn't become set until they are initialized. JavaScript has become popular because you can have less precise code and still make it work.

Now, in JavaScript, there is something that doesn't happen in most environments—you can mix and match variables. I strongly advise not doing this. As a programmer who started with integer and string values, I highly suggest you keep your variables organized. If you mix and match variables and simply don't care, you will have a hard time releasing your game—and releasing is important.

Since Construct 2 is exported in JavaScript, it is important to have a basic understanding of how it works. It is also important to know what kinds of variables are present because, even in JavaScript, you have to use different kinds of variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.141.27