Book Image

Haxe Game Development Essentials

Book Image

Haxe Game Development Essentials

Overview of this book

Haxe is a powerful and high-level multi-platform language that's incredibly easy to learn. Used by thousands of developers and many high-profile companies, Haxe is quickly emerging as a forerunner in the area of cross-platform programming. OpenFL builds on top of Haxe to make developing for multiple platforms quick and painless. HaxeFlixel provides you with the tools you need to build amazing 2D games easier than ever before. Cross-platform development has been supercharged using the Haxe programming language, making it increasingly easy and hassle-free to develop multi-platform games. If you've programmed games before and want to learn out how to deliver games across multiple platforms, or develop games faster, then Haxe Game Development Essentials is the book for you. It starts by showing you how to set up your development environment, then running you through some Haxe language fundamentals, and finally taking you through the process of programming a game from start to finish. You will learn how to create a side scrolling shooter game using HaxeFlixel. Next you will learn to enhance the game with new gameplay features, user interfaces, animations, sound, and configuration files to make your game expandable. Once your game is built and ready, you will learn how to deploy it to web, Android, iOS, and desktop systems. By the end of this book, you will be confident about creating multi-platform games using Haxe, OpenFL, and HaxeFlixel in a faster and easier way.
Table of Contents (16 chapters)
Haxe Game Development Essentials
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

Hello World


Now that we have everything we need installed, we should make sure that Haxe, OpenFL, and HaxeFlixel are all up and running. To do this, we'll create a new project using the command line, run it, and then add an image to the screen to ensure that we can make changes.

Creating a project

To create a project, open up a command or terminal window and navigate to the folder in which you want your game's folder to be created.

After doing this, enter this command:

flixel tpl -n "HelloWorld"

This will create a new folder using the standard HaxeFlixel project template. It will also create project files for your code-editing tool of choice, allowing for quick integration out of the box.

Tip

If HaxeFlixel doesn't automatically open your project in your code-editing tool, open it in the tool manually. For FlashDevelop users, double-click on the .hxproj file in the project's folder. For Sublime Text users, drag your project's folder into an empty Sublime window.

A number of Haxe classes with the file extension .hx will be created. These are the base files you need to get a basic HaxeFlixel project up and running. We'll be going over the project's structure throughout the book, so it's not important to know what everything means right now.

Running the project

Now that we have a project set up, we can run it to see what we get out of the box.

If you're running FlashDevelop, select Debug from the drop-down list next to the play button and then select flash from the drop-down list next to that. After this, hit the play button to run your project.

For Sublime Text users, you can press Ctrl + Enter to run the project using the Haxe package that was installed earlier.

To create a build using the command line, navigate to your project's folder and enter this command:

lime test flash

The project should build and then open with your operating system's default application for running .swf files. When the .swf file loads, you'll briefly see the HaxeFlixel logo and then you'll be presented with a blank screen.

Copying assets

We're going to need the image that we want to display, so let's get that now. In the assets provided for this chapter, there is an image named HelloWorld.png. Copy it to the images folder under assets in your project's directory.

Making changes

Now that we have the image copied to the template project, let's add the traditional Hello World image to the screen.

Open MenuState.hx and navigate to the Create function. It will look like this:

override public function create():Void
{
  super.create();
}

After super.create();, add the following lines:

var helloWorldText = new FlxSprite();
helloWorldText.loadGraphic(AssetPaths.HelloWorld__png);
add(helloWorldText);

This will create a new variable of the FlxSprite class. It will then load in the image we just copied over, and then finally add the image to the screen. We'll go into more detail about how to use the FlxSprite class and work with loaded assets later, but that's a quick rundown of what's happening.

All that's left to do is to run the project to see your changes! In FlashDevelop, you just need to press the play button. For Sublime Text users, press Ctrl + Enter.

To create a build using the command line, enter the test command again:

lime test flash

That's it! You should see your SWF load and display the text Hello world!. If something along the way didn't work, take some time to review the chapter to see where things went wrong.