Skip to content

Instantly share code, notes, and snippets.

@klaufir
Created June 21, 2013 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaufir/5830629 to your computer and use it in GitHub Desktop.
Save klaufir/5830629 to your computer and use it in GitHub Desktop.
HaxeFlixel crashing after FlxG.switchState
package;
import nme.Lib;
import org.flixel.FlxGame;
import org.flixel.FlxG;
import org.flixel.FlxSprite;
import org.flixel.FlxState;
class GameChangerMain extends FlxGame
{
public function new()
{
FlxG.debug = true;
var stageWidth:Int = Lib.current.stage.stageWidth;
var stageHeight:Int = Lib.current.stage.stageHeight;
var ratioX:Float = stageWidth / 320;
var ratioY:Float = stageHeight / 480;
var ratio:Float = Math.min(ratioX, ratioY);
super( 80, 120, MenuState, 4, 30, 30);
}
}
class MenuState extends FlxState
{
public static var playState:PlayState;
public static var ball:Agent;
override public function update():Void
{
// PRESS SPACE TO CRASH!
if ( FlxG.keys.justPressed( "SPACE" ) )
{
playState = new PlayState();
ball = new Agent();
playState.add( ball );
FlxG.switchState( playState );
}
super.update();
}
}
class Agent extends FlxSprite
{ }
class PlayState extends FlxState
{ }
package;
import nme.display.StageAlign;
import nme.display.StageScaleMode;
import nme.display.Sprite;
import nme.events.Event;
import nme.events.KeyboardEvent;
import nme.Lib;
import nme.ui.Keyboard;
import org.flixel.FlxGame;
class Main extends Sprite
{
public function new ()
{
super();
if (stage != null)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(?e:Event = null):Void
{
if (hasEventListener(Event.ADDED_TO_STAGE))
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
initialize();
var demo:FlxGame = new GameChangerMain();
addChild(demo);
#if (cpp || neko)
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUP);
#end
}
#if (cpp || neko)
private function onKeyUP(e:KeyboardEvent):Void
{
if (e.keyCode == Keyboard.ESCAPE)
{
Lib.exit();
}
}
#end
private function initialize():Void
{
Lib.current.stage.align = StageAlign.TOP_LEFT;
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
}
// Entry point
public static function main() {
Lib.current.addChild(new Main());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment