- Haxe Game Development Essentials
- Jeremy McCurdy
- 1315字
- 2021-07-30 10:29:52
The Haxe syntax
Haxe is a very easy language to learn, so we'll be go over the basic syntax now and build on what we know as the book progresses.
At its core, Haxe contains all of the same things you find in other languages: variables, data types, data structures, access modifiers, functions (also known as methods), and expressions.
Haxe is also an object-oriented language, so you will see classes, inheritance, and polymorphism.
Data types and structures
The basic data types in Haxe are Bool
, Float
, Int
, and so on.
Those data types are considered basic because they are simple pieces of data that aren't represented by a class. There are many data types and structures that are important and are represented by a class. They are explained here.
Operators
Haxe has a number of operators that are used to perform math equations, concatenation, and comparison. Let's go over the most common ones.
Access modifiers
Variables, classes, and functions can all be given an access modifier to determine how they can be accessed in your codebase. These are the access modifiers you will use most often.
Variables
Here's an example of a variable:
var score:Int = 0;
Variables are defined using the var
keyword, followed by the names you choose to give them. The colon allows you to specify a data type (in this case, it's an integer). Using the equals symbol allows you to assign a value, and the semicolon ends the line of code.
Semicolons are required to end every line of code. You will get errors if you forget to add them.
Tip
It is best practice to provide a data type; it increases the readability of your code and makes debugging much easier because you don't have to guess what data type you're using.
It's also worth stating that you aren't required to provide a data type to variables in most cases. Haxe supports type inference at compile time, which means that the compiler will do its best to determine what data type you intend to use based on the value assigned to it. For example, if a variable was set to 5
without a data type provided, the inferred type given to the variable would be Int
.
Here's a list of data types with their descriptions:

Here's a list of operators with their descriptions:

Here's a list of access modifiers with their descriptions:

Functions
Functions in Haxe work in pretty much the same way that functions and methods work in other languages. A function definition looks like this:
private function processData(inputData:String, optionalData:Int = -1):String{ return inputData; }
This function will simply take in a piece of string data and return it immediately. We start by using an access modifier to indicate that the function is private. We then use the keyword function
to indicate that the block of code contains a function (similar to the way we use var
to declare variables). After this, we provide a name for the function.
Inside the parentheses, we add arguments (also referred to as parameters). These are any pieces of data you wish to pass in when calling the function. You provide a name for the property, followed by the data type. Arguments passed into functions don't have to be typed, but for the sake of clarity and debugging, they should be typed.
You'll also note that the second argument in the function has = -1
next to it. This indicates that the second value will default to a value of -1
if no value is passed in for the second parameter. This lets you create robust functions that can have optional arguments.
After the arguments section, you will see that we specified a String
data type, and this means that the function will return a string value. If your functions don't need to return a value, set the return type to Void
.
The function's logic is wrapped in curly brackets (also named curly braces); all of the function's logic must be contained between the opening and closing brackets.
Finally, we return a value using the return
keyword followed by the returned value. Functions that have a specified return type must return data of that type. If you set the return type to Void
, then you do not have to use the return
keyword.
One last thing to note about functions is that you cannot overload functions in Haxe. You must either use optional arguments or create multiple functions instead.
The for loops
The for
loops in Haxe behave a little differently from the way they behave in most other languages. Every for
loop uses iterators—objects designed to be iterated through in sequence. This basically means that Haxe for
loops work much like for-each loops do in other languages.
A for
loop looks like this:
for(i in 0...10) { //Loop functionality goes here. }
The loop starts with the for
keyword, followed by opening and closing parenthesis that contain the iteration parameters. The i
parameter is a variable that will store the current iteration value, in this case, an integer from 0 to 9. After this, there is the in
keyword, which acts to separate the iteration variable from the iterator. Finally, we have 0...10
, which uses a special range operator that Haxe uses to iterate from one value to a higher value.
Note
You use ellipsis (...) to separate the low and high values. It's worth noting that the maximum value that i
can be is the high value minus 1, because the maximum value is converted to a zero-based numbering system.
One other thing to note is that for
loops in Haxe can't loop in reverse, but rather they must always increment.
Classes
Classes in Haxe work in the same way that they work in most object-oriented languages. Let's have a look at an example and walk through it:
package examplePackage; import packageName.ClassName; class ExampleClass extends ParentClass { public static inline var CONSTANT_VALUE:Int = 1; public var publicVariable:String; private var privateVariable:Int; public function new() { super(); } override private function ParentClassFunction( { super.ParentClassFunction(data); } }
To start, we define the package that our class resides in. This typically relates to the folder path to the class file from the root of the source directory. Using packages lets you have classes with the same name in a project, which is useful for things like code libraries. The package
line is ended with a semicolon.
After the import
statements, you define the classes you intend to use in variables and functions. If a class you want to use is in the same package as your class, you don't need to import it.
Next, there's the class definition. You use the keyword class
followed by the name of your class. If you have a parent class that you're extending, use the keyword extends
followed by the name of the class you're extending. Make sure to import the parent class if it isn't in the same package as the current class.
After defining the class, you wrap all variables and functions in a set of curly brackets.
Inside the class, you'll see that we have a couple of functions. The first is the new
function. This is the constructor function for the class and is a special function that will get called when you make a new instance of your class. If you're extending a class and overriding its functionality, you must also call the super
function; this will call the constructor of the parent class.
The second function uses the override
keyword. This indicates that you intend to add or change functionality in a function that's defined in the parent class. Since it's a function that belongs to the parent, you need to call the function using the super
object. This fires the parent's function and then anything done afterwards will be exclusive to the child class and any of its children.
- 精通Nginx(第2版)
- C程序設(shè)計(jì)簡明教程(第二版)
- 計(jì)算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- Manga Studio Ex 5 Cookbook
- TestNG Beginner's Guide
- Mastering Scientific Computing with R
- 深度強(qiáng)化學(xué)習(xí)算法與實(shí)踐:基于PyTorch的實(shí)現(xiàn)
- PostgreSQL Replication(Second Edition)
- 程序員修煉之道:通向務(wù)實(shí)的最高境界(第2版)
- Mastering JavaScript Design Patterns(Second Edition)
- Android傳感器開發(fā)與智能設(shè)備案例實(shí)戰(zhàn)
- Web性能實(shí)戰(zhàn)
- Unity 2017 Game AI Programming(Third Edition)
- Building Business Websites with Squarespace 7(Second Edition)
- H5頁面設(shè)計(jì)與制作(全彩慕課版·第2版)