EDIT: See also next question on Stackoverflow:
Flash builder Mobile AS3 project: Screen DPI, Application DPI, image DPI, center image
It has something to do with DPI settings…….
I want to center a DisplayObject to the center of the screen. This is an image dynamically created from a resource. I use the the following code to center the object after it is created:
public function showProgress(bValue : Boolean = true):void
{
this.imgProgress.visible = bValue;
if( bValue )
{
this.imgProgress.x = (this.stage.stageWidth - this.imgProgress.width) * 0.5;
}
}
Looks quite simple to me but the object is still not in the center. For example:
this.stage.stageWidth == 500;
this.imgProgress.width == 480;
Result is: 10px; (this.imgProgress.x) /* 10px is correct */
I tried several things, also with localToGlobal and GlobalToLocal but getting unpredictable results, still not centered.
I think it has something to do with the screen dpi or something? But the Capabilities.screenDPI returns strange results. For example, running as iPhone 3GS it returns 163dpi.
How do i calculate the correct center position?
EDIT:
Some extra info:
public class Main extends Sprite
{
// setup stage
this.stage.color = 0;
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
imgProgress is a child of this.stage
It has something to with the aspect ratio (when calculated you are able to center it correcly)
See also:
Set ApplicationDPI in AS3 Mobile application