Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created February 7, 2012 21:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgranick/1762072 to your computer and use it in GitHub Desktop.
Save jgranick/1762072 to your computer and use it in GitHub Desktop.
How to switch graphics based on screen density (NME recipe)
package com.eclecticdesignstudio.minehx;
import nme.display.Sprite;
import nme.system.Capabilities;
import nme.Assets;
/**
* ...
* @author Joshua Granick
*/
class MineHX extends Sprite {
public function new () {
super ();
var baseImagePath = "";
var screenDensity = 1;
var dpi = Capabilities.screenDPI;
if (dpi < 200) {
screenDensity = 1;
baseImagePath = "images/quality/standard/";
} else if (dpi < 300) {
screenDensity = 1.5;
baseImagePath = "images/quality/medium/";
} else {
screenDensity = 2;
baseImagePath = "images/quality/high/";
}
}
}
@jgranick
Copy link
Author

jgranick commented Feb 7, 2012

This isn't the only way it could be handled, but it is one example of how you can use Capabilities.screenDPI to switch on higher resolution graphics.

In this example, all of the images have been exported at three different sizes, representative of "1x", "1.5x" and "2x"

The screenDensity value can be used for relative positioning. For example, instead of positioning an object at "x = 100", you could use "x = 100 * screenDensity" in order to take the scale into account.

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