FlashPunk Forums

September 02, 2010 *
News:
 



Pages: [1]
  Print  
Author Topic: net.flashpunk.graphics.BackdropAutotile  (Read 333 times)
p0wn
....................
*
Posts: 14


View Profile Email
« on: June 10, 2010 »

Autotiling backdrop

What it does: creates a backdrop using a spritemap, and randomly places tiles. It creates a nice textured backdrop with the right sprite sheet.

Note: Due to the modifications required, this could be considered alpha at best.

In order to get this to work you must:
  • Edit net.flashpunk.graphics.Image:

    Change

    private var _buffer:BitmapData;

    to

    protected var _buffer:BitmapData;

  • Then you must edit net.flashpunk.graphics.Spritemap:

    add a getter function for the variable pixels:
Code:
/*
 * Get the current buffer
 *
 */
public function get pixels():BitmapData
{
return super._buffer;
}
    [/li]


These are necessary mods, maybe someone can figure out a way to do this without them.

Here's the code for the class:

Code:
package net.flashpunk.graphics
{
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
import net.flashpunk.FP;
import net.flashpunk.Graphic;

/**
* A background texture that can be repeated horizontally and vertically
* when drawn. Really useful for parallax backgrounds, textures, etc.
*/
public class BackdropAutotile extends Canvas
{
/**
* Constructor.
* @param texture Source texture.
* @param repeatX Repeat horizontally.
* @param repeatY Repeat vertically.
*/
public function BackdropAutotile(texture:*, size:uint = 16)
{
if (autoTile) _autoTileSpriteMap = new Spritemap(texture, size, size);
_textWidth = _texture.width;
_textHeight = _texture.height;
super(FP.width + _textWidth, FP.height + _textHeight);
FP.rect.x = FP.rect.y = 0;
FP.rect.width = _width;
FP.rect.height = _height;
var matrix:Matrix = new Matrix();

_graphics.clear();

for (var rows:uint = 0; rows < uint(_height / size); rows++)
for (var cols:uint = 0; cols < uint(_width / size); cols++)
{

_autoTileSpriteMap.setFrame(uint(FP.random * _autoTileSpriteMap.cols), uint(FP.random * _autoTileSpriteMap.rows));
matrix.translate(cols * size, rows * size);
draw(cols * size, rows * size, _autoTileSpriteMap.pixels);

}


}

/** @private Renders the Backdrop. */
override public function render(point:Point, camera:Point):void
{
point.x += x - camera.x * scrollX;
point.y += y - camera.y * scrollY;

if (_repeatX)
{
point.x %= _textWidth;
if (point.x > 0) point.x -= _textWidth;
}

if (_repeatY)
{
point.y %= _textHeight;
if (point.y > 0) point.y -= _textHeight;
}

_x = x; _y = y;
camera.x = camera.y = x = y = 0;
super.render(point, camera);
x = _x; y = _y;
}

// Backdrop information.
/** @private */ private var _autoTileSpriteMap:Spritemap;
/** @private */ private var _texture:BitmapData;
/** @private */ private var _textWidth:uint;
/** @private */ private var _textHeight:uint;
/** @private */ private var _repeatX:Boolean;
/** @private */ private var _repeatY:Boolean;
/** @private */ private var _x:Number;
/** @private */ private var _y:Number;
}
}

Logged
Exarmaster
....................
*
Posts: 45


I am, therefore, I am.

Exarimus
View Profile WWW
« Reply #1 on: June 10, 2010 »

I've forgotten a bit about FlashPunk since I last used it,
but perhaps you could add some classes.
Make one that extends Image, and one that extends Spritemap,
and merely add what you need to those classes.
If what I know of classes applies to FP, it should be rather simple,
and ensures that no modifications to the core are needed.
Logged
p0wn
....................
*
Posts: 14


View Profile Email
« Reply #2 on: June 10, 2010 »

This class extends Image, and the mods are required for this to work, unfortunately.
Logged
prchakal
--------------

Posts: 3


View Profile Email
« Reply #3 on: June 14, 2010 »

and how to use it?

do you have a sample code with sample images?
Logged
getbrett
....................
*
Posts: 11


View Profile Email
« Reply #4 on: June 18, 2010 »

Looks interesting, could you provide an example of its application?
Logged
p0wn
....................
*
Posts: 14


View Profile Email
« Reply #5 on: June 22, 2010 »

It's used just like backdrop, except you give it an image that's a spritemap. I will post some code later, running to work right now
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC