Skip to content

Resource Handling in Flash – Part Two

In the last post I showed two approaches to access resources such as sound or image data in Flash. To make resources available to your application you can either load resources one-by-one on demand or embed them all into the main SWF file so they are available as soon as the application has loaded. Both variants however have limitations. So in this post I’m going to show some alternatives.

Read more…

Resource Handling in Flash – Part One

It’s easy to pickup ActionScript3 with a programming background in C++,/C# or Java as the languages shares the same concepts and similar syntax. So, why learn about stages and timelines, keyframes and whatnot when you can write your games like you always did? Well… almost. There are concepts in Flash that require some getting used to. Resources is one of them.

There are two fundamental different ways to get your resources loaded.

One is to embedd all the potentially needed data into your SWF file. This increases the initial loading time but once the application has fully loaded all resources are instantly available, too.

The second option is to send a lean application and load individual resources on demand. This is will reduce the initial load but making things less convenient for the coder and user.

Read more…

Flash TMX Parser

I’ve created all the key gameplay elements for Rune Hunt and the next step is to build levels to see if they can be used to create fun gameplay. I didn’t want to write my own tools so I searched for a map editor flexible and feature complete enough to meet my requirements. The editor that impressed me the most was the Tiled Map Editor that has comfy features (including Undo!) and exports an xml based level format.

For people interested in using Tiled for their flash game but unsure how to handle the level format I’ve put together a little example. It contains code that parses a tmx file and recreates the data on the flash side in form of custom classes designed to make all the data easily accessible. The code is generic enough to be used in about any AS3 based project. I support XML, CSV, base64 and zlib-compressed base64 encoding.

In my sample I modified a Flixel Framework v2.23 based demo (the original version called FlxCollision is available on githup) to reload the current game state out of a TMX file on keypress. If you run the editor and the swf simultanously you can make changes in the editor, save, switch to the swf, press the Enter key and see the changes taking effect.

Tiled Editor and Demo App

If you’re interested it’s easy to give it a try. No source compiling is required. Just get the files and unzip them somewhere. Then execute the FlxCollisions.swf file found in bin-debug by double clicking it and load the level map01.tmx found in bin-debug/data in the map editor. No restarting of the flash app should be required. Just trigger the reload ingame whenever you changed something!

If you want to use the same approach for your own project all relevant code is included. The interesting parts are found in the PlayStateTmx class where the TMX file is loaded and parsed and then used to populate the FlxState with level content matching the tiles and objects described in the level file. Have fun! 🙂

Game Preview – Rune Hunt

Rune Hunt Preview

When I discovered Flixel some months ago I was impressed by the wide range of of quality flash games that had been been developed and released based on it. I gave it a closer look, liked it and started to play around with it. I’ve not had a personal game project for years and had almost forgotten how much fun it is: Working on a project where I have complete creative control, no time constraints and where a broad range of skills are getting challenged. Skills like doing graphics and sound effects, thinking about gamedesign and story – skills that in my day job as a programmer are done by others.

Progress on my game project has been slow (I only work on it a couple of hours a week) but steady and I’m at a point where I’m quite confident that it’s eventually getting released and playable for a larger audience. So why not preview it on my blog?

When I posted the first tech demo of Rune Hunt (a working title that will probably stick) in the Flixel forums I described it like this:

The setup is classic: a hero in a dungeon full of treasure, traps and NPC of questionable attitude. The twist is that I’m limiting the player’s view to what his avatar is seeing. Top-down-ego-perspective so to speak. I hope it’s making exploring the dungeon more interesting and the enemies more scary if you don’t see all the stuff long before it becomes relevant/dangerous.

This is still more or less accurate. The opportunity to take all kind of ideas and concepts that I come up with (about algorithms, about gameplay mechanics, about game design and narrative) and just try them in-game is something I fully embrace and thus I’m really not sure where it all ends. So I won’t linger on describing story or game mechanics – you’ll see it when its done.

“When it’s done” is a pretty lazy approach to project management. It’s also coming with a high risk of never beeing done at all – just think about Duke Nukem Forever. Maybe I could create a more fun game when I’d take a different development approach, when I’d stick closer to what has worked before and apply proven game design practices instead of following a vision that is constantly in flux. But I wouldn’t have more fun creating it that way. And maybe players will like it in the end – despite the lack of standard feedback loops (like competing for highscores or leveling your character or mastering the mechanics to beat the game) that I left out because I am personally bored of them. Maybe others can appreciate what’s there instead: stuff that I considered worth trying out and then good enough to keep it in. I’m curious to see if playing the game will be as much fun as creating it was!

But if not I’m still having plans to extract some value out of it. But that’s another story…

Retro 2.0

Video games, when created by individuals, often fall into the category retro games: A genre that imitates the style and design of games authored in the early decades of video gaming.

It’s a smart pick. If the hobbyist would try to compete with todays teams of specialists that transform millions of cash into AAA titles he were doomed to fail. But two decades ago team size or money weren’t the limiting factors – the constraints were of technical nature. Back then a talented individual with a good idea could create a state-of-the art game. And thanks to better tools, better learning resources and faster computers all these games are perfectly doable now for anyone willing to learn how. Pick this niche and suddenly you’re on a level with the competition.

Despite all the limitations the old games were fun and often still are. Fun has nothing to do with photorealism. Reading a Book can be a compelling experience – because the story is unfolding in the mind of the reader. When a couple dozen abstract black symbols used in alternation can achieve that how could eye-candy and sourround sound be a critical factor in games?

At the core of any good game is the gameplay and/or narration. And to get that right you don’t need a lot of resources. Quite the opposite: The games industrie is often critizised for its lack of innovation. And when a multi-million dollar budget is at stakes you really can’t make experiements. When you have “asset pipelines” and the likes it’s hard to establish a shared vision of what exactly you want.

I think it’s still possible for individuals or a small groups to create games that matter. Thinking “retro” might help. But instead of recreating what has been done before rather copy the concept:

  • Focus on gameplay and narration
  • Build your game around a key idea
  • Think about efficient reuse of resources
  • Use the players imagination to fill the gaps

There are vantages to being independent. And there are possibilities far beyond those available decades ago. Contrasting this the indie movement in it’s current state is – with exceptions – still rather insignificant.

What I like about Flixel

I’m amazed how many flash games not only have the visual style but also the gameplay of the games of past decades. While there are good reason for pixelated graphics I’d really love to see a more variety in gameplay and presentation.

The technical limitations of the past are gone. Is there a reason not to have physics, particles, dynamic lighting, line of sight or shadows, AI driven enemys, complex simulations and cooler stuff that I can’t even think of in the games? No, not even when we’re talking about Flash games. Why we’re not seeing more of this stuff is because this technology is not made easily available to developers by the toolkit they’re working with.

A great example for the sleeping potential is the flash port of the Box2D physics engine that got released in September 2007. Even though it’s a pretty straight forward port that’s not optimized for flash at all a flood of games got released since, featuring completely new gameplay or enriching established genres with physics. The basis of a good game is a good choice of engine, tools and middleware.

A flash game engine on the rise (just my subjecitve impression) is Flixel. A game Fathom that the author of Flixel developed with it caught my attention: It’s not not only extremely polished but also had a surprising story-twist and unique elements like a fish swarm guiding your way, dynamic gameplay-relevant lighting and beautiful particle effects…

This speaks for the author’s talent but also for the engine. And based on how well Flixel is embraced by the community it’s apperently also easy to use unlike PBE a flash game engine I’ve been working with. I think “ease of use” is really a key factor for any tool or software targeting the hobbyist. Game development should be about asking what while most people seem to struggle with the how. I’d wish for game development to be as accessible as painting a picture or writing a story. In it’s current sate it’s a form of media that most can only consume not express themselves with. The crucial step of evolution would be to overcome this barrier. Everything that helps that goal has my sympathy and that it seems like a step in the right direction is what I like about Flixel.

Unpredictable Flash

In the last couple of months I’ve programmed a lot of AS3 code. And when there was performance critical code to write it often got frustrating. The problem is not that AS3 compiles into bytecode which is running on a virtual machine. It’s not the lack of hardware acceleration or threading. The problem is a lack of transparency – it’s often impossible to find the best approach without trying and comparing all the different possible options!

Let me give you an example: I have a source Bitmap object and want to draw it with a PixelBender shader in my target buffer.

var target:BitmapData;
var source:Bitmap;
...
source.blendShader = myShader;
target.draw(source, matrix, colorTransform, BlendMode.SHADER, clipRectangle);

Welcome to the first pitfall: The clipRectangle does work as promised but it doesn’t affect the performance. You can set it to a one pixel square and it still evaluates every pixel in the target buffer. Good to know? Yeah, but it’s not in the docs. Not sure that the people at Adobe even know about it.

So, if the area I need to update is small enough it’s more efficient to copy the relevant pixels into an intermediate buffer apply the shader on it and copy it back. copyPixels comes to mind, right? Yes, that will work. And we all know how fast that is. But besides copyPixels there’s also a getPixels and a getVector method that allows me to extract a rectangular portion of the source. How fast are these? Turns out that copyPixels is the winner, getVector is about 50% slower and getPixels which basically does the same thing is 500% slower then copyPixels. Dooh!

But why do I even mention alternatives to the copyPixels way when it’s fast and everyone that reads blogs knows that it’s fast? Because getVector is a valid alternative. Turns out there’s quite some overhead to setting up and executing a pixelbender Shader. And after some more testing it turn’s out that for my specific simple shader the fastest option to update small rectangles in the target buffer is to just extract that portion using getVector, then iterate over the contents of a target and a source vector, do the calculations in plain Actionscript and copy the result back using setVector.

It’s a matter of luck and stubbornness that the tech demo I’ve been working on was successfully pitched and not canned as “not doable in flash”. Writing bytecode directly can’t be any harder then that. 😛

clipRectangle