HaxeFlixel Tutorial – Particles and Emitters

If you check the HaxeFlixel’s particle demo page, you can see some basic examples of particle usage. In this tutorial, we’ll cover the following sections:

  • Create default particle emitter
  • Adding custom image
  • Animating custom image
  • Adjusting particle properties

Setup

Let’s create a new HaxeFlixel template project as usual:

For assets, we can use any image for testing custom particle images, so let’s go for Julien’s Sparkles spritesheet. Download the zip and extract the sparkle.png into your /assets/images  folder.

Introduction

Particles are generated from emitters. As such, you only need to check the API pages for FlxParticle and FlxEmitter for more info on how to implement particle effects. In a nutshell, you need an emitter (FlxEmitter) to “emit” particles (FlxParticle).

Default Particle Emitters

Following the code from FlashGameDojo (it’s in ActionScript 3, but it’s very similar to Haxe anyway) — here’s the most basic code to write an emitter:

And when you build with lime test neko , you see the result:

licecap-particle

You may notice the default white particles emitting correctly, but there seems to be a problem — the default HaxeFlixel logo gets emitted too. What went wrong?

Note that the white particles are the 2×2 pixel white squares we wrote in the p.makeGraphic(...)  line above. We only created 5 particles for the emitter in the loop. Since the emitter needs to emit 1 particle per 0.1 second, it will run out of particles in its pool after 0.5 seconds. As a result, it generates its own FlxParticle objects that do not have a default image — resulting in the logo.

So the easiest fix is to adjust the loop to add more particles, from 5 to say, 15 or 20, so we’ll never run out of particles:

And now it looks fixed:

licecap-particle-2

Custom Particle Images

To add custom images to particles, you just need to load a graphic during particle creation:

Just note the size of each sprite in the spritesheet. In the case of sparkle.png, each frame is 32×32. Now when you test, the sparkle image is used:

licecap-particle-3

It’s a little too small to see, but that’s the default size of the first sparkle frame’s image anyway.

Animating Particles

To animate the particles, you just need to add animation during creation, exactly like how you would for FlxSprites:

And the output:

licecap-particle-4

Okay, using what we’ve learned so far, let’s try different examples on how to use the emitter:

  • Fire smoke trail
  • Explosion
  • Explosion with particle gravity

Fire Smoke Trail

We’re going to create smoke puffs that goes upwards, slowly fading away. Among the attributes we set include alpha fading, color fading, and size changing, and particle origin jitter:

And here is the result:

licecap-particle-5

One thing I excluded was setting the rotation — by default, particles will rotate randomly. But if you don’t want particles to rotate, just set it like this:

Explosions

Explosions are the default behaviour of FlxEmitter — as demonstrated at the beginning of this tutorial. Referring to the API, the emitter’s default start  function only “emits” the explosion once, like this:

Explosions With Particle Gravity

This is demonstrated in the Particle demo page — it’s as easy as adding gravity to the emitter:

And the result:

licecap-particle-6

Note the code above assumes we’re emitting the explosion in 0.02 second intervals. If you only want to do it once, set the interval to 0 instead of 0.02

One last thing — to stop the emitter, just do this:

Closure

Particle effects can be easily added to the game using FlxEmitter. The most common usage is explosion and smoke.

One example is “bursting coins out of a treasure chest”, where the emitter “emits” a single burst of coins. If you want an example of how to do this, check out PhotonStorm’s excellent Collectible Particles tutorial.

Check out his other Flixel tutorial series, they’re very helpful! They’re in ActionScript 3, but still relevant.

This concludes the tutorial on HaxeFlixel particles and emitters.

HaxeFlixel Tutorial – Parallax Scrolling

One great way to enhance the feel of your game is parallax scrolling. I could only find tutorials that briefly mention it, so here’s one dedicated to parallax scrolling. In most cases, parallax is used on horizontal plane, so we’ll proceed with that in mind.

Setup

For placeholder images, let’s use some background images from OpenGameArt, Red Baron Backgrounds by MindChamber. Download the zip, and extract it somewhere.

We’ll be using the following files for this tutorial: highwayz – Copy.jpg and highwayz_floor.jpg. I didn’t include the images here in this tutorial so you would go and download the original files from the link above — to support the author 🙂 Anyway, put those two images into your /assets/images  folder and move on.

Now let’s create a HaxeFlixel template project, as usual:

Now we can proceed with the code-related stuff.

Code Setup

Parallax effect can be easily achieved by setting the scrollFactor  value.

If you want to have parallax backgrounds which are repeated, you will need FlxBackdrop , which is part of the flixel-addons  haxelib. To enable the flixel-addons haxelib, go to your Project.xml file and enable the following code:

To use parallax effect, you only need to do two things:

  1. Create an FlxSprite (or anything derived from FlxObject) and set its scrollFactor  value
  2. OR create a FlxBackdrop  and specify the X/Y scroll rate, and whether it repeats on the X-plane and/or Y-plane.
  3. Scroll the camera!

Parallax with [scrollFactor]

The code below demonstrates the above parallax with only scrollFactor :

And that’s it! Build with lime test neko  and you can test the parallax with the LEFT/RIGHT arrow keys.

licecap-parallax-1

From other tutorials, like this (Dustytome) and this (Haxecoder), we can observe that scrollFactor/parallax technique can be applied to groups of objects or even UI elements, depending on your creativity. 🙂

Parallax with [FlxBackdrop]

If you want repeated backgrounds in your parallax scrolling, just modify the following lines from the above code:

And here’s the result:

licecap-parallax-2

 

There’s nothing much on the API page for FlxBackdrop, so I guess the usage is straight-forward.

HaxeFlixel Tutorial – Multiple Cameras

If you check the flixel-demos Github page, you can find sample code on how to implement split screen camera, as shown on the demo page.

In this tutorial, we will create a simple platformer which implements 4 cameras. The purpose is to demonstrate that it is possible to have multiple cameras with different properties.

Setup

As usual, let’s create a new template HaxeFlixel project from the terminal:

We won’t be needing any assets, because we’ll use the default HaxeFlixel graphics.

Code

First, let’s create a map:

And here’s the result when you build it with lime test neko :

Screen Shot 2015-04-11 at 10.31.30 AM

And now let’s add a playable character, complete with collision and movement logic:

And here’s how it’ll look:

licecap-camera2

Although it isn’t shown, the camera takes up the full area of the screen (in the case above, it’s 640 x 480). We can resize the cameras to only fit a portion of the screen — and along with this, we can create multiple cameras, to achieve split-screen multiplayer effects. Let’s add some code to make this happen:

Note: The FlxG has a variable camera  that controls the first main camera, whereas the cameras  variable holds an list of all the cameras. Be careful not to make typos here.

Now when you test, you’ll get this:

licecap-camera2-2

Notice that the background of each camera is of a different colour, but the screen is still showing the same content (one red player moving around). What this means to us, is that we know each camera has properties that can be customised individually.

Now, by knowing this, we can expand on the code to do the following:

When you test, you will see that this is starting to look like a 4-player split-screen game:

licecap-camera2-3

Please excuse the erratic movements — I was mashing my keyboard to simulate 4 players moving around in their individual screens.

Anyway, I hope this tutorial gives you an idea of how multiple cameras can be used (e.g. for mini-maps or split-screen multiplayers). You can refer to the FlxCamera API for more info. This concludes the tutorial on creating multiple cameras in HaxeFlixel.

HaxeFlixel Tutorial – Using The Camera

Today we’re going to cover the FlxCamera class, and its common uses in HaxeFlixel. Here’s a list we’ll be going through:

  • Camera shake
  • Camera flash/fade/fill
  • Camera follow/lerp/bounds

Project Setup

First, let’s create a template HaxeFlixel project, as usual:

To demonstrate camera scrolling and follow functions, let’s create a large CSV map data. Copy the data below, save it as map-data.csv, and put it in your /assets/data  folder:

For the image tilesheet, let’s go to OpenGameArt.org and use one of the tiny 16 basic spritesheets. Download the basictiles.png (a.k.a. basictiles_2.png) and put it in the project’s /assets/images  folder.

Now we can begin coding.

Map & Player Setup

We will not be loading map data using TMX file this time — for simplicity sake, we just load using CSV file. We’ll skip the explanation and jump straight to setting up the map code. Let’s start with the MenuState.hx:

If you build with lime test neko  now, you should see this:

Screen Shot 2015-04-06 at 11.34.00 AM

Note: If the map doesn’t show up, make sure your .csv file does not end with an empty line.

Let’s set the resolution lower so we can zoom in on the map. In the Main.hx, change the resolution width/height from 640×480 to 320×240:

Now if you build again with lime test neko , you should get this:

Screen Shot 2015-04-06 at 11.37.12 AM

Oh, note that the pillar tile is going to be our player tile. Please excuse the poor choice of graphic; We’re just using one placeholder tilesheet. 🙂

Now, let’s quickly parse the player object so we can move on to the camera stuff:

There shouldn’t be any difference visually when you build, — we just have a player object which we can control.

Now let’s add some player input in the update  function:

And if you build now, you should be able to control the pillar… er, player:

licecap-camera

Great! Now let’s move on to the main content of today’s tutorial — using the camera.

The reason why we took quite a few steps to setup the game is so that we have a visual reference, to know the camera functions are working. If you started with a simple empty black screen with a blue box in the middle, it would’ve been difficult to tell if the camera actually shook, or if the camera actually follows the player as you move.

Camera Shake

The simplest way to use the camera shake can be written as follows:

You can refer to the FlxCamera API on how to the function callback and whatnot. If you’re not sure how callbacks are written, try reading the Button events & callback tutorial here.

Notice how when the screen shakes, there’s a visible black area around the screen’s edges:

licecap-camera-2

From a quick search, this page reveals a simple solution. Just add the following lines in your create  function to extend the camera size:

Note that the ext  value is arbitrary, as long as the camera shake’s intensity doesn’t go beyond the extended range.

And now there won’t be any visible black area when the screen shakes:

licecap-camera-3

Camera Flash/Fade/Fill

Camera flash can be used for situations such as lightning flash, or explosions. Let’s just modify the existing camera shake test buttons like this:

Now you should get this when you test:

licecap-camera-4

The camera’s fade  and fill  functions are similar to flash .

fade  will gradually fill the screen with a color — the opposite of flash.   fill  instantly fills the screen with a certain colour. These two functions can be used hand-in-hand for situations like fading in or out between screens. You can check out the FlxCamera API for more info.

Camera Follow/Lerp/Bounds

To get the camera to follow something (in this case, the player), you can just write it like this:

And here’s the result when you move around:

licecap-camera-5

At first glance, there are three problems:

  1. The camera scroll is too rigid (no deadzone).
  2. The camera movement is not smooth (no lerping).
  3. The camera can move out of bounds (shows the black area).

Let’s go fix each of the problems above.

1) To fix the rigid scroll issue, you need to add “deadzones” in the camera (an area within the screen that won’t scroll the camera). HaxeFlixel has a few presets which can be used in the  follow  function, as indicated in the API page. Here’s an example usage, by modifying the existing code:

Note that we added the preset STYLE_TOPDOWN and now it’ll automatically follow the player, with a little deadzone, as demonstrated below:

licecap-camera-6

2) To add smooth camera scrolling (lerping the movement), you can just add additional values to the follow  function, like this (refer to the API):

And now the camera will lag a little when the player moves:

licecap-camera-7

3) To prevent the camera from moving out of bounds, you just need to set the bounds of the map, like this:

And this will give you the result:

licecap-camera-8

There’s a lot of other things you can experiment with in FlxCamera, so go ahead and try them all out! The information above should be enough to give you a rough idea of what’s possible with the FlxCamera — they’re very handy in prototyping games.

This concludes today’s tutorial on HaxeFlixel’s camera. In the next post, we’ll try something more advanced with the camera — split screens and parallax scrolling.

HaxeFlixel Tutorial – Custom App Icon

This is supposed to be an easy step, but I fumbled for an hour figuring this out. So here’s a tutorial to help those who may have had difficulty like me — to add a custom icon to a HaxeFlixel build.

According to a HaxeFlixel page here, we learn that we can include the icon in the Project.xml file. Whenever you create a new HaxeFlixel project, there is this line at the bottom of the Project.xml file:

What can we add here? According to the OpenFL XML format page, the syntax can be as follows:

It wasn’t mentioned what exactly “path” means. Anyway, I figured it out after doing some searching, and found this Google forum thread. Read on for a summary of what I learned.

The path="icon.png"  above implies you can put your icon file in your root folder (where your Project.xml is), but in other cases we could put them in another folder such as /assets/images  or /assets/icons .

If your icon file is in the /assets/images  folder, then the simplest code would be as follows:

Now when you build your game with lime test neko , you should see that your application has the icon — by checking the /export/<target>/neko/bin  folder (in the example below, I used the flixel.svg icon):

Screen Shot 2015-04-01 at 10.19.36 PM

But what if the icon is still showing the default HaxeFlixel logo? Referring back to the Google forum thread link mentioned above, you may need to try the following:

1) Removing the icon in the Flixel XML

Locate the include.xml in the flixel folder, then comment or remove this line:  <icon path="assets/images/logo/HaxeFlixel.svg" /> . For OSX, the path would default to “/usr/lib/haxe/lib/flixel/3,3,6”. I’m not familiar with the path for Windows and Linux, but I hope it doesn’t take too long for you to find it. 🙂

2) Clearing the cache

Delete your HaxeFlixel project’s /export  folder and rebuild your game (i.e.  lime test neko ). The app icon does not get regenerated after you build your game for the first time, so you have to clear the cache for the icon to be updated.

3) Check your SVG icon

I was using this flixel svg icon as a test, and somehow the icon didn’t appear correctly when I built the game. I had to open the SVG file with SublimeText, and change the data a little, from this:

… to this:

In a nutshell, the width and height needs to match the viewBox property. Otherwise, the icon will appear weird, e.g. as a tiny image instead of the full icon.

Also, the width and height needs to be the same, otherwise the resulting icon will appear stretched.

This concludes the tutorial on including a custom icon into your HaxeFlixel game.