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
Arrow up icon
GO TO TOP
haXe 2 Beginner's Guide

You're reading from   haXe 2 Beginner's Guide Develop exciting applications with this multi-platform programming language

Arrow left icon
Product type Paperback
Published in Jul 2011
Publisher
ISBN-13 9781849512565
Length 288 pages
Edition Edition
Languages
Arrow right icon
Toc

Table of Contents (21) Chapters Close

haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting to know haXe FREE CHAPTER 2. Basic Syntax and Branching 3. Being Cross-platform with haXe 4. Understanding Types 5. The Dynamic Type and Properties 6. Using and Writing Interfaces, Typedefs, and Enums 7. Communication Between haXe Programs 8. Accessing Databases 9. Templating 10. Interfacing with the Target Platform 11. A Dynamic Website Using JavaScript 12. Creating a Game with haXe and Flash Pop Quiz Answers Index

Preface

haXe is the universal programming language which is completely cross-platform and provides a standard library that remains the same—regardless of platform.

haXe 2 Beginner's Guide will get you up and running with this exciting language and will guide you through its features in the easiest way possible.

haXe has filled the gap in creating multi-platform applications, and this book will fill the gap in learning all you need to know about haXe—even if it is the first time you have heard of it.

This book will enable you to fully realize haXe's potential for translating code from a haXe program into different languages.

Start with learning how to install haXe, work your way up to templating, and finally learn how to make the same code work for multiple platforms. In between, find heaps of tricks and techniques and work with haXe's typing system. Learn about inheritance, go from learning what a parameter is to creating your own parameterized classes, and find out what the fuss is all about regarding the dynamic type.

By the time you are done with this book, you will find yourself writing efficient haXe code for multiple platforms in less time than you can say "compatible".

Here is haXe

haXe started as a web-oriented programming language and has gone a long way since its debut in late 2005. Its main goal was to correct several big issues within web development, but now people and companies are using it to be able to cover several platforms and devices.

Where and for what is haXe used?

Well, it seems like you are interested in haXe, and we will soon write our first haXe program together. However, before doing that, you may be wondering who uses haXe, and for what kind of tasks or applications it is used.

Where is haXe used?

haXe is actually used in several places. There are some companies that are using it for their professional work. That does not necessarily mean that they use haXe for their whole work; many companies that uses haXe are indeed going through a move towards haXe, or are simply using haXe for some parts of their work and do not necessarily plan to use it more. Even though haXe is not difficult to learn, it is really difficult to change the technologies a company is using.

There are also several open source projects that use haXe. For example, haXe libraries are written in haXe, and many of these are open source.

There are also hobbyists, or students, who are learning and using haXe. Learning haXe and participating in the community helps in learning many programming concepts (the community sometime talks about some people that are not in haXe at the moment but may be added later, for example.) It is also advantageous to learn something other than what is usually taught in schools.

What is haXe used for?

haXe is used for a lot of different things. At first, it is mainly used for making website-related things. This can be web-based tools (such as a mind-mapping program), or games in Flash

or JavaScript.

It can also be used for writing desktop applications. Some people have been using a great combination of Flash and Neko to achieve this. However, this is not the only way to do things! You can write command-line applications that will not need Flash, or you can use or write another graphic library.

One can write servers with haXe too. The framework provides some helpers to do that. One impressive example of a server written in haXe is haXeVideo available at http://code.google.com/p/haxevideo/. This is a video server that allows live streaming of videos to Flash clients. Although it is not ready for production at the moment, it is a great start and can put you on the right track.

haXe can be used to directly target mobile devices such as iOS and Android, but it's also possible to use it to write web-based applications targeting mobile devices. Nowadays, many phones have a browser that is able to run JavaScript code. There are also some runtimes that allow one to create applications based on web technologies running on mobile devices.

One language to rule them all

If you have already been developing web applications, then you certainly know how painful it is to switch from, say, PHP, to JavaScript and Flash. In fact, even the most basic things can become painful in such a scheme. For example, switching from one language to another can make adding objects to an array or a list difficult and error-prone while developing web applications.

haXe is there to unify the development of the three main parts of web applications; with it, you will be able to write your server-code, your client-code, and your rich-client code with only one language: haXe. However, it is also possible to use it with other languages.

Object-oriented programming

haXe also brings the oriented-object programming concept with the well-known principle of classes to platforms that do not natively support it. This way, you will be able to create a program using the well-known concepts of classes, types (also with generics), enums, and others even if this program runs in the browser.

haXe versions

haXe's version 1 was made available in 2006, but since 2008, we have been using haXe 2. haXe 2 brought several changes both to the language and to the library. Therefore, this is the version we're going to learn together. When you get familiar with haXe, you will understand that haXe is a very fast evolving language and sometimes, even minor versions may bring some important new features. This allows haXe to bring you more and more power as time goes by, but keep in mind that it may sometime (although this is very rare) break your code.

Note that in this book, we are using haXe Version 2.0.6.

At the beginning of haXe, only the Flash and Neko targets were available. This means that haXe code could only be compiled to be run on Flash (AVM indeed, as that is the name of the virtual machine) or on the Neko virtual machine. Some weeks after, the already-announced Javascript target made its appearance, but with limited support (particularly in regards to closures). Nowadays, the Javascript target is really mature and can be used without any major problems. Later on, new versions of Flash were supported. All of these targets and the base of the compiler were due to Nicolas Cannasse from Motion-Twin. He is the genius mind behind haXe.

The first target that had been created by someone else was the PHP target. Franco Ponticelli released it hoping that people would experience with it, as it is much easier to get a PHP host than a Neko host. Since then, the PHP target has evolved and has become very mature and may be used in a production environment.

In 2009, the C++ generator was developed by Hugh Sanderson. This target creates C++ code from your haXe code. You can then compile it with a C++ compiler, such as GCC. Beware that it is still under heavy work. Hugh Sanderson is famous in the haXe community for making possible the development of games for iPhone and Android with haXe.

Some people are working on implementing other targets. At the time of writing this book, we know about a Java target in development by yours truly.

Also, note that even though it is not really a target on its own, it is now possible to use the Javascript target to write applications that are to be run on NodeJS.

haXe and the new models of web applications

Nowadays, we see many new and exciting web applications that are using a model that is quite new where the following three parts are communicating together:

  1. The server.

  2. The client-side with Flash.

  3. The client-side with Javascript.

It is usually quite difficult to make those three parts communicate because each language has its own structure. With haXe, as you are using the same language everywhere, you do not have to switch between structures.

In addition, haXe has support for its own remoting; this is a way to allow you to communicate between several haXe applications as seamlessly as possible through the network or between a JavaScript and a Flash application running in the same page.

Some people have also implemented haXeRemoting in other languages making it possible to communicate with applications written in those languages.

haXe as an universal language

Nowadays, haXe can be used in order to do much more than just assisting with web applications. It can now be used in order to create, for example, applications for the iPhone or Android devices or for desktops.

In order to do so, a part of the Flash API has been implemented and made available when targeting C++. This way, it is possible to use this well-known API to create games on Windows, Linux, MacOSX, iPhone, and Android.

Getting help

As you will gain experience with haXe, you will surely encounter new problems to solve. This is an interesting part of programming, but if you find yourself really blocked, then there are several ways to get help.

Reading some documentation

There is some documentation available on the haXe website (http://www.haxe.org/doc) and you can also find the up-to-date API documentation at http://www.haxe.org/api. By the way, the haXe website is a wiki, which means that once you have registered, and when you have enough experience with haXe, you will be able to contribute to the documentation by modifying it or adding some new pages to it. You can also contribute by translating pages into your own language.

Asking questions

At some point, you will certainly want to talk with people about your problems and queries, or maybe share your thoughts about haXe. There are two main places to do that—the haXe forum and the haXe mailing list.

The haXe forum

Once you have registered an account on the haXe wiki, you can use it to post your questions on the haXe forum accessible at http://www.haxe.org/forum. This forum has been created primarily for newcomers who are generally more comfortable with such tools. There, you have great chances of finding other newcomers and also some haXe experts to help you.

The haXe mailing list

The mailing list was the first place where the community grew. Nowadays, it is where most haXe experts are, but they are also pleased to help newcomers there. On the mailing list, people ask questions to solve their problems, but it is also a place to discuss the language and tools evolution. The mailing list can be joined by going to http://lists.motion-twin.com/mailman/listinfo/haxe. If you want to know what is going to happen in the next versions of haXe, this is the place to be.

Some advice

Before you ask a question on the mailing list or on the forum, here is some advice.

  • First of all, always be polite and humble, particularly since you are a new comer.

  • Do not hesitate to introduce yourself in your first message (telling people who you are will help them to identify you, making you part of the community)

  • Always explain what you are trying to achieve. This point is to help people help you. If they know what you are trying to achieve, they will know better how to assist you.

  • Keep in mind that not all participants are native English speakers. Indeed, there are chances that most of them are not. Try to express yourself in an easy-to-understand way.

  • Stay focused on one problem (or one group of linked problems) per thread. If you have several very different problems, create several threads.

  • Always say what platform you are targeting (PHP, Flash, Neko, Javascript, and so on). Some problems may be platform specific.

  • If you have a problem with some code, always try to reduce it to the smallest possible snippet that still reproduces the problem.

  • If you send some code, always try to make it easily understandable. People who are trying to help are generally happy to do so, but if they have to guess what the variable is because you named it "avhgk" instead of "userName", they will sense that you are not even trying to help them help you.

Reading some blogs

There are many people who write about haXe on their own blog.

Nicolas Cannasse

Nicolas Cannasse, haXe's creator, maintains his own blog at http://www.ncannasse.fr where he discusses new and future things in haXe and its ecosystem, Flash, but also about the IT world.

Weblob

http://www.weblob.net is Franco Ponticelli's blog. Should I remind you that Franco Ponticelli is the PHP target creator? Although this is a low-traffic blog, you will find many interesting articles about haXe on it.

GameHaXe

Hugh Sanderson, the creator of the C++ target, maintains his blog located at http://www.gamehaxe.com. You will find some of his thoughts about haXe, the IT world, and experiments about game development in haXe.

He also explains how one can use haXe to target iOS and Android.

A Bug's Life

The author's blog, located at http://www.benjamindasnois.com, is where he talks about haXe. As he is also developing a Java target for haXe, he also writes a lot about it.

Blog.haxe.org

On http://blog.haxe.org, you can find an aggregate of several blogs about haXe. Some of them we have already discussed.

Helping the community

When you have some experience with haXe, you will certainly feel the need to participate in the community and help, as you have been helped. There are several ways to do this.

The mailing list and the forum

At first, and that is certainly the most obvious part, you can answer people's questions on the mailing list and on the forum.

If you do so, do not hesitate to give some advice. Share you experience—people who are migrating from the same language as you (if you are migrating) may want to hear about your experience.

Another way of participating on the mailing list is by making some proposals. If you think that you have a great idea that can make haXe even better, or can help newcomers, or everyday development with haXe, then you should propose it. People will tell you what they think about your idea and will eventually help you to make them come true.

The wiki

You can register on the wiki and write on it. There are several things that are needed on the wiki:

  • Tutorials

  • Updating the documentation according to changes

  • Updating the documentation to reflect important things said on the mailing list

  • Translating into your own language

Write on your blog or website

If you have a blog or a website, then you can write about haXe and your haXe experience on it. People always want to know how others have made the switch and how well it went. Having several sites talking about haXe helps spread the word too. Moreover, you can explain how you achieved a task and write a tutorial.

Writing libraries or tools

You can also write some libraries or tools and share them. As a developer, you will certainly use someone else's library at some point just because it is useless to rewrite things if they are already written well. So, think that someone else may also be interested in your work and distributing your libraries or tools is a great way to give back to the community.

You can also correct bugs in the haXe library and help maintaining libraries, so that they stay compatible with new versions of haXe.

You can find some libraries on http://lib.haxe.org.

Talking about haXe

Maybe, you are part of an association of programmers, or you are a student in an IT oriented school, or even a teacher. If this is the case, then maybe you can organize some conferences to talk about haXe. It is difficult to get people to know about a new and young language, such as haXe, because schools do not teach it and not many sites are talking about those languages.

Some people are already organizing such events (sometimes, even with workshops) and the more there are, the better it will be, because people will at least know about haXe and they can then decide if they want to use it or not (I do believe they will want to use it!).

If haXe is well-known, then you will certainly have more opportunities to use it.

What this book covers

Chapter 1, Getting to know haXe. In this chapter, you will be learning about haXe's history along with how to install it. You will also get some important information about how you can interact with the haXe community.

Chapter 2, Basic Syntax and Branching. In this chapter, you will learn about the basics of the haXe syntax. You will also learn how to make your programs behave differently depending on the situation.

Chapter 3, Being Cross-platform with haXe. In this chapter, you will learn how your code can be used to create programs that are going to run on several platforms.

Chapter 4, Understanding Types. In this chapter, you will learn about haXe's typing system.

Chapter 5, The Dynamic Type and Properties. In this chapter, you will learn about the Dynamic type. You will also learn how you can give your objects some properties.

Chapter 6, Using and Writing Interfaces, Typedefs, and Enums. In this chapter, you will learn what interfaces, typedefs, and enums are and how you can use them to improve your code.

Chapter 7, Communication Between haXe Programs. In this chapter, you will learn how to get your different haXe programs to communicate together by using haXeRemoting.

Chapter 8, Accessing Databases. In this chapter, you will learn how to access, read, and write to and from databases.

Chapter 9, Templating. In this chapter, you will learn how one can use templating in haXe to create, for example, views.

Chapter 10, Interfacing with the Target Platform. In this chapter, you will learn how you can access native functions and features from the platform you are targeting.

Chapter 11, A Dynamic Website Using Javascript. In this chapter, you will learn how you can use Javascript to create a website.

Chapter 12, Creating a Game with haXe and Flash. In this chapter, you will be guided through the creation of a game.

Appendix. In this section, we provide answers to the pop quiz.

What you need for this book

In order to run the code from this book, you will need the following:

  • The haXe compiler

  • The Neko runtime (it is installed with haXe if you use the haXe installer)

  • A PHP capable web server (you can use solutions such as WAMP/XAMP on Windows or MAMP on MacOSX)

  • A MySQL server (included in solutions named before)

  • SWFMill (http://swfmill.org/):

Who this book is for

This book is written for both beginners and developers who want to learn this multi-platform programming language to build web applications from scratch.

Conventions

In this book, you will find several headings appearing frequently.

To give clear instructions of how to complete a procedure or task, we use:

Time for action – heading

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

This heading explains the working of tasks or instructions that you have just completed.

You will also find some other learning aids in the book, including:

Pop quiz – heading

These are short multiple choice questions intended to help you test your own understanding.

Have a go hero – heading

These set practical challenges and give you ideas for experimenting with what you have learned.

You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "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."

A block of code is set as follows:

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

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

class TesthaXe
{
   public static function main(): Void
   {
      var e = new EReg("^Hello.*",""); //Creates an EReg matching any 
        string beginning with Hello
      if(e.match("Hello Benjamin"))
      {
         neko.Lib.println("Matches");
      } else
      {
         neko.Lib.println("Does not match");
      }
   }
}

Any command-line input or output is written as follows:

X-Cache : MISS from rack1.tumblr.com
X-Cache-Lookup : MISS from rack1.tumblr.com:80
Via : 1.0 rack1.tumblr.com:80 (squid/2.6.STABLE6)
P3P : CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Set-Cookie : tmgioct=h6NSbuBBgVV2IH3qzPEPPQLg; expires=Thu, 02-Jul-
2020 23:30:11

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Go to http://www.haxe.org and click on the Download link".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.1

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image