Flash Builder Mobile AS3项目:将DisplayObject置于中心

时间:2012-05-07 22:41:32

标签: actionscript-3 flash object mobile center

编辑:另请参阅Stackoverflow上的下一个问题: Flash builder Mobile AS3 project: Screen DPI, Application DPI, image DPI, center image

它与DPI设置有关.......


我想将DisplayObject置于屏幕中心。这是从资源动态创建的图像。我使用以下代码在对象创建后居中:

public function showProgress(bValue : Boolean = true):void
{
  this.imgProgress.visible = bValue;
  if( bValue )
  {
    this.imgProgress.x = (this.stage.stageWidth - this.imgProgress.width) * 0.5;
  }
}

对我来说看起来很简单,但对象仍然不在中心。例如:

this.stage.stageWidth == 500;
this.imgProgress.width == 480;
Result is: 10px; (this.imgProgress.x) /* 10px is correct */

我尝试了几件事,也使用了localToGlobal和GlobalToLocal,但是得到了不可预测的结果,仍然没有居中。

我认为它与屏幕dpi有什么关系?但是Capabilities.screenDPI会返回奇怪的结果。例如,作为iPhone 3GS运行,它返回163dpi。

如何计算正确的中心位置?

修改: 一些额外的信息:

public class Main extends Sprite
{
// setup stage
this.stage.color = 0;
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;

imgProgress是this.stage的孩子

1 个答案:

答案 0 :(得分:0)

它与纵横比有关(计算时你可以正确地对中)

另见: Set ApplicationDPI in AS3 Mobile application