Log in
Please log in or register.
Pages: [1]
  Print  
Author Topic: Tutorial: Loading a level file from an image  (Read 4601 times)
failrate
Jr. Member
**
Posts: 52



View Profile WWW
« on: Wed, Jun 24, 2009 »

This tutorial is a work in progress.

In my PlayState base class (which inherits or extends FlxState), I import one extra library:
Code:
import flash.display.BitmapData;

Now I can work directly with the bitmapData of an embedded image.  I add one function that accepts a BitmapData object and builds blocks in the world.  It uses a crude form of RLE (Run-Length Encoding) to reduce the total number of blocks used.

Code:
public function loadLevelDataFromBitmapData(b:BitmapData):void
{
var w_offset:uint = 0;
var h_offset:uint = 0;
var rle:Array = [0, 0];
for (var h:int = 0; h < b.height; ++h)
{
for (var w:int = 0; w < b.width; ++w)
{
var val:uint = b.getPixel(w,h)
if ( val != rle[0] || w == b.width-1 )
{
switch( rle[0] )
{
case 0: // solid block
//FlxG.log('block: ' + [rle[0], w_offset, h_offset, rle[1]*8, 8] );
_blocks.add(this.add( new FlxBlock(w_offset, h_offset+1, rle[1]*8, 8, ImgTech)));
break;
                                case 10109983: // brown dirt
                                        _blocks.add(this.add( new FlxBlock(w_offset, h_offset+1, rle[1]*8, 8, ImgDirt)));
                                        break;
}
w_offset += rle[1]*8;
rle[0] = val;
rle[1] = 1;
}
else
{
rle[1] += 1;
}
}
rle[0] = 0;
rle[1] = 0;
w_offset = 0;
h_offset += 8;
}

FlxG.log("converted");
}

To generate the blocks for a level, I call the function in the level's initializer:
Code:
loadLevelDataFromBitmapData((new ImgLevel).bitmapData);
Here, ImgLevel is an embedded png.  I used The Gimp to create a png that's 80 x 80, with white meaning no blocks and black meaning a solid normal block.

Known Issues:
For some reason, 80 pixels in the image * 8 pixel height offset doesn't seem to add up to 640 high.
Logged
lechuckgl
Jr. Member
**
Posts: 77


View Profile
« Reply #1 on: Wed, Jun 24, 2009 »

Nice, but why from an image ? A XML is better ! Less size and more flexible !
Logged
failrate
Jr. Member
**
Posts: 52



View Profile WWW
« Reply #2 on: Wed, Jun 24, 2009 »

A) I'm not a bit fan of XML
B) Lets me draw a level in any paint program, so I don't need a separate editing program
C) They *are* small.  An 80x80 png can be just a few kb in size.  XML files can be that small or larger.
Logged
Adam Atomic
Administrator
Hero Member
*****
Posts: 724


hostest w/ the mostest


View Profile WWW
« Reply #3 on: Wed, Jun 24, 2009 »

ditto, using an image can be quite nice and compact Smiley
Logged

lechuckgl
Jr. Member
**
Posts: 77


View Profile
« Reply #4 on: Wed, Jun 24, 2009 »

You still have the issue of flexibility. Let's say you want to add objects to your map, bind a button to a door, or something like that.
Don't get me wrong, I like your idea...but I still think xml can offer much more than images. For simple levels your idea is perfect.
Logged
Adam Atomic
Administrator
Hero Member
*****
Posts: 724


hostest w/ the mostest


View Profile WWW
« Reply #5 on: Wed, Jun 24, 2009 »

yea the object layer or placement of non-tile objects can be tricky - you can colorcode pixels and things, buuut...i dunno.  tricky stuff Smiley
Logged

lechuckgl
Jr. Member
**
Posts: 77


View Profile
« Reply #6 on: Wed, Jun 24, 2009 »

Color coding pixels for different tile maps for each block, color coding pixel for spawn points , color coding pixels for objects, color coding pixel for triggers.....thats too much  !
And you still cannot bind a switch or button to a particular door...

That's why I say ( and I am working on it at this moment ): XML NEVER BEEN SO MUCH FUN  ( 1k points for the first person who says what game inspired that catchphrase !  Grin )
Logged
failrate
Jr. Member
**
Posts: 52



View Profile WWW
« Reply #7 on: Wed, Jun 24, 2009 »

Anything that is easy to do using the image I do with the image.
Anything that is easy to do directly with ActionScript, I write in ActionScript.
Between those two, I find myself not needing any XML.

Of course, lechuckgl, I did like your Tile tutorial.  I simply think that it is largely a matter of preference.  Thank you for your feedback.
Logged
Adam Atomic
Administrator
Hero Member
*****
Posts: 724


hostest w/ the mostest


View Profile WWW
« Reply #8 on: Wed, Jun 24, 2009 »

Anything that is easy to do using the image I do with the image.
Anything that is easy to do directly with ActionScript, I write in ActionScript.

Ditto, but yea definitely personal preference.  And for end user modification, XML is pretty much the tits; can't be asking users to muck about in your AS!
Logged

pearTree
Newbie
*
Posts: 4


View Profile
« Reply #9 on: Thu, Jun 25, 2009 »

Since flash gives you access to read and write pixels of bitmaps, it should be possible to make a level editor that outputs a bitmap. Just decide what colour represents what entity, have a palette on the side, option for setting width and height, text field to put in the level name, a 2D array displayed as a large grid, and a big "SAVE" button to output the bitmap.

Shouldn't be too much trouble for an accomplished actionScripter (not me).
Logged
lechuckgl
Jr. Member
**
Posts: 77


View Profile
« Reply #10 on: Thu, Jun 25, 2009 »

I am not arguing that ! What I am saying is that an image is not flexible enough. With XML you can easely have overlaped blocks ( if you want 2 or more layers ), entities related to each other, trigger zones, text for dialogs....evering you will ever need for a level. If you are using an image, sooner or later you'll find out that you will be needing additional files to make make a fully customizable complex level....but, as I said before, if you only want a simple level, this may be the best solution.

Offtopic: I can't belive no one knows what game inspired the phrase "xml never been so much fun" Tongue
Logged
Spaulding
Newbie
*
Posts: 8


View Profile
« Reply #11 on: Thu, Jun 25, 2009 »

I put together a very similar image-to-tile processing class with RLE support. I agree that it's gonna be easier to draw a maze or cave or something in a paint program than to make a multidimensional array in XML. But it's true, some games will have dialogue and such, which would certainly be better served by XML. So...the XML has all that stuff and the URL path for the tile layout bitmap(s).
Logged
capsuley
Newbie
*
Posts: 3



View Profile
« Reply #12 on: Fri, Jun 26, 2009 »

Might not be a bad idea to combine both methods until a proper level editor is created. Generate the majority of the level's layout using a bitmap and then create a very small tool to gen the xml from the bitmap. From there it would be quite easy to add the complexity that the bitmap alone does not afford you while still being able to visually create the level layout.
Logged
lechuckgl
Jr. Member
**
Posts: 77


View Profile
« Reply #13 on: Fri, Jun 26, 2009 »

Might not be a bad idea to combine both methods until a proper level editor is created. Generate the majority of the level's layout using a bitmap and then create a very small tool to gen the xml from the bitmap. From there it would be quite easy to add the complexity that the bitmap alone does not afford you while still being able to visually create the level layout.

Well, that what I am aiming to...sort of. I am developing a level maker that will allow you to draw blocks ( http://flixel.org/forums/index.php?topic=112.0 ) and place them anywhere. In fact, I've already finished the XML-to-Flixel parser and I am creating levels from a XML file...now I just haveto develop a tool to generate those XML files Smiley

I still think images are not flexible enough, so combining technics is, in my opinion, a bad idea.
Logged
Hooka
Newbie
*
Posts: 1


View Profile
« Reply #14 on: Sat, Sep 19, 2009 »

War has never been so much fun! - Cannon Fodder theme song Wink

Anyways, just felt that was missing... and been enjoying the engine and tutorials so far, just gonna take me a bit to get my mind around all the intricacies!
Logged
Pages: [1]
  Print  
 
Jump to: