HaxeFlixel Tutorial – Sliders

Today we’ll be covering some basics on FlxSlider. Sliders are commonly used in options menu, e.g. music volume.

For the purpose of simplicity, let’s proceed with this goal — a slider that can adjust the x/y position of a player object.

Note: It seems the FlxSlider only has a horizontal bar, no vertical version.

Setup

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

Creating The Player

Let’s create a player object in the default starting class for HaxeFlixel projects — the MenuState.hx:

You should see this when you build with lime test neko :

Screen Shot 2015-03-27 at 5.58.10 PM

Creating The Slider

The slider is not part of the basic flixel package — we need to use the flixel-addons haxelib. Go to the Project.xml file and uncomment the line as shown below:

Now that the haxelib is included, you can use FlxSlider. In the MenuState.hx, let’s add the following code:

And here’s the output:

Screen Shot 2015-03-27 at 6.29.37 PM

It’s a little ugly, but it works out of the box. We’ll polish the aesthetics later.

Referring to the new() function in the API page, here’s an illustration for quick reference:

slider-tutorial-ss1

Note — the Thickness parameter affects both the handle AND the bar’s thickness.

Improving The Slider

Now, how do we improve the slider? Let’s list down a few things we may want to do:

  • Have an image for the handle
  • Have an image for the bar
  • Replace the labels with custom font and text
  • Play sound on event (hover, click)
  • Trigger a callback on value change, for whatever reason

That’s a handful of things, so let’s tackle them one by one.

Setup (again)

Before we proceed, let’s download some placeholder resources. Let’s use Kenney’s UI Pack — it contains some sounds, fonts and graphics in one package.

For graphics, put these in the /assets/images  folder: blue_sliderDown.png and blue_panel.png.

For the audio, put these in the /assets/sounds  folder: rollover1.ogg and switch3.ogg.

For the font, put this in the /assets/data  folder: kenvector_future.ttf.

Code Cleanup

Let’s clean up the code a little — adhering to the DRY principle:

And here’s the updated output:

Screen Shot 2015-03-27 at 10.33.39 PM

Updating The Slider Body

The body  property is a FlxSprite. That means we can modify the image by using loadGraphic , or create a complicated Sprite image and load it into the body. In our case, the default FlxSprite does not have 9-slice capabilities. But wait! We can create a 9-slice image, then load that image into the slider’s body. Awesome hack, huh? Let’s try that now:

And here’s the output:

Screen Shot 2015-03-27 at 10.41.36 PM

Updating The Slider Handle

We can do the same thing (loading a 9-sliced image) for the handle, but it’s unnecessary in this case. So let’s do it in a simpler way, and update the code as follows:

And this is how it looks:

Screen Shot 2015-03-27 at 10.45.38 PM

Updating The Text

We can easily format the slider’s text like this:

Now, the slider looks a lot better already, with the updated text and font:

Screen Shot 2015-03-27 at 10.57.21 PM

Adding Sound

There are only two events which you can attach a sound to — the hover and click events. So, since it’s there, we might as well make the best of it:

I can’t demonstrate the sound through image or GIF, but you should be able to hear the sound when you test the game.

Event Callback

Finally, we most probably need to do something when a value changes on the slider. Perhaps, for example, when the player’s x-position is updated, we play the player’s “victory” animation. To add an event, just add a function to the callback property, like this:

And this concludes the slider tutorial.

In case you wonder why I am doing a lengthy tutorial on HaxeFlixel slider — it’s because the FlxSlider is the very first thing I attempted to learn in HaxeFlixel, and considering my lack of Haxe/HaxeFlixel knowledge back then, I couldn’t even figure it out. Now that I am more familiar with it, I figure I’d write a tutorial on it to test my understanding. I guess I’ve learned a lot! 🙂

HaxeFlixel Tutorial – Creating A Simple Text

Today we’re going to create simple texts in HaxeFlixel, using FlxText.

Setup

Let’s begin with a template HaxeFlixel project by using the following command:

As usual, we will begin coding from MenuState.hx file.

Simple Text

Now let’s add a simple text onto the screen. You can refer to the post here (check the links mentioned) or here (FlxText section) on how to add text, but in case you’re lazy, here’s the code:

If you build using lime test neko , the output would look like this:

Screen Shot 2015-03-26 at 10.30.25 AM

Text with custom font

Here’s how to create a text with custom font. We assume your custom font is named “kenvector_future.ttf” and you have put it in your HaxeFlixel project’s assets/data/  folder. You just need to create a new FlxText, and use setFormat()  to update its properties.

And now the output would look like this:

Screen Shot 2015-03-26 at 3.02.04 PM

Note that you can use setFormat  to modify other properties too — check the API for more info.

To further demonstrate how else you can further modify the properties using setFormat , here’s an example:

And here’s the output:

Screen Shot 2015-03-26 at 3.11.23 PM

Text Filters

You can also add filters to texts — check out the possible filters at the API page. The problem a beginner would have with the API is — there isn’t any explanation on how to use it. So, here’s an example:

And here is the result:

Screen Shot 2015-03-26 at 3.29.08 PM

I believe with the setFormat  and addFilter  functions above, you will be able to create texts that suit most of your needs. This concludes the tutorial on creating simple text in HaxeFlixel.

Haxe Tutorial – Upgrading/Downgrading Haxelibs

This isn’t really HaxeFlixel related, but I encountered an issue when I recently upgraded my haxelibs.

Haxelib is a collection of haxe (.hx) files that expand on existing Haxe language to make life easier for the developer. OpenFL, a game framework, is a haxelib. HaxeFlixel is also a haxelib that expands on OpenFL.

The issue I faced was — I upgraded my OpenFL version from 2.2.8 to 3.0.0-beta. The framework apparently has been overhauled, and as a result, broke HaxeFlixel’s API. In fact, I couldn’t even get an empty HaxeFlixel template project to compile.

So here’s a note for awareness:

Use this command to upgrade all your existing libraries to the latest version:

Use this command to check the library info (which mentions the library versions):

Use this command to upgrade/downgrade your library:

As such, I had to downgrade my OpenFL version to 2.2.8 in order for HaxeFlixel to work again. So, if you ever encounter a problem with compiling your code even though it was never modified, check if the haxelib upgrades broke anything. 🙂

EDIT:

Ashiq brought up an idea in the comments, which I overlooked — you can set your haxelib versions for your individual projects. This would help ensure your older projects do not risk getting broken by future haxelib upgrades.

As shown in the OpenFL XML format page, you just have to go to your Project.xml file and add a “version” property to your include code, something like this:

 

HaxeFlixel Tutorial – 9-slice Images and Buttons

To handle 9-slice images and buttons, we will have to look into the  flixel.addons.ui  package, where we find FlxUI9SliceSprite (API). Thankfully, there is the flixel-ui Github page which includes a very lengthy readme of how to use the UI addons:

https://github.com/HaxeFlixel/flixel-ui

Unfortunately, it still takes some effort for beginners like myself to figure out how to use it, because the information focuses on using XML to generate and format the UI components. As such, we’re going to proceed with the tutorial below using only code, to illustrate usage without XML.

Setup

Let’s create a new template HaxeFlixel project for this tutorial:

Now let’s find some placeholder images. We’ll be using this image, taken from Kenney’s UI pack (visit the website www.kenney.ul for more awesome stuff, btw!) — “blue_button07.png“:

blue_button07Right-click the image above, and save it into your HaxeFlixel project’s  /assets/images  folder. Now we can begin coding.

9-slice Images

In case you’re not sure how 9-slice works, here’s an image to demonstrate:

tut-9slice-ss1In a nutshell, 9-slice images preserve the resolution of the borders. It’s most commonly used in dialogue boxes and button images.

Before we can use the  flixel.addons.ui.FlxUI9SliceSprite  class, we will need to include the haxelib. Go to your Project.xml and enable the following code:

Now you can start using it. Here’s how we add a simple 9-slice image:

In case you’re wondering how the  _slice  works, here’s an illustration:

tut-9slice-ss2

This should be the result when you build with lime test neko :

Screen Shot 2015-03-21 at 10.45.35 AM

9-slice Buttons

If you look at the FlxUITypedButton API page, the descriptions are not helpful in telling you how to use the 9-slicing. However, if you look into the code (FlxUITypedButton.hx on Github), you will discover that there are in fact, annotated comments for the function.

Before we proceed, let’s add the following images into the  /assets/images  folder:

blue_button08blue_button08.png

green_button07green_button07.png

sheet_buttonsheet_button.png

Now let’s update the code like this:

The code above demonstrates 3 parts:

  1. Default 9-sliced button (with default skin)
  2. 9-sliced button using 3 separate images as the 3 states (up, hover, down)
  3. 9-sliced button using one image spritesheet

It may take a while to understand what the function parameters mean — in fact, I recommend you check the Github FlxUITypedButton.hx page, and look for the  loadGraphicSlice9  function’s comments and read the parameter descriptions.

However, if you want a rundown of what to take note of, here’s some points:

  • When loading separate 3 separate images for the button, make sure  _graphicArray  and  _sliceArray  are of equal length. For buttons, the length must be 3, and for toggle buttons, the length is 6. (We’ll talk about toggle buttons some other day!)
  • When loading one spritesheet for the button, make sure to specify the original image size. In the code above, it’s the last two values  49, 49  part in Line 51.
  • When loading one spritesheet for the button, you can either specify just one slice array data, or 3 slice array data. I.e. Line 51, the  _sliceArray  value can also be written as  [_slice]  or [_slice,_slice,_slice] .

Here is a screenshot of how the screen should look like in the end, after adding the three 9-slice button code above:

Screen Shot 2015-03-22 at 4.11.03 AM

And this is a GIF of the buttons in action, to further illustrate the button’s states (up, hover, down):

licecap-button-2

This concludes the tutorial for 9-slice images and buttons. I hope it wasn’t too confusing — It took me a while to figure it out, and it took me even longer to find a way to write the tutorial in an understandable way.

In the next post, we move on to another most commonly used component — Text in HaxeFlixel.

HaxeFlixel Tutorial – Button Events, Callbacks

In the Simple Button tutorial, we covered how to create a simple default button (which comes with text). If you check the API, you’ll notice that FlxButton is extended from FlxTypedButton, which does not include text. This means, we can actually use the functions in FlxTypedButton from FlxButton.

One thing that stumped me as a beginner was that the API did not explain how to use the callback events; the onUp, onDown, onOver and onOut variables are exposed, but there was no example on how to use them. I tried these, but it didn’t work:

It turns out, after some Googling, the answer here, in the upgrade guide page, that explains that you have to set the callbacks within those variables:

Note: the OnButtonUp will replace the OnClickButton callback that was set in the new FlxButton(...)  — they use the same onUp event handler.

Okay, we now know how to add callback to the button events. What if you want to trigger the callbacks with arguments? According to the upgrade guide page, you can do this (Let’s reduce the code to just one onDown event for simplicity sake):

It sounds straight-forward, but … let’s say we have 3 buttons, and we want to know which button the player clicked on. How do we code that? There are two ways I could think of.

The first method is just to add and bind simple values for each button’s callback; In the case below, we use the strings “button 1”, “button 2”, and “button 3”.

However, what if, for each button we click, we want to trigger something else within that button? We can pass the button itself as the value:

Pretty cool, huh?

Note: There’s something quirky about the button event behaviour — the onDown event triggers even when you hold your mouse button down and hover over/hover out of the button. This also happens for onUp event. If you want the onDown/onUp to trigger only if the mouse is over the button, you can set allowSwiping to false.

One last thing — what about sound? e.g. Play a sound when the mouse hovers over and out of a button? Check the upgrade guide again, I think the example usage is straight forward. However, just in case you need an example, here is one, based on the code we’ve written above so far (assuming you have the audio file in the /assets/sounds  folder):

I think that about sums up all there is to know about button handling. In the next post, I’ll attempt to cover 9-slice images, and if possible, 9-slice buttons.