Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created February 7, 2012 21:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgranick/1762234 to your computer and use it in GitHub Desktop.
Save jgranick/1762234 to your computer and use it in GitHub Desktop.
How to dynamically handle different screen sizes (NME recipe)
import nme.display.Sprite;
import nme.display.StageAlign;
import nme.display.StageScaleMode;
import nme.Lib;
/**
* @author Joshua Granick
*/
class ScreenResize extends Sprite {
private var setHeight:Float;
private var setWidth:Float;
public function new () {
super ();
initialize ();
resize (Lib.current.stage.stageWidth, Lib.current.stage.stageHeight);
Lib.current.stage.addEventListener (Event.RESIZE, stage_onResize);
}
private function initialize ():Void {
Lib.current.stage.align = StageAlign.TOP_LEFT;
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
}
private function resize (newWidth:Float, newHeight:Float):Void {
// position objects based on size
this.setHeight = newHeight;
this.setWidth = newWidth;
}
// Event Handlers
private function stage_onResize (event:Event):Void {
resize (stage.stageWidth, stage.stageHeight);
}
}
@donaldalanmorrison
Copy link

@jgranick: Is it necessary to extend Sprite here? I'm new to NME and Flash, so please forgive the innocent question. Is the ScreenResize class intended to be a singleton?

@jgranick
Copy link
Author

jgranick commented Aug 6, 2012

Hi!

This is a small project to illustrate an example of listening to stage RESIZE events.

It extends Sprite, because this is actually the full application. In Flash or NME, the main class for your application should extend the Sprite class, or extend a class that extends the Sprite class.

If you wanted to handle resizing in your own project, you should be able to use this sample to copy, paste or modify for your own use :)

@donaldalanmorrison
Copy link

@jgranick: Thanks, that was another guess -- the stage must have one sprite child at its root. I'll google the rest of the rules in the Flash API. I won't suggest a specific renaming of the ScreenResize class because I'm not familiar with all the rules, and their consequent impact on workflow/user scenarios/etc.

Digressing: I tried guessing your email because I'm curious how many packages in the standard NMEHaXe.org distribution are truly multi-platform (other than just nme.*). :-)

@donaldalanmorrison
Copy link

Well, I don't see a way to delete comments, so I'll just say I've sorted out which packages are cross-platform and which aren't. I found it useful to install extra libs from the command-line via "haxelib install ___". This page was helpful: http://www.haxenme.org/documentation/features/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment