Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
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
$28.99 $19.99
Print
$48.99
Subscription
$15.99 Monthly

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Black & white paperback book shipped to your address
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
Buy Now

Product Details


Publication date : Jul 26, 2011
Length 288 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781849512565
Category :
Languages :
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

Key benefits

What you will learn

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Black & white paperback book shipped to your address
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
Buy Now

Product Details


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

Table of Contents

21 Chapters
haxe 2 Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
Foreword Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
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
Pop Quiz Answers Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


Lars Mathiasen Feb 6, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Feefo Verified review Feefo image
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela