Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
haXe 2 Beginner's Guide
haXe 2 Beginner's Guide

haXe 2 Beginner's Guide: Develop exciting applications with this multi-platform programming language

eBook
$25.99 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

haXe 2 Beginner's Guide

Chapter 1. Getting to know haXe

Entering the haXe World.

In this chapter you will discover haXe's world and enter it. You'll install haXe and write your first program.

This is our first chapter and you will soon be taking your first steps with haXe. However, before you get into action, let's introduce you to haXe and set up your working environment.

In this chapter, you will:

  • Install haXe

  • Choose a code editor based on our requirements

  • Write and run your first program

  • Write a program interacting with the user

Seems like a long list? Don't be afraid, you will make it!

Installing haXe


Enough talking, let's install haXe! You will see how easy it is.

Two ways to install: The installer and sources compilation

You should know that there are two ways to install haXe, which are as follows:

  1. Using the installer: An executable that will automatically install haXe for you.

  2. Compiling from sources using the code repository located at the following URL:

    http://code.google.com/p/haxe

One important thing to note is that haXe can be installed on on Windows, Linux, and MacOSX. This way, you don't have to worry about your operating system and can continue working in your environment.

Installing on Windows

Installing haXe on Windows is pretty easy, but there are some caveats that we should avoid.

So, carry out the following steps:

  1. Go to http://www.haxe.org.

  2. Click on the Download link.

  3. There, you will find a link to download the Windows version of the installer. It comes as an executable file.

  4. Run the executable.

Note that if you're working on Windows Vista or Windows 7, you will need to run the executable as Administrator. You can do so by right-clicking on the installer while holding the Shift key down and then clicking on Run as Administrator.

Installing on MacOSX

Installing haXe on MacOSX is very straightforward. Carry out the following steps:

  1. Go to http://www.haxe.org.

  2. Click on the Download link.

  3. There, locate and click on the link to download the OSX Universal Installer.

  4. The installer comes as a DMG image, if it's not automatically mounted; mount it by double-clicking on it.

  5. Go to where the DMG image is mounted and run the installer.

That's it! You have haXe installed on MacOSX!

Installing on Linux

Some distributions have haXe in their repositories, so you may install it from your distribution's repository if it's available.

Alternatively, you can use the Linux Installer to install haXe; it does work on many distributions. Carry out the following steps to install haXe on Linux:

  1. Go to http://www.haxe.org.

  2. Click on the Downloads link.

  3. Find the link to download the Linux version of the installer.

  4. Uncompress the file by using the tar –zxf hxinst-linux.tgz command.

  5. Add execution rights on the installer by using the chmod +x hxinst-linux command.

  6. Run the installer with administration rights either by using the sudo –s command or the su command, and then running ./hxinst-linux.

Now, you should have haXe installed on your Linux machine.

Installing nightly builds

If you need features that are not in the latest release, you can use the nightly builds to easily have access to binaries of what is in the repository without the need of compiling it on your own.

You will be able to find them on the haXe website's Download page for your operating system.

Note that the nightly builds do not come with an installer and therefore, you are advised to first install using the installer, so that it sets up all environment variables and then replaces installed files by the ones from the nightly builds archive.

On MacOSX and Linux machines, you should find them in /usr/lib/haxe, while on Windows, they should be in c:\haxe.

Verifying your installation

You can verify that haXe is correctly installed by opening a terminal and running the haxe command; if haXe is correctly installed, then you should get the help message.

You can also test that Neko is correctly installed by running the neko command. You should again get the help message.

Choosing an editor


There are not many editors for haXe, but still there are some that you should know of. Although I do suggest that you look at these because they can help you increase productivity. I do think that it's also important in the beginning to use the haXe compiler (that is, the haxe command) on your own to understand how it works and how it is used. You'll get more and more comfortable as you go through the examples.

FlashDevelop 3

The FlashDevelop IDE supports haXe projects. This is certainly the most advanced haXe code editor on Windows at the time of writing and also, it is an open source software.

FlashDevelop supports auto-completion, project management, syntax highlighting, and compilation rights from the IDE.

You can download FlashDevelop for free from http://www.flashdevelop.org. If you want to have a quick look at it before trying it, the following screenshot shows how it looks:

The TextMate bundle

TextMate is an easy-to-extend text editor for MacOSX. It can be extended by installing "bundles". There's a bundle providing haXe auto-completion, syntax highlighting and compilation from TextMate at http://github.com/freewizard/haxe2.tmbundle. You simply have to download the bundle as a ZIP file and rename it with the .tmbundle extension. Once this is done, double-click it. If you already have TextMate installed, the plugin should get installed.

The following screenshot shows what it looks like with the haXe bundle:

VIM

There are several scripts that you can install to add support for haXe in VIM. Many of these are collected at http://github.com/MarcWeber/vim-haxe. With those scripts, you get syntax highlighting and auto-completion. Although VIM is generally used by people using Linux, it can be used on Windows and MacOSX. This way, one can have the same tools across multiple platforms.

Writing your first program


So, you've installed haXe and got some advice in case you need help. Now is the time to get into action by writing your first program: and as usual, it will be a Hello World!

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.

A program with some interaction


Now that we've gone through the classic "Hello World", let's do something a bit more interesting; how about taking some parameters on the command line? If you're not familiar with commands, this is the usual way to take some information from the user without having to write an interactive program.

Time for action – Interacting with the user


Now, we're going to create an application that takes your name as a parameter, greets you, and displays the number of characters in your name.

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

    class Main
    {
      public function new()
      {
      
      }
    
      public static function main()
      {
       var name = neko.Sys.args()[0];
       trace("Hello " + name);
       trace("Your name is " + Std.string(name.length) + " characters long.");
      }
    }
  2. Open a terminal; go to the directory where you saved Main.hx and type the haxe –main Main -neko characterCounter.n command.

  3. Type the command (this is an example with my first name):

    neko characterCounter.n Benjamin

  4. You will see the following output:

    Main.hx:12: Hello Benjamin

    Main.hx:13: Your name is 8 characters long.

What just happened?

The program read your name from the command line, greeted you by appending your name to "Hello", and displayed the number of characters that make up your name.

  • The code: You already know the basics about the class and the main function. What is interesting to see here is how we get the arguments. When targeting neko, we can use the args function of neko.Sys. This function takes no parameters and returns an Array of String.

    As you can see, we can access an array's item by its index using the square-brackets notation. Here we are accessing the item at index 0 (arrays are 0-based, that means that the first item is always at index 0).

    Then, we use the trace function that you've already seen to display "Hello" followed by the name of the user. As you can see here, string concatenation is done by using the + operator. It is still quite important to note that there are classes such as StringBuf that can be used to achieve the same behavior, if you are focused on performances.

    You'll also notice that String has a variable named length that allows you to retrieve the number of characters in it.

    By the way, haXe is typed, and here we are using Std.string to convert the length of the string from Int to String. Std.string can be used to convert any value to a String. This is not mandatory here, as when using the + operator, if one of the two values is not an Int nor a Float, the operator will return a String. In this case, all operands will be considered as String.

  • The compilation: Well, as you can see, there's absolutely nothing new in the compilation process. The only thing we've changed is the output file's name.

  • Running the neko code: In the third step, we invoke the neko VM and tell it to execute the characterCounter.n file by passing it an argument: Benjamin.

  • Possible improvements: Our program is quite simple, but if you've some experience with programming, you may have noticed one point where our program may encounter an exception: we are relying on the fact that the user will give us at least one argument, and we access it directly without verifying that it's really there. So, if the user gives us no argument, we will encounter an exception. There are two ways to handle that: either we verify how many arguments the user has given, or we can also try to catch exceptions. This is something that we will explain later.

Pop quiz – basic knowledge

  1. It is possible to install haXe on:

    1. Windows

    2. MacOSX

    3. Linux

    4. Android

    5. iOS

  2. What major version do we use nowadays?

    1. haXe 1

    2. haXe 2

    3. haXe 3

    4. haXe 4

  3. The main function of a program has to be:

    1. static and named main

    2. public and named first

    3. public and static, and named first

  4. The debugging function that prints some text is named:

    1. println

    2. print

    3. trace

    4. debug

Summary


In this chapter, we have seen how to carry out your first steps with haXe. In particular, we've seen how to install haXe and how to write haXe programs. We've also seen how you can interact with the user in the console.

Now, let's go to the second chapter in which we will learn about the basic syntax and how to do branching.

Left arrow icon Right arrow icon

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 26, 2011
Length: 288 pages
Edition :
Language : English
ISBN-13 : 9781849512565
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 26, 2011
Length: 288 pages
Edition :
Language : English
ISBN-13 : 9781849512565
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 130.97
Haxe Game Development Essentials
$26.99
haXe 2 Beginner's Guide
$48.99
OpenGL 4.0 Shading Language Cookbook
$54.99
Total $ 130.97 Stars icon

Table of Contents

12 Chapters
Getting to know haXe Chevron down icon Chevron up icon
Basic Syntax and Branching Chevron down icon Chevron up icon
Being Cross-platform with haXe Chevron down icon Chevron up icon
Understanding Types Chevron down icon Chevron up icon
The Dynamic Type and Properties Chevron down icon Chevron up icon
Using and Writing Interfaces, Typedefs, and Enums Chevron down icon Chevron up icon
Communication Between haXe Programs Chevron down icon Chevron up icon
Accessing Databases Chevron down icon Chevron up icon
Templating Chevron down icon Chevron up icon
Interfacing with the Target Platform Chevron down icon Chevron up icon
A Dynamic Website Using JavaScript Chevron down icon Chevron up icon
Creating a Game with haXe and Flash Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(3 Ratings)
5 star 33.3%
4 star 33.3%
3 star 33.3%
2 star 0%
1 star 0%
Lars Mathiasen Feb 06, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Feefo Verified review Feefo
razaina Sep 11, 2011
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
It's about 2 years I'm using haXe. I'm not a beginner but I still have constantly a lot of stuff to learn.haXe is a powerful language which provides many features, and gives to developers a tool to create websites, and applications. Thanks to its cross-platform features, it brings ease to target different platforms using a single unified programming language.In this book, Benjamin Dasnois made a great job of introducing the specifics of the language.The book is really easy to read, and at first sight, it is aimed for beginners, but not only because I was surprised to learn stuff I never tried to do before, and to reinforce my knowledges in some area like how different applications can communicate between each other (Chapter 7 : Communication between haXe programs).The chapters of the book are cleverly well structured.The "Time for action" heading followed by the "What just happened ?" make this book so enjoyable to read.Like a recipe, you just have to follow instructions and then you can ta(e)ste it. The second heading gives clear explanations about how tasks or instructions work.The only negative for this book to me is that C++ which is a haXe target that I didn't experimented yet, isn't covered, so I expected to read some stuff about it.If you're looking for a comprehensive introduction to what haXe is all about, or if you're a beginner or an intermediate haXe developer who want to fill gaps in your knowledge, I really would recommend buying this book.Anyway, subscribe to the haXe mailing list and stay tuned for all the new features the language is offering.
Amazon Verified review Amazon
Amazon Customer Oct 26, 2011
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Writing a book about haxe seems to me extremely difficult because of the language multi platform nature, constant improvement process and a variety of projects related to the haxe. And I'm glad that Benjamin Dasnois started this great work of collection knowledge about haxe in a book format. This is the first edition and certainly there are a number of things I would suggest to improve.My main complaint is to extreme brevity. Of course a small size is good for a book, but text often doesn't explain why things are so as they are, only enlists strict facts about the language without discussion, gets you know about alternatives or providing a list of advantages and disadvantages. OOP concepts definitions from my point of view are currently poor and I'm sure that it should be said in preface that a reader must be familiar with at least one OOP language to follow the author's ideas easily.The other thing I was very disappointed about is a low quality of the code snippets. Examples contain dead code, number of compile errors, absence of single code style and bad indentation. Also almost everywhere in the book single line comments spread across several lines and that produces errors after copying code into a text editor. As a result there's a strong feeling of incompleteness and mediocrity. Hope it will be corrected as soon as possible.In my opinion there are still enough unexposed places in the book without further references (e.g. how I can make types comparable, what is the range of integer, performance discussion and targets nuances). Some links to the documentation or even well-known discussions in mailing lists would be very useful if it is not possible to include that information into the book.There are several things I would recommend to add to this book. A full description of haxe compiler options because it's not clear how to work with the given examples. More steps how to setup environment for running examples for those who is unfamiliar with described target platform. Also I think that there should be more explanation and some comparisons with other languages to know why one should prefer haxe instead of using them. A fair description of current haXe problems and their possible solutions could enclose a useful chapter. And surely I should mention that book doesn't consider C++ target and has no information about macroses.On the other hand I was glad to get know about templates and SPOD library in haXe and will definitely try to play with them closer. There is a good chapter how to feel comfortable in the community that is very important for haxe beginners.On the whole "haXe 2 Beginner's Guide" in current version is an overview of some haXe possibilities. It could be useful for those who is familiar with OOP concepts and wants to see main features of haxe without a lot of details but with some examples.[...]
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.