Skip to content

Instantly share code, notes, and snippets.

@steverichey
Created December 18, 2013 18:23
Show Gist options
  • Save steverichey/8027205 to your computer and use it in GitHub Desktop.
Save steverichey/8027205 to your computer and use it in GitHub Desktop.
Custom Preloader for HaxeFlixel, used in Don't Move
package;
import flash.Lib;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flixel.system.FlxPreloader;
import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
class Preloader extends FlxPreloader
{
// will only need to call this one item more than once
private var _progressBar:Bitmap;
static inline var BAR_HEIGHT:Int = 32;
static inline var BAR_WIDTH:Int = 632;
public function new()
{
super();
minDisplayTime = 1;
}
// note the lack of a super call; this is to prevent drawing unnecessary items, which was
// causing crashes in HTML5, which I hope to target someday!
override private function onAddedToStage( e:Event ):Void
{
removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
Lib.current.stage.align = StageAlign.TOP_LEFT;
addEventListener( Event.ENTER_FRAME, onEnterFrame );
create();
}
// this class mimics the look of the levelup bar from playstate but is drawn programmatically.
// this is to prevent importing anything unnecessary, like PNG files.
// as a result, everything is hand-coded to use 4x pixels, like the rest of the game.
override private function create():Void
{
_min = 0;
#if FLX_NO_DEBUG
_min = Std.int( minDisplayTime * 1000 );
#end
var border1:Bitmap = new Bitmap( new BitmapData( BAR_WIDTH - 8, 4, false, 0xff949494 ) );
var border2:Bitmap = new Bitmap( new BitmapData( 4, BAR_HEIGHT - 8, false, 0xff949494 ) );
var border3:Bitmap = new Bitmap( new BitmapData( 4, BAR_HEIGHT - 8, false, 0xff545454 ) );
var border4:Bitmap = new Bitmap( new BitmapData( BAR_WIDTH - 8, 4, false, 0xff545454 ) );
_progressBar = new Bitmap( new BitmapData( 1, BAR_HEIGHT - 8, false, 0xffE5240F ) );
var progressBarShine = new Bitmap( new BitmapData( BAR_WIDTH - 16, 4, false ) );
_progressBar.x = border4.x = border1.x = Std.int( Lib.current.stage.stageWidth / 2 ) - Std.int( border1.width / 2 );
border1.y = Std.int( Lib.current.stage.stageHeight / 2 ) - Std.int( BAR_HEIGHT / 2 );
border2.x = border1.x - 4;
_progressBar.y = border3.y = border2.y = border1.y + 4;
border3.x = border1.x + border1.width;
border4.y = border2.y + border2.height;
progressBarShine.x = border4.x + 4;
progressBarShine.y = border3.y + 4;
addChild( border1 );
addChild( border2 );
addChild( border3 );
addChild( border4 );
addChild( _progressBar );
addChild( progressBarShine );
}
// fill the bar as progress continues. note that it only updates every 4 pixels, mimicking the 4x resolution
// of the rest of the game.
override private function update( Percent:Float ):Void
{
_progressBar.width = Std.int( Percent * ( BAR_WIDTH - 8 ) / 4 ) * 4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment