1. 在可视化对象列表中添加项目
Flash Player 现在把ActionScript 虚拟机(AVM)和渲染引擎两块功能集成在一起了。AVM 完成执行ActionScript 代码,而渲染引擎负责在屏幕上绘制对象,绘制对象需要两步骤:通过ActionScript引擎创建可视化对象。渲染引擎把可视化对象绘制在屏幕上
AVM中创建了一个可视化对象后,并没有绘制在屏幕上,因为它还不存在于渲染引擎里,要把它放到渲染引擎里需要添加该对象到可视化对象列表,通过调用DisplayObjectContainer实例的addChild()或addChildAt()方法添加
package
{
import
flash.display.Shape;import
flash.display.Sprite;public class
Sample1120extends
Sprite
{
public function
Sample1120()
{
var
red:Shape =new
Shape();var
green:Shape =new
Shape();var
blue:Shape =new
Shape(); red.graphics.beginFill(0xFF0000); red.graphics.drawCircle(10,20,10); red.graphics.endFill(); green.graphics.beginFill(0x00FF00); green.graphics.drawCircle(15,25,10); green.graphics.endFill(); blue.graphics.beginFill(0x0000FF); blue.graphics.drawCircle(20,20,10); blue.graphics.endFill(); stage.addChild(red); stage.addChild(green); stage.addChildAt(blue,2);//下面演示重复添加同一对象
//var container1:Sprite = new Sprite();
//container1.addChild(red);
//container1.addChild(green);
//container1.addChild(blue);
//this.addChild(container1);
//var container2:Sprite = new Sprite();
//container2.addChild(red);
//this.addChild(container2);
} } }
2. 在可视化对象列表中删除项目
使用DisplayObectContainer类的removeChild()和removeChildAt()方法
package
{
import
flash.display.DisplayObjectContainer;import
flash.display.Sprite;import
flash.text.TextField;public class
Sample1120extends
Sprite
{
public function
Sample1120()
{
this
.addChild(new
TextField());this
.addChild(new
TextField());this
.addChild(new
TextField());trace
("删除前:"
+this
.numChildren); RemoveAllElement(this
);trace
("删除后:"
+this
.numChildren); }private function
RemoveAllElement(container:DisplayObjectContainer):void
{
var
length:int = container.numChildren;for
(var
i:int=0;i<length;i++)
{
this
.removeChildAt(0); } } } }
3. 在可视化对象列表中移动对象位置
使用DisplayObectContainer类的setChildIndex()方法改变项目的位置,getChildIndex()和getChildAt()方法得到项目在显示列表中的位置。getChildIndex()返回当前对象的索引,getChildAt()返回当前对象的引用
package
{
import
flash.display.Shape;import
flash.display.Sprite;import
flash.events.MouseEvent;public class
Sample1120extends
Sprite
{
public function
Sample1120()
{
var
arrColor:Array = [0xFF0000,0x00FF00,0x0000FF,0xFFFF00,0x00FFFF]; stage.addEventListener(MouseEvent.CLICK,OnClick);for
(var
i:int=0; i<arrColor.length;i++)
{
var
shape:Shape = AddElement(arrColor[i]); shape.x = i*3+20; shape.y = 20; } }private function
AddElement(color:int):Shape
{
var
shape:Shape =new
Shape(); shape.graphics.beginFill(color); shape.graphics.drawCircle(10,10,30); shape.graphics.endFill();this
.addChild(shape);return
shape; }private function
OnClick(event:MouseEvent):void
{
this
.setChildIndex(this
.getChildAt(0),this
.numChildren-1); } } }
4. 创建自定义可视化类
继承DisplayObject 或它的子类来创建新类
package
{
import
flash.display.Shape;import
flash.display.Sprite;public class
Sample1121extends
Sprite
{
public function
Sample1121()
{
var
_circle:Circle =new
Circle(0xFF0000,80); _circle.x = 100; _circle.y = 100; addChild(_circle); } } }import
flash.display.Shape;internal class
Circleextends
Shape
{
private var
_color:uint;private var
_radius:uint;public function
Circle(color:uint=0x000000,radius:uint=10)
{
_color = color;
_radius = radius;
Draw();
}
private function
Draw():void
{
graphics.beginFill(_color);
graphics.drawCircle(0,0,_radius);
graphics.endFill();
}
}
5. 创建简单的按钮
package { import flash.display.Sprite; import flash.display.StageScaleMode; import flash.events.MouseEvent; public class Test extends Sprite { public function Test() { stage.scaleMode = StageScaleMode.NO_SCALE; var button1:MyButton = new MyButton("确 定"); button1.addEventListener(MouseEvent.CLICK,onClick); this.addChild(button1); } private function onClick(event:MouseEvent):void { trace("click"); } } } import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; internal class MyButton extends Sprite { private var button:Sprite; private var label:TextField; private var textFormat:TextFormat; private var _text:String; private var _width:Number; private var _height:Number; private var _bgColor:uint; private var _fontColor:uint; private var _overBgColor:uint; private var _overFontColor:uint; public function MyButton(text:String,width:Number=85,height:Number=30,bgColor:uint=0x14B9F8,fontColor:uint=0xFFFFFF,overBgColor:uint=0x2BC4FE,overFontColor:uint=0xFFFFFF) { this._text = text; this._width = width; this._height = height; this._bgColor = bgColor; this._fontColor = fontColor; this._overBgColor = overBgColor; this._overFontColor = overFontColor; button = new Sprite(); button.graphics.beginFill(this._bgColor); button.graphics.drawRoundRect(0,0,this._width,this._height,10,10); button.graphics.endFill(); this.addChild(button); label = new TextField(); label.autoSize = TextFieldAutoSize.CENTER; label.selectable = false; label.mouseEnabled = false; textFormat = new TextFormat(); textFormat.color = this._fontColor; textFormat.size = 16; label.defaultTextFormat = textFormat; label.text = this._text; label.x = (button.width-label.width)/2; label.y = (button.height-label.height)/2+1; button.addChild(label); //mask var maskRect:Sprite = new Sprite(); maskRect.graphics.beginFill(0xFFFF00,0); maskRect.graphics.drawRoundRect(0,0,this._width,this._height,10,10); maskRect.graphics.endFill(); maskRect.addEventListener(MouseEvent.MOUSE_OVER,button_mouseOverHandler); maskRect.addEventListener(MouseEvent.MOUSE_OUT,button_mouseOutHandler); maskRect.buttonMode = true; this.addChild(maskRect); } private function button_mouseOverHandler(event:MouseEvent):void { button.graphics.clear(); button.graphics.beginFill(this._overBgColor); button.graphics.drawRoundRect(0,0,this._width,this._height,10,10); button.graphics.endFill(); textFormat = new TextFormat(); textFormat.color = this._overFontColor; textFormat.size = 16; label.defaultTextFormat = textFormat; label.text = this._text; } private function button_mouseOutHandler(event:MouseEvent):void { button.graphics.clear(); button.graphics.beginFill(this._bgColor); button.graphics.drawRoundRect(0,0,this._width,this._height,10,10); button.graphics.endFill(); textFormat = new TextFormat(); textFormat.color = this._fontColor; textFormat.size = 16; label.defaultTextFormat = textFormat; label.text = this._text; } }