Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Writing a Hello World


Let's create an application that will simply display the "Hello World" message. We will use only cross-platform code and will compile it to neko to begin.

  1. Copy the following code in a file named Main.hx

    class Main
    {
       public static function main()
       {
          trace("Hello World");
       }
    }

    Tip

    Downloading the example code for this book

    You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

  2. Open a terminal; go to the directory where you saved Main.hx and type the haxe –main Main –neko helloworld.n command.

  3. Type this command: neko helloworld.n.

  4. You will see the following output:

    Main.hx:5:Hello World

What just happened?

As you've guessed, the program just wrote "Main.hx:5:Hello World" in our terminal and ended.

  • The code: In our code we declare a class named Main, which has a public and static function main. The method trace is a method that allows one to debug an application by printing a message along with the file's name and line number of the call. If you don't really understand what classes are, don't worry, we are going to explain it to you soon.

  • The compilation: The command in the second step compiles the haXe code to neko. Notice that the –main switch that's followed by the name of the class containing the function has to be executed when launching the program. This function has to be a static function named main that takes no parameters.

  • Running the neko code: In the third step, we invoke the neko VM and tell it to execute the helloworld.n file.