Log in
Please log in or register.
Pages: [1]
  Print  
Author Topic: Deliberate tiles  (Read 3503 times)
ben
Newbie
*
Posts: 11



View Profile
« on: Mon, Jun 8, 2009 »

FlxBlock always chooses a random graphic from your TileGraphic:Class, which isn't right if you want to design your own levels in a way that's more involved than simply defining areas of certain blocks.  So I added a parameter to the FlxBlock constructor, TileID, that allows you to select which piece of the image you want to use:

Code:
//@desc Constructor
//@param X The X position of the block
//@param Y The Y position of the block
//@param Width The width of the block
//@param Height The height of the block
//@param TileGraphic The graphic class that contains the tiles that should fill this block
//@param Empties The number of "empty" tiles to add to the auto-fill algorithm (e.g. 8 tiles + 4 empties = 1/3 of block will be open holes)
//@param TileID Which number tile to use (negative results in random)
public function FlxBlock(X:int, Y:int, Width:uint, Height:uint, TileGraphic:Class, Empties:uint = 0, TileID:int=-1)
{
    super();
    x = X;
    y = Y;
    width = Width;
    height = Height;
    if(TileGraphic == null)
        return;

    _pixels = FlxG.addBitmap(TileGraphic);
    _rects = new FlxArray();
    _p = new Point();
    _tileSize = _pixels.height;
    var widthInTiles:uint = Math.ceil(width/_tileSize);
    var heightInTiles:uint = Math.ceil(height/_tileSize);
    width = widthInTiles*_tileSize;
    height = heightInTiles*_tileSize;
    var numTiles:uint = widthInTiles*heightInTiles;
    var numGraphics:uint = _pixels.width/_tileSize;
    for(var i:uint = 0; i < numTiles; i++)
    {
        if((Math.random()*(numGraphics+Empties) > Empties) && (TileID<=-1)) {
            _rects.push(new Rectangle(_tileSize * Math.floor(Math.random() * numGraphics), 0, _tileSize, _tileSize));
        } else if ((Math.random() * (numGraphics + Empties) > Empties) && (TileID > -1)) {
            _rects.push(new Rectangle(_tileSize * TileID, 0, _tileSize, _tileSize));
        } else {
            _rects.push(null);
        }
    }
}

The original case is preserved so you can probably just replace the constructor in FlxBlock.as if you want to and it shouldn't hurt anything.
Logged
sharksharkshark
Newbie
*
Posts: 11


View Profile
« Reply #1 on: Mon, Jun 8, 2009 »

I was just reading the documentation to find a solution to this issue.

Thanks for this post. You saved me some time Smiley.
Logged
prettymuchbryce
Guest
« Reply #2 on: Wed, Jun 10, 2009 »

Thanks I was confused about this too.
Logged
prettymuchbryce
Guest
« Reply #3 on: Wed, Jun 10, 2009 »

Sorry to double post

but is it impossible to have tiles with transparency? Can blocks only be complete squares?
Logged
Adam Atomic
Administrator
Hero Member
*****
Posts: 724


hostest w/ the mostest


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

You can use graphics with transparency and you can use the Empties parameter to add "blank" tiles into the mix
Logged

sharksharkshark
Newbie
*
Posts: 11


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

Adam, is there an example of using the Empties parameter? The explanation in the docs is a bit confusing.
Logged
Adam Atomic
Administrator
Hero Member
*****
Posts: 724


hostest w/ the mostest


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

Sorry about that - I meant to include an example of it in Mode this time and forgot.  Basically, pretend you have a tileset to use on a FlxBlock that has 4 tiles in it.  When the FlxBlock is created, each tile that populates the block is chosen at random from those 4 tiles.  The "Empties" parameter allows you to add virtual, transparent tiles to your tileset to get mixed in.  So if you have say 4 tiles, you can set Empties to 4, and about half of the tiles will just be transparent holes, rather than actual tiles.  It's kind of confusing but helpful for specific effects, especially backgrounds (like the background of the labyrinth in Fathom)
Logged

sharksharkshark
Newbie
*
Posts: 11


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

Sorry about that - I meant to include an example of it in Mode this time and forgot.  Basically, pretend you have a tileset to use on a FlxBlock that has 4 tiles in it.  When the FlxBlock is created, each tile that populates the block is chosen at random from those 4 tiles.  The "Empties" parameter allows you to add virtual, transparent tiles to your tileset to get mixed in.  So if you have say 4 tiles, you can set Empties to 4, and about half of the tiles will just be transparent holes, rather than actual tiles.  It's kind of confusing but helpful for specific effects, especially backgrounds (like the background of the labyrinth in Fathom)

So an Empty tile still acts as an FlxBlock, meaning it's like an invisible wall? Or are these basically dead and collisions won't be checked against an Empty tile?
Logged
lechuckgl
Jr. Member
**
Posts: 77


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

You could improve this by setting tile_width and tile_height throguh the constructor, so the image containing the tiles doesn't need to be a stripe ! Tilemaps never come in stripes Tongue

sharksharkshark: if you haven't guess it by now, "invisible" tiles are invisible walls...

By the way: hello everyone, I am new to the forum !
Logged
Adam Atomic
Administrator
Hero Member
*****
Posts: 724


hostest w/ the mostest


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

if you're doing collisions empties will be treated as colliding, since they're inside the box of the block.  Really the empties option was included mainly for non-colliding display tricks
Logged

Pages: [1]
  Print  
 
Jump to: