Skip to content

Instantly share code, notes, and snippets.

@Beeblerox
Beeblerox / FlxBoundingBoxSprite.hx
Created April 18, 2016 17:04
FlxSprite with correct bounding box calculations
package;
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.math.FlxRect;
/**
* ...
* @author Zaphod
*/
var tileWidth:Int = 32;
var tileHeight:Int = 32;
var tileSize:FlxPoint = new FlxPoint(tileWidth, tileHeight);
var tileSpacing:FlxPoint = new FlxPoint(2, 2); // we need to add spacing between tile images
var tileBorder:FlxPoint = new FlxPoint(2, 2); // and add borders around tiles (copy some pixels around tile images)
var tileFrames:FlxTileFrames = FlxTileFrames.fromBitmapAddSpacesAndBorders("your/tileset/image", tileSize, tileSpacing, tileBorder); // this method just takes original image, creates new one and generates required collection of frames, which can be loaded in tilemap or sprite
tilemap.loadMapFromCSV(mapData, tileFrames); // all tilemap loading methods can take tile frames collection as a source of graphics for them
/**
* This method takes array of tileset bitmaps and the size of tiles in them and then combine then in one big tileset.
* The order of bitmaps in array is important.
*
* @param bitmaps tilesets
* @param tileSize the size of tiles (tilesets should have tiles of the same size)
* @return atlas frames collection, which you can load in tilemaps or sprites:
*
* var combinedFrames = combineTiles(bitmaps, new FlxPoint(16, 16));
* tilemap.loadMapFromCSV(mapData, combinedFrames);
typedef FlxGraphicAsset = AcceptOneOfFive<String, Class<Dynamic>, CachedGraphics, TextureRegion, BitmapData>;
abstract AcceptOneOfFive<T1, T2, T3, T4, T5>(EitherOfFive<T1, T2, T3, T4, T5>)
{
public inline function new(e:EitherOfFive<T1, T2, T3, T4, T5>)
{
this = e;
}
public var value(get, never):Dynamic;
_point.x = x;
_point.y = y;
var csx:Float = 1;
var ssy:Float = 0;
var ssx:Float = 0;
var csy:Float = 1;
var x1:Float = (_origin.x - frame.center.x) * scale.x;
var y1:Float = (_origin.y - frame.center.y) * scale.y;
@Beeblerox
Beeblerox / GetButtonCodesState
Created January 5, 2014 10:40
Helper state for getting codes for button on gamepad
package ;
import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.util.FlxColor;
#if cpp
import openfl.events.JoystickEvent;
#end
@Beeblerox
Beeblerox / dumpBits example
Created July 8, 2013 12:17
This is an example of how you can reduce memory usage for Tilesheet class in openfl with dumpBits() method. But you also listen for stage resize event and recreate tilesheet after context loss.
import flash.Lib;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import openfl.Assets;
import openfl.display.Tilesheet;
import flash.display.Graphics;
import flash.events.Event;
class Particle
package ;
import org.flixel.FlxG;
import org.flixel.plugin.pxText.PxBitmapFont;
import nme.display.BitmapData;
import nme.geom.Rectangle;
class BitmapFont extends PxBitmapFont
{
private var glyphWidth:Int;