Newgrounds API with Haxeflixel

I was recently asked to do a tutorial on how to get the Newgrounds API, allowing you to use Newgrounds Ads, Medals, Leaderboards and other site special stuff, to work with HaxeFlixel using FlashDevelop. Since there isn’t much on the subject, I thought I’ll write a quick one up to get people started.

To integrate the Newgrounds API with Haxeflixel with Flash Develop, you first download the Newgrounds API SWC library file from Newgrounds. To do this you must get it through the Newgrounds project system, which may require some time and an upload of your draft project for approval. Once you are able to download it, you must select the Actionscript 3.0 platform and choose the SWC file (left). This file is then added to the root of your Flash Develop project (right).

Newgrounds API DownloadNewgrounds API project

Once added, go to the project.xml file and add the following two lines.

<haxedef name="as3_native" if="flash"/>
<haxeflag name="-swf-lib" value="NewgroundsAPI.swc" if="flash"/>

What these two lines do is define a preprocessor name “as3_native” to Haxe, so it will accept Actionscript3 files, and adds a flag to the NewgroundsAPI.swc file we added so it will be treated as a flash library, the if statements mean that these two project definitions will only be included when building to flash.

Once you have done these, you should be able to use the NewgroundsAPI in HaxeFlixel the same way one would use in any Actionscript3 project. To test if these steps have worked correctly, let’s include a flash ad in the custom preloader from the last tutorial I did.

Firstly, we need to import the functions we will need, which are in the newgrounds and newgrounds.components namespace:

import com.newgrounds.*;
import com.newgrounds.components.*;

Then in our create() function, we need to connect the API to Newgrounds, this will require the API ID and Encryption Key provided to you from Newgrounds in your project, under the API tools (see the screenshot from earlier to find where it is, I’ve covered mine up for security reasons). You pass these into the API as strings using the connect function:

API.connect(root, "apiIdGoesHere", "encryptionKeyGoesHere");

Then we check if the API is connected to Newgrounds, create an Ad at the centre of the screen, add it too the scene and increase the display time so we can see the ad.

if (API.isNewgrounds)
{
var ad:FlashAd = new FlashAd();
ad.x = (stageWidth / 2) - (ad.width/2);
ad.y = (stageWidth / 2.5) - (ad.height/2);
addChild(ad);
minDisplayTime = 8;
}

The build into Flash, and you should see the Ad like the picture below.

Gamepopper Custom Preloader

If you have any compilation issues, be sure to check that you added the haxedef and haxeflag tags to the project.xml file and you have the NewgroundsAPI.swc file added to the root of your project. If you can build the project but cannot see the ad, then you might not have entered the API ID or Encryption Keys right, as both are needed to connect the API, or you haven’t added the add to the scene using addChild.

But if you can build the project and see the ad, congratulations! You now have the Newgrounds API working in your HaxeFlixel project! If you want to use any other functions of the API, you can use Newgrounds’ Official Documentation as a guide, just be sure to import the Newgrounds API classes like the above on any classes that uses them. It’s also important to wrap any usage of the API functions with an #if flash preprocessor directive, as there might be issues when building to incompatible platforms.

#if flash
if (API.isNewgrounds)
API.unlockMedal("You made it through!");
#end

So there you have it, Newgrounds API with HaxeFlixel. These steps maybe similar for other Haxe based game libraries such as HaxePunk, as well as other IDEs such as Sublime2, assuming that these platforms are able to build to flash platforms.

Advertisements

4 thoughts on “Newgrounds API with Haxeflixel

  1. You are an angel with wings of light, Thank you very much !
    Great tutorial, its just what I was wondering in the internet for hours to find
    Really really thank you, you are the best

  2. Thanks for this tutorial, really helped out a lot! After reading this though, I do wonder if it would be possible to load (and play) a .swf animation in HaxeFlixel.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s