Saturday, September 12, 2009

AS3... a fresh start

I never wanted to migrate to AS3. It's such a spin from my comfy chair in AS2-land that I refused all coersion to migrate. That is, until I started another isometric project. For anyone who doesn't already know, I'm the king of procrastination. My flash projects folder outnumbers my completed games folder almost four to one. Typically, I get a good idea in my head and start coding it, but I always hit a wall when it comes time to do graphics and sound effects because I am novice at best in both categories.

This isometric game plan has been nagging at me for almost two years. I've got bits and pieces of code scatted throughout half a dozen notebooks in my desk, but I always knew I couldn't polish the game because I don't have the graphic hand to make the game look nice.When I started this project last weekend, I started it in AS2. Within hours I haid laid out a basic system for map generation, and I could grind out a nice map (not including player interaction or interactable objects; just tiles) in a few hours.

I started doing the math and wondered how far I could push the limits of AS2 knowing it had it's limitations. I found out that with an AS2 isometric engine, I could -barely- push between 1200-1500 tiles on a single map. Damnit, another wall: performance. Against all the will I could muster, I started looking into AS3...

My first test with AS3 was the language. It's completely different than AS2. Adding a movie clip to the stage used to take me ten seconds in AS2; in AS3 the first sprite to display on the stage took me thirty-five minutes. Flash forward to now though, and I can't BELIEVE I didn't migrate sooner. I can now pushing 1800 tiles to the screen and still maintain between 40-50FPS (down from 61 stage framerate). That kind of power was overwhelming! Not only did I find myself giggling like a little girl, but I vowed to never, EVER write another application in AS2.

So, after I got the new engine laid out, I took to writing a tile system. Nothing fancy, but I wanted something dynamic and simple to generate a tile from a single number. In AS2 we used to a gotoAndStop() engine, but that wasn't going to cut it for me because I wanted to be able to have a different look to each room from a spritesheet. Furthermore, that meant that I was going to have to break a tile down into each "piece" or "face". So now what do I do? You guessed it: Base 2. Base 2 allowed me to set each face to a specific number, rangine from 1-512:

  • 1 - Bottom
  • 2 - North West Wall
  • 4 - North East Wall
  • 8 - South East Wall
  • 16 - South West Wall
  • 32 - North West Ramp
  • 64 - North East Ramp
  • 128 - South West Ramp
  • 256 - South East Ramp
  • 512 - Top
Using this, I worked with Chris from Flashkit and developed a system to determine what base 2 numbers went into any specific combination. With those numbers, using each side only once, you can generate a number anywhere from 1-1023. So my TileMaker class takes in the number 527, and kicks me back 512, 8, 4, 2, and 1. I translate these numbers back into single digit numbers from 0-9 and pull an object from an array that houses all the data I need to create a new child Sprite of the tile with correct depth, matrix, and coordinates.

From there it was only a matter of piecing it all together into a simple map editor that you can see here. All the controls are found using the Help button at the top. To erase a tile, uncheck all the face check boxes in the right panel. Also, don't press Render Map twice... kind of a last minute bug I forgot to remove. If you press it twice without closing the first render window, you will be unable to close the bottom window and will be forced to refresh your browser.

3 comments:

  1. I had the same experience when I switched to AS3. Pretty nifty idea with the numbering system. Reminds me of configuring the adress on old intelligent lighting fixtures.

    One suggestion, if I may: could you add a additive and a subtractive option? By that I mean that when I select a wall only the wall gets added/removed instead of the whole tile bieng replaced by a wall. Don't know if that was already in your designdoc.

    ReplyDelete
  2. As I never intended it to be a widely used public tool, I'm not sure how useful the option would be. One thing I'm still debating is whether or not I need to add walls on the inside of a bounding room. What I mean is, if you place a full "block", would it be necessary to add a northwest wall in the tile to the southeast of the block?

    Thanks for the feedback on the numbering system. I originally started using power of two back when Trogdor released Minotaur. "Bit-counting" as it has come to be known, was at the core of how he produced random mazes.

    Unfortunately, the side project has taken me entirely too long. My university homework has seriously slowed my progress on both projects with C++ garbage, but I hope to be back on RC by the end of October.

    ReplyDelete
  3. I like your editor! How's the projects progress? :)

    ReplyDelete