我正在扩展Loader()
,是否有可能在内容周围绘制边框而不诉诸某些黑客?我理解Loader不能有任何额外的孩子,否则我只是创建一个内容大小的形状并添加它.但有没有办法强制或转换内容对象,以便在其上使用绘图API?
通常,您只需在加载时将内容添加到显示对象,并根据需要对其进行操作:
var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, eventCompleteHandler); loader.load(new URLRequest(URL)); function eventCompleteHandler(event:Event):void { addChild(createElementWithBorder(event.target.content)); } function createElementWithBorder(content:DisplayObject):DisplayObject { var container:Sprite = new Sprite(); container.addChild(content); var border:Shape = container.addChild(new Shape()) as Shape; border.graphics.lineStyle(2,0x000000); border.graphics.drawRect(0, 0, content.width,content.height); return container; }
...但是如果你仍然生气,你不能使用Loader作为DisplayObjectContainer,使用组合可能是线索(以下只是一个简单的例子,当然需要适应):
public class LoaderBorder extends Sprite { private var loader:Loader; public function LoaderBorder() { loader = new Loader() } public function load(url:String):void { loader.contentLoaderInfo.addEventListener(Event.COMPLETE, eventCompleteHandler); loader.load(new URLRequest(url)); } private function eventCompleteHandler(event:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, eventCompleteHandler); var content:DisplayObject = event.target.content; var border:Shape = new Shape(); border.graphics.lineStyle(2,0x000000); border.graphics.drawRect(0, 0, content.width,content.height); addChild(content); addChild(border); } }
像装载机一样使用:
var test:LoaderBorder = new LoaderBorder() addChild(test); test.load(URL);