为什么在Flash Builder 4.6中使用flex的stage.stageWidth不能正常工作?

时间:2012-01-11 17:14:44

标签: android actionscript-3 flex flash-builder

下午好,
我正在使用adobe air创建一个Android应用程序(flex sdk 4.6.0),我试图找出手机屏幕的宽度是多少像素。所以我正在暂停教程here,但是当我将代码复制到我的项目中并单击运行时,它总是给我这个错误“TypeError:错误#1009:无法访问空对象引用的属性或方法。”并转到具有stage.stageWidth的行我也尝试过stage.width,但它做了同样的事情。那么stage.stageWidth不再起作用,或者你如何判断平板电脑或手机的屏幕大小?用手机替换了其他东西吗?我正在尝试获取信息,以便我可以使用动作脚本将图像置于屏幕中央。

protected function init():void {
            //function run at creationComplete="init()"
            movePhotoToCenter(myIMG1);
            // trying to get the stage width so that i can center a img that is allready added to the flex code and has a id of "myIMG1"
        }



        protected function movePhotoToCenter(img:Image):void {
            //"error here" where im centering the image
            var centerOfStage:Number = stage.stageWidth/2;
            var Xnumber:Number = centerOfStage - (img.width/2);
            trace(Xnumber);
            var move:Move = new Move(img);
            move.xFrom = img.x;
            move.xTo = Xnumber;
            move.yFrom = img.y;
            move.yTo = img.y;
            move.duration = 3000;
            move.play();
            trace(img.width);
            img.scaleX=1;
            img.scaleY=1;
            trace(img.width);   
        }

谢谢你的帮助, 贾斯汀

1 个答案:

答案 0 :(得分:4)

creationComplete完成事件在添加到 显示列表时触发,而不一定是 Stage的显示列表。 creationComplete在创建此对象及其所有子对象并将其添加到某些内容时触发。

还有一个事件要绑定到被调用的addedToStage(无论如何在flex地),当DisplayObject实际被添加到舞台时会发生。

请注意creationComplete只会在实例化一个对象并将其作为子项添加到某个对象时触发一次,其中addedToStage会在它被添加到舞台的任何时候触发(在它的情况下)被移除并在以后重新添加而不重新构建对象。)

有关此主题的更多详细信息,请参阅this blog post