FLAKit, my media management helper library


(Jacob Albano) #1

My first introduction to Flash development was in a college class I took a few years ago, which used as3 in an “Introduction to programming” class. The assignments all had to be turned in as Flash Professional .fla files, but I had discovered FlashDevelop and naturally wanted to use that. :smile: Unfortunately, the asset pipeline is completely different between the two environments, so I created FLAKit, a library that allowed me to use the same code seamlessly across both, meaning that I could develop in FlashDevelop and still turn in a project that was able to run in Flash Professional.

Since then it’s become much more than that – the main feature being live asset reloading. Since assets are loaded at runtime instead of embedded during compilation, compile time is hugely reduced, and I can press a button to instantly flush the library and load all assets in again. This means I can edit a spritesheet, switch back to the game, reload the scene and instantly see the changes I’ve made to the graphics; or I can move an entity in Ogmo editor, refresh the game and see the changes immediately.

Needless to say, this has hugely increased my productivity and cut iteration times down to close to the bare minimum. Loading assets dynamically also allows interesting things like choosing randomly between all sounds in a folder for each footstep’s sound, or naming your Ogmo levels “level01”, “level02”, etc, and loading them sequentially no matter how many of them you add.

For deployment, FLAKit includes a tool that generates a class containing [embed] tags for every asset in the library folder and allows access to them with the same exact interface as when loading dynamically. On projects with hundreds of assets, it’s a huge time saver to not have to manually embed each individually.

I hope you find it useful!

Get the source on Bitbucket.


Point-and-click architecture
Java Heap Space strikes again
(rostok) #2

I have similar PHP script for creating AS code with embedded assets. And both your post and library encouraged me to upgrade it with support for external assets. This approach is really awesome! Builds take much less time now.


(Jacob Albano) #3

Cool, glad you found it helpful! Remember that URLLoaders aren’t allowed by default due to security reasons, so you’ll always have to embed the assets for release. Runtime loaded assets are really awesome while you’re in the middle of development, though.


(Abel Toy) #4

Wow. Can’t wait to check this out. I was thinking about writing a script that’d generate an embed class for me automatically, but this takes it to a whole new level. Thank you very much, dude!


(Jacob Albano) #5

Thank you! I’ll be pushing an update tomorrow that fixes a few things and makes the interface a little more user-friendly…a lot of the code is from my early days of as3 so some things don’t entirely make sense. I’ll also be adding a readme to the repository…probably should have done that before announcing it!.


(Jacob Albano) #6

I just pushed an update that should make it much easier to use the library for people who weren’t involved in its development. :wink: There’s also a readme file that should contain all the information necessary to start using the library.


(Enzo M) #7

Hi, I am kind of new with extern libraries etc. So, as I got memory problems for my project, I thought FLAKit would help me loading all my assets and allow me to put more of them, as sounds etc.

I added, in my main.as, the two “super” lines you told in the readme. But then I don’t really understand how to generate the library. I tried opening .exe’s but the console gives me an error.

Moreover, I got an “assets” class in my project where I put all the embed codes and const variables in static. Can I still use FLAKit to generate Library.image for example ?


(Jacob Albano) #8

First off, I want to make sure you know that FLAKit is designed as a development aid, and loading assets dynamically won’t work in a non-debug Flash player. While FLAKit does include a utility to create a class with embedded assets, if you’re having memory issues due to embeds it ultimately won’t help you since you’ll have to build in embedded mode for release.

With that out of the way…

The source code should be put in your main src folder, and your document class has to extend the FLAKit Engine class. Make sure you aren’t extending Flashpuunk’s Engine class by accident.

FLAKit assumes that your assets live in the “lib” folder of your project. You have to put the EXEs and the DLL in that folder, and run them whenever you add new assets. XMLLibraryBuilder.exe creates an XML file of asset names, and EmbeddedLibraryBuilder.exe creates an Actionscript class with embed tags for all your assets.

No matter which method you use to populate it, the Library class always uses the same interface to retrieve assets. The embedded assets can’t be accessed by name directly (as in Library.IMAGE1).


[SOLVED] Using string to load a new world?
(Enzo M) #9

Hi, so if I understand, I can’t build a definitiv version of my project with FLAKit, it is just to help me develop and during this process only ? Anyway, this is a nice knowledge and I sure will use it whenever I need, that seems 100% nicer. I understand the way to do it now, thank you :slight_smile:

Anyway, about the problem I thought I could resolve, I think I found another solution. I will progressivly add parts of my background while the character goes on. I think of creating a condition for in which I create bitmapdata in an array. When one background part isn’t on the screen, I will remove it.

The only problem is that I got 105 parts of my back, so at least 105 embed assets to declare :frowning: I am searching a faster way to do it actually.


(Jacob Albano) #10

More or less. The idea is to use dynamic asset loading during development to enable live reloading and help save on compile time, then switch to embedded assets for release. Is your Java heap space issue a problem at compile time or when you’re in the middle of your game? I’ve never gotten that error and I worked on a project once that had around 100mb worth of asset data.


(Enzo M) #11

It depends of what I was using. Sometimes it comes when I launch the program, using a background (collision map for a top-view game) which is pretty large IMO : 11k*11k, and sometimes my program simply stop when I launch the game after the menuscreen : I click on start and my program stop running. When this happens, my console shows something like 1G in memory used, which is a lot I guess.

For the first error, which java heap space, I changed JVM configuration and added xmx 1XXX to up his capacity. But for the second one, which is a simple stop of the program, I am still trying to search solutions, since I have to load 3 more images (another part of the collision map and 2 background maps).

That’s why I want to use bitmapdata images loading progressivly (105 images of 1024*768).


(Jacob Albano) #12

Yikes, that’s a lot of data! If memory usage is your underlying problem, FLAKit won’t be any help since it loads all assets ahead of time before the entry point is called, so all the assets are in memory no matter what.


(Jacob Albano) #13

I just pushed another update to the library which makes it a lot more consistent internally and simpler to use externally. It should also play a lot nicer with Flashpunk.

public class Main extends Sprite
{
    public static var library:Library;
    
    public function Main()
    {
        super();
        addEventListener(Event.ADDED_TO_STAGE, initLibrary);
    }
    
    public function initLibrary(e:Event):void
    {
        library = new Library("../lib", Library.DynamicMode, init);
    }
    
    public function init():void 
    {
        //    Entry point
        trace("initialized");
        addChild(library.getImage("path/to/image.png"));

        // reset this if you want; it'll run every time assets are reloaded
        library.onLoadComplete = ...;.
    }
}

(John Andersson) #14

This is effin amazing! Will try to implement this. I have a hard time understanding how to implement this though, and I did read the readme… I’m not sure how I should go about changing my game to use this (I’ve added a ton of sounds and images already) Do I only have change Main.as of my game to make all of this work?


(Jacob Albano) #15

In order to make this work with Flashpunk, you should only have to make a few minor changes…

public class Main extends Engine
{
    public static var library:Library;
    
    public function Main()
    {
        super(800, 600);
    }
    
    override public function init():void
    {
        //    normally you'd do your setup here
        //    in this case we'll defer it one more step
        library = new Library("../lib", Library.DynamicMode, startup);
    }
    

    public function startup():void 
    {
        //  Entry point
        
        FP.world = new MyWorld(); // etc
    }
}

In the lib/ folder in the FLAKit repository, grab LibraryBuilder.exe and put it in your assets folder. I usually use lib/, but you can use whatever you want. Make sure to pass the appropriate path to the Library constructor. You’ll need to run LibraryBuilder every time you add a new asset.

That’s all you strictly have to do, but here are a few handy tricks to speed you along.

In the past I’ve set up Flashdevelop to run it automatically when I compile, which you can do with a batch file like this:

@echo OFF
cd lib
LibraryBuilder.exe --xml . --as3 ../src

Save it as assets.bat and put it next to your project file (*.as3proj). Add assets.bat to your pre-build command line in Project -> Properties -> Build. Now whenever you compile, your library will be automatically rebuilt and the EmbeddedAssets.as file will be moved to your source folder.

To automatically refresh your assets when you switch away from your game and back again, add this to your main class:

override public function focusGained():void 
{
    super.focusGained();
    library.reload();
}

Your images and such won’t instantly update, visually, so you’ll have to reset the world you’re in. Set library.onLoadComplete to a function you want to be called when it finishes reloading your assets. To create a new instance of whatever your World class happens to be, use this:

var type:Class = Object(FP.world).constructor;
FP.world = new type(); // won't work if your world takes constructor parameters!

Finally, you can use Flashdevelop’s configuration constants to switch between embedded and dynamic assets depending on the build mode. Embedded assets can’t be live-reloaded, but they’re required if you plan to upload the swf anywhere, or run it outside a debug player.

CONFIG::debug {
    library = new Library("../lib", Library.DynamicMode, startup);
}

CONFIG::release {
    library = new Library("../lib", Library.EmbedMode, startup);
    library.loadEmbedded(new EmbeddedAssets());
}

That should cover everything you need to know. I should probably put some of this into the readme, hmm…


(Jacob Albano) #16

One final thing: Changing your Main class like this will not make your existing assets “just work” with FLAKit. You’ll need to go through and replace any code which deals with assets.

Instead of this:

new Image(EMBEDDED_IMAGE);

you need this:

new Image(Main.library.getImage("path/to/image.png").bitmapData);

Instead of this:

new Sfx(EMBEDDED_SOUND);

you need this:

new Sfx(Main.library.getImage("path/to/sound.mp3"));

(John Andersson) #17

Thank you, you beautiful man (no homo)