haXe 2 Beginner's Guide
()
About this ebook
Related to haXe 2 Beginner's Guide
Related ebooks
Vaadin 7 UI Design By Example: Beginners Guide Rating: 0 out of 5 stars0 ratingsLaTeX Beginner's Guide Rating: 4 out of 5 stars4/5GNU Octave Beginner's Guide Rating: 3 out of 5 stars3/5Game Programming Using Qt: Beginner's Guide Rating: 0 out of 5 stars0 ratingsWordPress Plugin Development: Beginner's Guide Rating: 0 out of 5 stars0 ratingsLearning C# by Developing Games with Unity 3D Beginner's Guide Rating: 5 out of 5 stars5/5RavenDB 2.x Beginner’s Guide Rating: 0 out of 5 stars0 ratingsPentaho Data Integration Beginner's Guide Rating: 4 out of 5 stars4/5Mastering Python Rating: 0 out of 5 stars0 ratings.NET Generics 4.0 Beginner’s Guide Rating: 0 out of 5 stars0 ratingsUnreal Development Kit Game Programming with UnrealScript Beginner's Guide Rating: 3 out of 5 stars3/5Sage Beginner's Guide Rating: 4 out of 5 stars4/5qooxdoo Beginner's Guide Rating: 0 out of 5 stars0 ratingsEclipse Plug-in Development: Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsSquid Proxy Server 3.1 Beginner's Guide Rating: 3 out of 5 stars3/5concrete5 Beginner's Guide Rating: 0 out of 5 stars0 ratingsOracle ADF 11gR2 Development Beginner's Guide Rating: 0 out of 5 stars0 ratingsMooTools 1.2 Beginner's Guide Rating: 0 out of 5 stars0 ratingsInkscape Beginner’s Guide Rating: 5 out of 5 stars5/5Apache Roller 4.0 – Beginner's Guide Rating: 0 out of 5 stars0 ratingsHTML5 Web Application Development By Example Beginner's guide Rating: 0 out of 5 stars0 ratingsconcrete5 Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsCacti 0.8 Beginner's Guide Rating: 0 out of 5 stars0 ratingsHadoop Beginner's Guide Rating: 4 out of 5 stars4/5Celtx: Open Source Screenwriting Beginner's Guide Rating: 5 out of 5 stars5/5Modernizing Legacy Applications in PHP Rating: 0 out of 5 stars0 ratingsAlfresco One 5.x Developer’s Guide - Second Edition Rating: 3 out of 5 stars3/5NumPy Beginner's Guide Rating: 5 out of 5 stars5/5CouchDB and PHP Web Development Beginner’s Guide Rating: 0 out of 5 stars0 ratingsSparrow iOS Game Framework Beginner’s Guide Rating: 0 out of 5 stars0 ratings
Information Technology For You
Summary of Super-Intelligence From Nick Bostrom Rating: 5 out of 5 stars5/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Computer Science: A Concise Introduction Rating: 4 out of 5 stars4/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Supercommunicator: Explaining the Complicated So Anyone Can Understand Rating: 3 out of 5 stars3/5CompTIA A+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Core 1 Exam 220-1101 Rating: 0 out of 5 stars0 ratingsCompTIA Network+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Exam N10-008 Rating: 0 out of 5 stars0 ratingsPanda3d 1.7 Game Developer's Cookbook Rating: 0 out of 5 stars0 ratingsLinux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5ChatGPT: The Future of Intelligent Conversation Rating: 4 out of 5 stars4/5How To Use Chatgpt: Using Chatgpt To Make Money Online Has Never Been This Simple Rating: 0 out of 5 stars0 ratingsAn Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5Practical Ethical Hacking from Scratch Rating: 5 out of 5 stars5/5Hacking Essentials - The Beginner's Guide To Ethical Hacking And Penetration Testing Rating: 3 out of 5 stars3/5Windows Registry Forensics: Advanced Digital Forensic Analysis of the Windows Registry Rating: 4 out of 5 stars4/5Health Informatics: Practical Guide Rating: 0 out of 5 stars0 ratingsThe Basics of Hacking and Penetration Testing: Ethical Hacking and Penetration Testing Made Easy Rating: 4 out of 5 stars4/5Computer Organization and Design: The Hardware / Software Interface Rating: 4 out of 5 stars4/520 Windows Tools Every SysAdmin Should Know Rating: 5 out of 5 stars5/5Quantum Computing for Programmers and Investors: with full implementation of algorithms in C Rating: 5 out of 5 stars5/5A Mind at Play: How Claude Shannon Invented the Information Age Rating: 4 out of 5 stars4/5The Programmer's Brain: What every programmer needs to know about cognition Rating: 5 out of 5 stars5/5Cybersecurity for Beginners : Learn the Fundamentals of Cybersecurity in an Easy, Step-by-Step Guide: 1 Rating: 0 out of 5 stars0 ratingsCompTIA ITF+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Exam FC0-U61 Rating: 0 out of 5 stars0 ratingsThe Ultimate Guide to Landing a Network Engineering Job Rating: 0 out of 5 stars0 ratingsInkscape Beginner’s Guide Rating: 5 out of 5 stars5/5
Reviews for haXe 2 Beginner's Guide
0 ratings0 reviews
Book preview
haXe 2 Beginner's Guide - Benjamin Dasnois
Table of Contents
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why subscribe?
Free access for Packt account holders
Preface
Here is haXe
Where and for what is haXe used?
Where is haXe used?
What is haXe used for?
One language to rule them all
Object-oriented programming
haXe versions
haXe and the new models of web applications
haXe as an universal language
Getting help
Reading some documentation
Asking questions
The haXe forum
The haXe mailing list
Some advice
Reading some blogs
Nicolas Cannasse
Weblob
GameHaXe
A Bug's Life
Blog.haxe.org
Helping the community
The mailing list and the forum
The wiki
Write on your blog or website
Writing libraries or tools
Talking about haXe
What this book covers
What you need for this book
Who this book is for
Conventions
Time for action – heading
What just happened?
Pop quiz – heading
Have a go hero – heading
Reader feedback
Customer support
Downloading the example code for this book
Errata
Piracy
Questions
1. Getting to know haXe
Installing haXe
Two ways to install: The installer and sources compilation
Installing on Windows
Installing on MacOSX
Installing on Linux
Installing nightly builds
Verifying your installation
Choosing an editor
FlashDevelop 3
The TextMate bundle
VIM
Writing your first program
Time for action – Writing a Hello World
What just happened?
A program with some interaction
Time for action – Interacting with the user
What just happened?
Pop quiz – basic knowledge
Summary
2. Basic Syntax and Branching
Modules, packages, and classes
Packages
Modules
Classes
Accessing a class
Constants and its types
Booleans
Integers
Floats
Base 10
Scientific notation
Strings
Regular expressions
The null value
Flash9 and above
Binary and unary operators
Binary operators
Assigning values
Comparison operators
Arithmetic operators
Boolean operators
Bitwise operators
Unary operators
Blocks
Variable declaration and scope
Declaring a variable
At class level
Time for action – Declaring some fields
What just happened?
In a block of instructions
Field access and function calls
Constructing class instance
Conditional branching
If
Switch
Loops
While
For
Break and continue
Time for action – Using the break keyword
What just happened?
Time for action – Using the continue keyword
What just happened?
Return
Exception handling
Anonymous objects
Local functions
Managing a fridge
Time for action – Managing a fridge
What just happened?
Have a go hero – Throw Exceptions to prevent crashes
Summary
3. Being Cross-platform with haXe
What is cross-platform in the library
Object storage
The Std class
The haxe package
The haxe.remoting package
The haxe.rtti package
The haxe.Http class
Regular expressions and XML handling
Regular expressions
XML handling
Input and output
The DOM interface
The Flash interface
The standard input/output interface
Platform-specific packages
JavaScript
Flash
Neko
PHP
C++
Conditional compilation
Conditional compilation depending on flags
Conditional compilation depending on the target
The remap switch
Coding cross-platform using imports
Time for action – Welcoming the user on Neko & PHP
What just happened?
Pop quiz – Writing cross-platform code
Have a go hero – Handle XML
Time for action – Reading from the XML file
What just happened?
Time for action – Writing to an XML file
What just happened?
Testing our sample
Making it cross-platform
Summary
4. Understanding Types
Explicitly typed variables
Static typing
Values with several types
Defining a type
Inheritance
Multi-inheritance
Implementing an interface
Representing a blog article
Time for action – Representing different types of articles
Function's type
Expressing a function's type
Functions using functions
Dynamic functions
Anonymous objects
Duck typing
Creating a function to apply another one
Time for action – Applying a function on every item
What just happened?
Type inference
Assigning a value
Assigning the value of the variable to another one
Passing the variable as a parameter
Casting
Safe casting
Unsafe casting
Untyped
Type parameters
Usage of Type parameters
Creating a parameterized class
Constraint parameters
Extending the fridge
Time for action – A fridge with constraints
What just happened?
Have a go hero – Creating a typed container
Pop quiz – Verify your knowledge
Summary
5. The Dynamic Type and Properties
Freeing yourself from the typing system
Time for action – Assigning to Dynamic variables
Time for action – Assigning from Dynamic variables
Field access
Functions in Dynamic variables
Parameterized Dynamic class
Classes implementing Dynamic
Time for action – Implementing a non-parameterized Dynamic
What just happened?
Time for action – Implementing a parameterized Dynamic
What just happened?
Using a resolve function when implementing Dynamic
Time for action – Writing our Component class
Time for action – Parsing the configuration file
Time for action – Testing our parser
Time for action – Writing the configuration file
Time for action – Testing the writer
The dot-notation
Thoughts on writing our parser
The dynamic keyword
Warning
Properties in classes
Use
Implementation
Writing the getter
Writing the setter
Defining the property
Accessibility
The default getter
The default setter
The dynamic getter and setter
The never getter and setter
A compile-time feature
Have a go hero – Writing a person class
Pop Quiz – Doing the things
Summary
6. Using and Writing Interfaces, Typedefs, and Enums
Interfaces
Purpose
Default visibility
Type inference
Implementing an interface
Implementation and type inference
Implementation and visibility
Making use of interfaces
Typedefs
Time for action – Naming Anonymous Types
What just happened?
Aliases
Visibility
Private typedefs
Duck typing
Typedefs to access private members
Creating iterable objects
Enums
Basic enums
Enums' constructors
Switching on basic enums
Enums with parameters
Time for action – Switching over enums with parameters
What just happened?
Have a go hero – Write a fridge with an iterator
Time for action – Writing our Fridge class
What just happened?
Time for action – Making it possible to iterate over a fridge
What just happened?
Time for action – Creating the iterator
What just happened?
Our main function
Pop quiz – Typedef, interfaces, and Enums
Summary
7. Communication Between haXe Programs
The haXe serializer
Usefulness
Possibilities
Class instances
Custom serialization
Enum instances
Unserializing exceptions
Reducing size and preventing infinite loops
haXe remoting
Supported protocols and layers
Over HTTP
Implementation
Using the ExternalInterface
The JavaScript side
The Flash side
Using AMF
Time for action – Writing a zoo management tool
The Animal class
Representing the zoo
Time for action – Saving the list
Time for action – Loading the list
The remoting service
The getList function
The createAnimal function
Tying it together
The client
Time for action – Initializing the client connection
What just happened?
The createAnimal function
The listAnimals function
The main menu
Compiling the client
Have a go hero – Represent data from JS in Flash
Pop quiz – Doing the things
Summary
8. Accessing Databases
Connecting to databases
php.db.Connection and neko.db.Connection
SQL usage
The interface
The close function
The commit function
The rollback function
The escape function
The quote function
The request function
The addValue function
The dbName function
The lastInsertId function
Connecting to the database
MySQL
SQLite
Dealing with results
Typing
The SPOD
Setting a SPOD object
The table name
Non-mapped fields
The cache
A concrete example of SPOD use
Setting the object
Setting the connection
The Manager
The all function
The count function
The delete function
The get function
The getWithKeys function
The object function
The objects function
The search function
Handling relations
Creating a blogging system
Time for action – Creating the Users table
What just happened?
Time for action – Creating the User class
What just happened?
Time for action – Creating the Posts table
Time for action – Creating the Post class
What just happened?
Time for action – Creating the connection to the database
What just happened?
Time for action – Listing posts
Time for action – Listing users
Time for action – Adding a user
Time for action – Adding a post
Time for action – Branching it with the main function
Have a go hero – Listing posts from a specific user
Pop quiz – Doing the things
Summary
9. Templating
Introduction to the haxe.Template class
Printing a value
Branching
Using lists, arrays, and other iterables
Time for action – Executing code from a template
What just happened?
Integrating subtemplates
Creating a blog's front page
Time for action – Creating our SPOD class
Time for action – Connecting to the database
Time for action – Creating our template
Time for action – Reading the template from resources
Time for action – Executing the template
Time for action – Testing the result
Pop quiz – Doing the thing
Have a go hero – Doing more with the thing
Summary
10. Interfacing with the Target Platform
Extern classes
Time for action – Writing an extern
What just happened?
Using an extern
Class path
Limitations
The __init__ magic
Native types and haXe types
The Neko and PHP cases
PHP functions
Neko functions
Magic functions
The Flash magic
__new__
__arguments__
__typeof__
__delete__
__eval__
__gettimer__
__geturl__
__instanceof__
__random__
__trace__
__keys__
__global__
__as__
__is__
__in__
The Neko magic
Arrays and strings
The PHP magic
__php__
__call__
__var__
__physeq__
Have a go hero – Wrapping parameters in an array
Pop quiz – Doing the things
Summary
11. A Dynamic Website Using JavaScript
Manipulating the DOM
The browser's DOM
Nodes
Events
Level 1
Level 2
Time for action – Using events from the DOM level 0
What just happened?
Time for action – Using DOM level 2 events
What just happened?
Time for action – Working around by storing reference to functions
What just happened?
Have a go hero – A dynamic website with JavaScript
Time for action – Setting up the model
Time for action – Setting up our view
What just happened?
Time for action – Setting up the controller
Time for action – Creating a computer view
What just happened?
The main class
Summary
12. Creating a Game with haXe and Flash
Embedding assets
Time for action – Installing SWFMILL
Time for action – Creating a library
Time for action – Using assets
Event handling
Have a go hero – A game with Flash
The game design
The assets
The player's character
Time for action – Drawing the character
Time for action – Handling moves
What just happened?
Time for action – Moving the character
What just happened?
Time for action – Adding balls
What just happened?
Time for action – Updating the position
What just happened?
Time for action – Detecting collisions
What just happened?
Have a go hero – Adding a proper score display
Have a go hero – Adding an online score table
Pop quiz – Software used
Summary
A. Pop Quiz Answers
Chapter 1: Getting to know haXe
Chapter 3: Being Cross-platform with haXe
Chapter 4: Understanding Types
Chapter 5: The Dynamic Type and Properties
Chapter 6: Using and Writing Interfaces, Typedefs, and Enums
Chapter 7: Communication Between haXe Programs
Chapter 8: Accessing Databases
Chapter 9: Templating
Chapter 10: Interfacing with the Target Platform
Chapter 12: Creating a Game with haXe and Flash
Index
haXe 2
haxe 2
Copyright © 2011 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2011
Production Reference: 1180711
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849512-56-5
www.packtpub.com
Cover Image by Asher Wishkerman ( <a.wishkerman@mpic.de >)
Credits
Author
Benjamin Dasnois
Reviewers
Marcus Bergström
Andy Li
Franco Ponticelli
Acquisition Editor
Dilip Venkatesh
Development Editor
Maitreya Bhakal
Technical Editors
Kavita Iyer
Azharuddin Sheikh
Copy Editor
Neha Shetty
Project Coordinator
Shubhanjan Chatterjee
Proofreader
Lisa Brady
Indexer
Tejal Daruwale
Production Coordinator
ArvindKumar Gupta
Cover Work
ArvindKumar Gupta
Foreword
Remember the olden days?
I vaguely remember this odd box called a computer gradually conquering corners of my living room and bedroom. Can you recall those times? We would use a computer
for some specific tasks, such as writing a letter.
The term computer was quite popular back then. Now, the word is no longer part of our active vocabulary, let alone use a computer
, who says that anymore? Nobody does. That is because computers are no longer odd boxes in corners. They are the notebooks and mobile phones that we use every day. They are also the hybrids between the two, such as tablets and netbooks. They are the servers that run websites we constantly use as well. Even today's TVs are full-blown computers.
I find this change fascinating. I think we live in a very exciting time to be participating in the business. All these new devices bring us opportunities enabling us to express our creativity, while their differences challenge us and keep us inventive.
Unlike PHP or ActionScript, haXe is not designed for one specific purpose. It is just a programming language. That is why it is so versatile. Whether it is the hip new phone in the hands of a Japanese ad girl or a big bulky machine in a server room; whether it has a mouse, a multi-touch screen, a bendable/deformable interface; or even a mind reading sensor, haXe can be remixed and used for the device.
This means that you can use the same language and even the same codebase across the browser and the server part of your project, for example. To me, that is why haXe is so relevant.
Benjamin Dasnois is one of the people that has been around since haXe-day one. He remixes haXe to make it fit new needs. Benjamin is best known within the community for working on the Java target. (I would also say, that he is second to none for his funny French accent.)
In this book, Benjamin will introduce you to the world of haXe. Once you get to the last page, you will have the skill set to start developing for a browser and a big bulky server using the same language.
However, remember: a programming language is never your goal; it is a tool that assists you in achieving your goal, be it creating awesome software that enriches all of our lives or helping us achieve world domination.
Pimm Hogeling
haXe Developer and Interface Designer
About the Author
Benjamin Dasnois has always been fascinated by open source software and as such, has been giving courses about Linux in an IT school in France. In the meantime, Benjamin has been following and working with haXe since its beginning and uses it in his professional life. He now works on an open source project, started the integration of haXe into it, and made it work with the technologies that were already in use.
I would like to thank the entire haXe community, my schoolmates, and former students who really made me discover the contribution I could make by imparting my knowledge or communicating my skills.
About the Reviewers
Marcus Bergström is the Technical Director and a co-owner of the digital creative studio Quickform. Working on a day-to-day basis with projects, spanning everything from websites to business applications, he knows the importance of choosing the right tool for the job. With a portfolio packed with world known brands, quality assurance is paramount, as well as production speed.
As one of the early adopters of the language, Marcus has been using haXe as his secret weapon while spearheading all the developments of haXe within the company for years. His favorite features include the strict typing and the ability to write both client and server-side applications in one language.
His business is located in the sunny south of Spain where he lives with his wife and two children. He believes that Sundays are best enjoyed with a cold beer after a good round of golf with friends.
Andy Li is a haXe developer in Hong Kong, with a Bachelor's Degree (first class honors) in Creative Media from City University of Hong Kong. He is now a PhD student in the School of Creative Media, City University of Hong Kong, researching in the fields of computer vision, computer graphics, and installation art.
He has been actively involved in the haXe community, producing several open source haXe libraries, including CasaHx (haXe port of CasaLib for AS3), hxColorToolkit (toolkit for managing colors), hxOpenFrameworks (haXe binding to openFrameworks), and jQueryExtern (haXe extern file for jQuery).
His personal blog, http://blog.onthewings.net/, contains many open source experiments and programming tips.
Franco Ponticelli is an experienced software developer and a problem solver. An Architecture graduate with specialization in Industrial Design, he performs many different activities in the Information Technology area from 3D computer graphics to hard-core software development. In his constant research for the perfect development environment, he found haXe and fell in love with it.
He is the author of the book Professional haXe and Neko published by Wrox.
I would like to thank my wife and kids for their support.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
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