Created
June 3, 2014 17:18
-
-
Save misterpah/cdf9adc1156552d9e0c5 to your computer and use it in GitHub Desktop.
crashdumper in flash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.Lib; | |
import flixel.FlxGame; | |
import flixel.FlxState; | |
import openfl.Lib; | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import haxe.CallStack; | |
#if linux | |
import openfl.events.UncaughtErrorEvent; | |
#elseif flash | |
import flash.events.UncaughtErrorEvent; | |
#end | |
class Main extends Sprite | |
{ | |
private static var endl:String = "\n"; | |
private static var sl:String = "/"; | |
var gameWidth:Int = 640; // Width of the game in pixels (might be less / more in actual pixels depending on your zoom). | |
var gameHeight:Int = 480; // Height of the game in pixels (might be less / more in actual pixels depending on your zoom). | |
var initialState:Class<FlxState> = MenuState; // The FlxState the game starts with. | |
var zoom:Float = -1; // If -1, zoom is automatically calculated to fit the window dimensions. | |
var framerate:Int = 60; // How many frames per second the game should run at. | |
var skipSplash:Bool = false; // Whether to skip the flixel splash screen that appears in release mode. | |
var startFullscreen:Bool = false; // Whether to start the game in fullscreen on desktop targets | |
// You can pretty much ignore everything from here on - your code should go in your states. | |
public static function main():Void | |
{ | |
Lib.current.addChild(new Main()); | |
Lib.current.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onErrorEvent); | |
var b:BitmapData = null; | |
b.clone(); | |
} | |
private static function onErrorEvent(e:Dynamic) | |
{ | |
trace(crashStr(e.error)); | |
} | |
private static function crashStr(errorData:Dynamic):String { | |
return "--------------------------------------" + endl + | |
"crashed:\t" + Date.now().toString() + endl + | |
"error:\t\t" + errorData + endl + | |
"stack:" + endl + getStackTrace() + endl; | |
} | |
private static function getStackTrace():String | |
{ | |
var stack:Array<StackItem> = CallStack.exceptionStack(); | |
var stackTrace:String = ""; | |
stack.reverse(); | |
var item:StackItem; | |
for (item in stack) | |
{ | |
//trace(item); | |
stackTrace += printStackItem(item) + endl; | |
} | |
return stackTrace; | |
} | |
private static function printStackItem(itm:StackItem):String | |
{ | |
var str:String = ""; | |
switch( itm ) { | |
case CFunction: | |
str = "a C function"; | |
case Module(m): | |
str = "module " + m; | |
case FilePos(itm,file,line): | |
if( itm != null ) { | |
str = printStackItem(itm) + " ("; | |
} | |
str += file; | |
str += " line "; | |
str += line; | |
if (itm != null) str += ")"; | |
case Method(cname,meth): | |
str += (cname); | |
str += ("."); | |
str += (meth); | |
case LocalFunction(n): | |
str += ("local function #"); | |
str += (n); | |
} | |
return str; | |
} | |
public function new() | |
{ | |
super(); | |
if (stage != null) | |
{ | |
init(); | |
} | |
else | |
{ | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
} | |
private function init(?E:Event):Void | |
{ | |
if (hasEventListener(Event.ADDED_TO_STAGE)) | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
setupGame(); | |
} | |
private function setupGame():Void | |
{ | |
var stageWidth:Int = Lib.current.stage.stageWidth; | |
var stageHeight:Int = Lib.current.stage.stageHeight; | |
if (zoom == -1) | |
{ | |
var ratioX:Float = stageWidth / gameWidth; | |
var ratioY:Float = stageHeight / gameHeight; | |
zoom = Math.min(ratioX, ratioY); | |
gameWidth = Math.ceil(stageWidth / zoom); | |
gameHeight = Math.ceil(stageHeight / zoom); | |
} | |
addChild(new FlxGame(gameWidth, gameHeight, initialState, zoom, framerate, framerate, skipSplash, startFullscreen)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment