Created
February 7, 2012 21:24
-
-
Save jgranick/1762072 to your computer and use it in GitHub Desktop.
How to switch graphics based on screen density (NME recipe)
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 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/"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.