public function addItem(item:DisplayObject):void { addChild(item); layoutContents(); }
package { import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.Event; public class List extends Sprite { public function addItem(item:DisplayObject):void { addChild(item); layoutContents(); } public function removeItem(item:DisplayObject):void { if(contains(item)) { removeChild(item); layoutContents(); } } //对内部项目进行排列,可以是任意的排列算法 protected function layoutcontents():void { trace("do layout"); var y:Number = 0; var num:int = numChildren; for(var i:int=0; i<num; i++) { var child:DisplayObject = getChildAt(i); child.x = 0; child.y = y; y += child.height+2; } } }
package { import flash.display.Shape; public class ListItem extends Shape { public function ListItem() { super(); graphics.beginFill(0xFF6600); graphics.drawRect(0, 0, 30, 16); graphics.endFill(); } } }
package { import flash.display.Sprite; public class ListTest extends Sprite { public function ListTest() { var list:List = new List(); addChild(list); list.addItem(new ListItem()); list.addItem(new ListItem()); list.addItem(new ListItem()); } } }
我们可以看到,输出了3次 do layout 说明layoutcontents执行了3次,前两次都是多余的。
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |