我正在尝试通过将一些从 Sprite 继承的 actionscript 类附加到我的 Flex 应用程序,方法是将其附加到 UIComponent,然后将 UIComponent 添加到面板。但是,我的 Sprite 类的大小似乎比面板大。所以,我尝试使用 DisplayObject.scale 属性调整它的大小,但我需要知道我的容器的大小。由于我试图使我的代码可重用,我认为我应该让我的 Sprite 类通过其父属性获取它来处理调整大小(假设我可以通过 set 调整它的大小
this.scaleX = this.parent.Width/this.width 或类似的东西)
但是,我的 Sprite 类的父类 UIComponent 的宽度/高度为 0。所以,我的问题是:1) 有没有更好的方法来调整 DisplayObject 类的大小以适应其容器当附加到 Flex 对象时?2) 如果您知道,为什么我的 UIComponent 的大小仍然为 0,而我的 DisplayObject 仍然显示为完整大小?
这是我的代码,使用 Flash Builder 4:
私有 var loader:Loader = new Loader(); 私有函数 testLoader():void { var urlRequest:URLRequest = new URLRequest("http://localhost/welcome.png"); loader.load(urlRequest); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderLoadComplete); } 私有函数 onLoaderLoadComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete); var ui : UIComponent = new UIComponent(); ui.addChild(loader); panelTest.addElement(ui); 跟踪(“ui.width =” + ui.width); 跟踪(“ui.height =” + ui.height); trace("loader.width = " + loader.width); trace("loader.height = " + loader.height); trace("panelTest.width = " + panelTest.width); trace("panelTest.height = " + panelTest.height); }
这是运行时的结果:
ui.width = 0 ui.height = 0 装载机宽度 = 571 装载机.高度 = 411 panelTest.width = 480 panelTest.height = 320