Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
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
zł39.99 zł158.99
Paperback
zł197.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781849512572
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 26, 2011
Length: 288 pages
Edition :
Language : English
ISBN-13 : 9781849512572
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 zł20 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 zł20 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 528.97
OpenGL 4.0 Shading Language Cookbook
zł221.99
haXe 2 Beginner's Guide
zł197.99
Haxe Game Development Essentials
zł108.99
Total 528.97 Stars icon
Banner background image

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.