flash.display
public class DisplayObjectContainer
继承DisplayObjectContainer Inheritance InteractiveObject Inheritance DisplayObject Inheritance EventDispatcher Inheritance Object
子类 Loader, Sprite, Stage

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

DisplayObjectContainer 类是可用作显示列表中显示对象容器的所有对象的基类。 该显示列表管理 Flash Player 中显示的所有对象。 使用 DisplayObjectContainer 类排列显示列表中的显示对象。 每个 DisplayObjectContainer 对象都有自己的子级列表,用于组织对象的 Z 轴顺序。 Z 轴顺序是由前至后的顺序,可确定哪个对象绘制在前,哪个对象绘制在后等。

DisplayObject 是一种抽象基类;因此,不能直接调用 DisplayObject。 调用 new DisplayObject() 会引发 ArgumentError 异常。

DisplayObjectContainer 类是可以包含子对象的所有对象的抽象基类。 无法直接对其进行实例化;调用 new DisplayObjectContainer() 构造函数会引发 ArgumentError 异常。

有关详细信息,请参阅《ActionScript 3.0 编程》中的“显示编程”一章。

查看示例

另请参见

flash.display.DisplayObject
核心显示类
处理显示对象容器



公共 属性
 属性定义方
 InheritedaccessibilityProperties : AccessibilityProperties
此显示对象的当前辅助功能选项。
DisplayObject
 Inheritedalpha : Number
指示指定对象的 Alpha 透明度值。
DisplayObject
 InheritedblendMode : String
BlendMode 类中的一个值,用于指定要使用的混合模式。
DisplayObject
 InheritedcacheAsBitmap : Boolean
如果设置为 true,则 Flash Player 将缓存显示对象的内部位图表示形式。
DisplayObject
 Inheritedconstructor : Object
对类对象或给定对象实例的构造函数的引用。
Object
 InheritedcontextMenu : ContextMenu
指定与此对象相关联的上下文菜单。
InteractiveObject
 InheriteddoubleClickEnabled : Boolean
指定此对象是否接收 doubleClick 事件。
InteractiveObject
 Inheritedfilters : Array
包含当前与显示对象关联的每个滤镜对象的索引数组。
DisplayObject
 InheritedfocusRect : Object
指定此对象是否显示焦点矩形。
InteractiveObject
 Inheritedheight : Number
指示显示对象的高度,以像素为单位。
DisplayObject
 InheritedloaderInfo : LoaderInfo
[read-only] 返回一个 LoaderInfo 对象,其中包含加载此显示对象所属的文件的相关信息。
DisplayObject
 Inheritedmask : DisplayObject
调用显示对象被指定的 mask 对象遮罩。
DisplayObject
  mouseChildren : Boolean
确定对象的子项是否支持鼠标。
DisplayObjectContainer
 InheritedmouseEnabled : Boolean
指定此对象是否接收鼠标消息。
InteractiveObject
 InheritedmouseX : Number
[read-only] 指示鼠标位置的 x 坐标,以像素为单位。
DisplayObject
 InheritedmouseY : Number
[read-only] 指示鼠标位置的 y 坐标,以像素为单位。
DisplayObject
 Inheritedname : String
指示 DisplayObject 的实例名称。
DisplayObject
  numChildren : int
[read-only] 返回此对象的子项数目。
DisplayObjectContainer
 InheritedopaqueBackground : Object
指定显示对象是否由于具有某种背景颜色而不透明。
DisplayObject
 Inheritedparent : DisplayObjectContainer
[read-only] 指示包含此显示对象的 DisplayObjectContainer 对象。
DisplayObject
 Inheritedprototype : Object
[static] 对类或函数对象的原型对象的引用。
Object
 Inheritedroot : DisplayObject
[read-only] 对于加载的 SWF 文件中的显示对象,root 属性是此 SWF 文件所表示的显示列表树结构部分中的顶级显示对象。
DisplayObject
 Inheritedrotation : Number
指示 DisplayObject 实例距其原始方向的旋转程度,以度为单位。
DisplayObject
 Inheritedscale9Grid : Rectangle
当前有效的缩放网格。
DisplayObject
 InheritedscaleX : Number
指示从注册点开始应用的对象的水平缩放比例(百分比)。
DisplayObject
 InheritedscaleY : Number
指示从对象注册点开始应用的对象的垂直缩放比例(百分比)。
DisplayObject
 InheritedscrollRect : Rectangle
显示对象的滚动矩形范围。
DisplayObject
 Inheritedstage : Stage
[read-only] 显示对象的舞台。
DisplayObject
  tabChildren : Boolean
确定对象的子项是否支持 Tab 键。
DisplayObjectContainer
 InheritedtabEnabled : Boolean
指定此对象是否遵循 Tab 键顺序。
InteractiveObject
 InheritedtabIndex : int
指定 SWF 文件中的对象按 Tab 键顺序排列。
InteractiveObject
  textSnapshot : TextSnapshot
[read-only] 返回此 DisplayObjectContainer 实例的 TextSnapshot 对象。
DisplayObjectContainer
 Inheritedtransform : Transform
一个对象,具有与显示对象的矩阵、颜色转换和像素范围有关的属性。
DisplayObject
 Inheritedvisible : Boolean
显示对象是否可见。
DisplayObject
 Inheritedwidth : Number
指示显示对象的宽度,以像素为单位。
DisplayObject
 Inheritedx : Number
指示 DisplayObject 实例相对于父级 DisplayObjectContainer 本地坐标的 x 坐标。
DisplayObject
 Inheritedy : Number
指示 DisplayObject 实例相对于父级 DisplayObjectContainer 本地坐标的 y 坐标。
DisplayObject
公共 方法
 方法定义方
  
调用新的 DisplayObjectContainer() 构造函数会引发 ArgumentError 异常。
DisplayObjectContainer
  
将一个 DisplayObject 子实例添加到该 DisplayObjectContainer 实例中。
DisplayObjectContainer
  
将一个 DisplayObject 子实例添加到该 DisplayObjectContainer 实例中。
DisplayObjectContainer
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
使用 EventDispatcher 对象注册事件侦听器对象,以使侦听器能够接收事件通知。
EventDispatcher
  
指示安全限制是否会导致出现以下情况:在列表中忽略了使用指定 point 点调用 DisplayObjectContainer.getObjectsUnderPoint() 方法时返回的所有显示对象。
DisplayObjectContainer
  
确定指定显示对象是 DisplayObjectContainer 实例的子项还是该实例本身。
DisplayObjectContainer
 Inherited
将事件调度到事件流中。
EventDispatcher
 Inherited
getBounds(targetCoordinateSpace:DisplayObject):Rectangle
返回一个矩形,该矩形定义相对于 targetCoordinateSpace 对象坐标系的显示对象区域。
DisplayObject
  
返回位于指定索引处的子显示对象实例。
DisplayObjectContainer
  
返回具有指定名称的子显示对象。
DisplayObjectContainer
  
返回 DisplayObject 的 child 实例的索引位置。
DisplayObjectContainer
  
返回对象的数组,这些对象位于指定点下,并且是该 DisplayObjectContainer 实例的子项(或孙子项,依此类推)。
DisplayObjectContainer
 Inherited
getRect(targetCoordinateSpace:DisplayObject):Rectangle
返回一个矩形,该矩形根据 targetCoordinateSpace 参数定义的坐标系定义显示对象的边界,但不包括形状上的任何笔触。
DisplayObject
 Inherited
将 point 对象从舞台(全局)坐标转换为显示对象的(本地)坐标。
DisplayObject
 Inherited
检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器。
EventDispatcher
 Inherited
指示对象是否已经定义了指定的属性。
Object
 Inherited
计算显示对象,以确定它是否与 obj 显示对象重叠或相交。
DisplayObject
 Inherited
hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean
计算显示对象,以确定它是否与 x 和 y 参数指定的点重叠或相交。
DisplayObject
 Inherited
指示 Object 类的实例是否在指定为参数的对象的原型链中。
Object
 Inherited
将 point 对象从显示对象的(本地)坐标转换为舞台(全局)坐标。
DisplayObject
 Inherited
指示指定的属性是否存在、是否可枚举。
Object
  
从 DisplayObjectContainer 实例的子列表中删除指定的 child DisplayObject 实例。
DisplayObjectContainer
  
从 DisplayObjectContainer 的子列表中指定的 index 位置删除子 DisplayObject。
DisplayObjectContainer
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
从 EventDispatcher 对象中删除侦听器。
EventDispatcher
  
更改现有子项在显示对象容器中的位置。
DisplayObjectContainer
 Inherited
设置循环操作动态属性的可用性。
Object
  
交换两个指定子对象的 Z 轴顺序(从前到后顺序)。
DisplayObjectContainer
  
swapChildrenAt(index1:int, index2:int):void
在子级列表中两个指定的索引位置,交换子对象的 Z 轴顺序(前后顺序)。
DisplayObjectContainer
 Inherited
返回指定对象的字符串表示形式。
Object
 Inherited
返回指定对象的原始值。
Object
 Inherited
检查是否用此 EventDispatcher 对象或其任何始祖为指定事件类型注册了事件侦听器。
EventDispatcher
事件
 事件摘要定义方
 Inherited Flash Player 获得操作系统焦点并变为活动状态时调度。EventDispatcher
 Inherited 将显示对象添加到显示列表中时调度。DisplayObject
 Inherited 在将显示对象直接添加到舞台显示列表或将包含显示对象的子树添加至舞台显示列表中时调度。DisplayObject
 Inherited 用户在同一 InteractiveObject 上按下并释放用户指针设备的主按钮时调度。InteractiveObject
 Inherited Flash Player 失去操作系统焦点并变为非活动状态时调度。EventDispatcher
 Inherited 如果 InteractiveObject 的 doubleClickEnabled 标志设置为 true,当用户在该对象上快速连续按下两次并释放指针设备的主按钮时调度。InteractiveObject
 Inherited 播放头进入新帧时调度。DisplayObject
 Inherited 显示对象获得焦点后调度。InteractiveObject
 Inherited 显示对象失去焦点后调度。InteractiveObject
 Inherited 用户按下某个键时调度。InteractiveObject
 Inherited 用户尝试使用键盘导航更改焦点时调度。InteractiveObject
 Inherited 用户释放某个键时调度。InteractiveObject
 Inherited 在 Flash Player 窗口中,用户在 InteractiveObject 实例上按下指针设备按钮时调度。InteractiveObject
 Inherited 用户尝试使用指针设备更改焦点时调度。InteractiveObject
 Inherited 用户移动 InteractiveObject 上的指针设备时调度。InteractiveObject
 Inherited 用户将指针设备从 InteractiveObject 实例上移开时调度。InteractiveObject
 Inherited 在 Flash Player 窗口中,用户将指针设备移动到 InteractiveObject 实例上时调度。InteractiveObject
 Inherited 在 Flash Player 窗口中,用户在 InteractiveObject 实例上释放指针设备按钮时调度。InteractiveObject
 Inherited 在 Flash Player 窗口中,鼠标滚轮滚动到 InteractiveObject 实例上时调度。InteractiveObject
 Inherited 将要从显示列表中删除显示对象时调度。DisplayObject
 Inherited 在从显示列表中直接删除显示对象或删除包含显示对象的子树时调度。DisplayObject
 Inherited 将要更新和呈现显示列表时调度。DisplayObject
 Inherited 用户将指针设备从 InteractiveObject 实例上移开时调度。InteractiveObject
 Inherited 用户将指针设备移动到 InteractiveObject 实例上时调度。InteractiveObject
 Inherited 对象的 tabChildren 标志值发生更改时调度。InteractiveObject
 Inherited 对象的 tabEnabled 标志发生更改时调度。InteractiveObject
 Inherited 对象的 tabIndex 属性值发生更改时调度。InteractiveObject
属性详细信息
mouseChildren属性
mouseChildren:Boolean  [read-write]

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

确定对象的子项是否支持鼠标。 如果对象支持鼠标,则用户可以使用鼠标与其交互。 默认值为 true

当您使用 Sprite 类的实例(而不是使用 SimpleButton 类)来创建按钮时,此属性很有用。 当您使用 Sprite 实例来创建按钮时,您可以选择使用 addChild() 方法添加其它 Sprite 实例来修饰按钮。 此过程可能导致鼠标事件出现意外行为,因为当您期望父实例成为鼠标事件的目标对象时,作为子项添加的 Sprite 实例却可能成为目标对象。 要确保父实例用作鼠标事件的目标对象,您可以将父实例的 mouseChildren 属性设置为 false

设置此属性不会调度任何事件。 您必须使用 addEventListener() 方法才能创建交互式功能。


实现
    public function get mouseChildren():Boolean
    public function set mouseChildren(value:Boolean):void

另请参见


示例
如何使用示例

下例设置一个名为 container 的 Sprite 对象(一种显示对象容器),并显示:当您将该对象的 mouseChildren 属性设置为 false 时,mouseClick 事件的目标为 container 对象,而不是其子对象中的任一个:
import flash.display.Sprite;
import flash.events.MouseEvent;

var container:Sprite = new Sprite();
container.name = "container";
addChild(container);

var circle:Sprite = new Sprite();
circle.name = "circle";
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawCircle(40, 40, 40);

container.addChild(circle);

container.mouseChildren = false;

container.addEventListener(MouseEvent.CLICK, clicked);

function clicked(event:MouseEvent):void {
    trace(event.target.name); // container
}
numChildren属性 
numChildren:int  [read-only]

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

返回此对象的子项数目。


实现
    public function get numChildren():int

另请参见


示例
如何使用示例

下例设置了两个名称分别为 container1container2 的 Sprite 对象。 Sprite 是一种显示对象容器。 该示例调用 addChild() 方法以设置显示层次结构:container1container2 的子项,其它两个显示对象 circle1circle2container1 的子项。 trace() 方法的调用显示每个对象的子项数。 请注意,孙项并不包括在 numChildren 计数中:
import flash.display.Sprite;

var container1:Sprite = new Sprite();
var container2:Sprite = new Sprite();

var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFFCC00);
circle1.graphics.drawCircle(40, 40, 40);

var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00CCFF);
circle2.graphics.drawCircle(80, 40, 40);

container2.addChild(container1);
container1.addChild(circle1);
container1.addChild(circle2);

trace(container1.numChildren); // 2
trace(container2.numChildren); // 1
trace(circle1.numChildren); // 0
trace(circle2.numChildren); // 0
tabChildren属性 
tabChildren:Boolean  [read-write]

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

确定对象的子项是否支持 Tab 键。 为对象的子项启用或禁用 Tab 切换。 默认值为 true


实现
    public function get tabChildren():Boolean
    public function set tabChildren(value:Boolean):void

引发
IllegalOperationError — 调用 Stage 对象的此属性会引发异常。 Stage 对象未实现此属性。

示例
如何使用示例

下例创建一个 container1 显示对象容器,并将两个显示对象 circle1circle2 添加到其子级列表。 该示例将子项的 tabChildren 设置为 false,以便它可以使用 tabIndex 管理自己的 Tab 键顺序:
import flash.display.Sprite;

var container:Sprite = new Sprite();
container.tabChildren = false;

var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFFCC00);
circle1.graphics.drawCircle(40, 40, 40);
circle1.tabIndex = 1;

var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00CCFF);
circle2.graphics.drawCircle(120, 40, 40);
circle2.tabIndex = 0;

container.addChild(circle1);
container.addChild(circle2);
若要查看此示例的结果,请编译并运行文件。 当单击其中一个圆形时,可以按 Tab 键切换具有焦点(由亮黄色的矩形表示)的显示对象。
textSnapshot属性 
textSnapshot:TextSnapshot  [read-only]

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

返回此 DisplayObjectContainer 实例的 TextSnapshot 对象。


实现
    public function get textSnapshot():TextSnapshot

另请参见


示例
如何使用示例

下例只适用于 Flash 创作环境。 Flex 没有提供用于将静态文本添加到文件的任何方法。 若要为此示例准备 Flash 文件,请在影片的第一帧中添加一个或多个静态文本字段。 然后将以下脚本插入到第一帧并运行该文件。 输出将为所添加的静态文本:
trace(this.textSnapshot.getText(0, this.textSnapshot.charCount));
构造函数详细信息
DisplayObjectContainer()构造函数
public function DisplayObjectContainer()

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

调用 new DisplayObjectContainer() 构造函数会引发 ArgumentError 异常。 但是,可以调用 DisplayObjectContainer 的以下子类的构造函数:

方法详细信息
addChild()方法
public function addChild(child:DisplayObject):DisplayObject

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

将一个 DisplayObject 子实例添加到该 DisplayObjectContainer 实例中。 子项将被添加到该 DisplayObjectContainer 实例中其它所有子项的前(上)面。 (要将某子项添加到特定索引位置,请使用 addChildAt() 方法。)

如果添加一个已将其它显示对象容器作为父项的子对象,则会从其它显示对象容器的子列表中删除该对象。

参数

child:DisplayObject — 要作为该 DisplayObjectContainer 实例的子项添加的 DisplayObject 实例。

返回
DisplayObject — 在 child 参数中传递的 DisplayObject 实例。

事件
added:Event — 将显示对象添加到显示列表中时调度。

引发
ArgumentError — 在子项与父项相同时引发。 此外,当调用方是要添加的子项的子项(即孙项等) 时也会引发。

另请参见


示例
如何使用示例

下例设置了两个名称分别为 container1container2 的 Sprite 对象。 Sprite 是一种显示对象容器。 该示例调用 addChild() 方法以设置显示层次结构:container1container2 的子项,其它两个显示对象 circle1circle2container1 的子项。 trace() 方法的调用显示每个对象的子项数。 请注意,孙项并不包括在 numChildren 计数中:
import flash.display.Sprite;

var container1:Sprite = new Sprite();
var container2:Sprite = new Sprite();

var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFFCC00);
circle1.graphics.drawCircle(40, 40, 40);

var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00CCFF);
circle2.graphics.drawCircle(80, 40, 40);

container2.addChild(container1);
container1.addChild(circle1);
container1.addChild(circle2);

trace(container1.numChildren); // 2
trace(container2.numChildren); // 1
trace(circle1.numChildren); // 0
trace(circle2.numChildren); // 0
addChildAt()方法 
public function addChildAt(child:DisplayObject, index:int):DisplayObject

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

将一个 DisplayObject 子实例添加到该 DisplayObjectContainer 实例中。 该子项将被添加到指定的索引位置。 索引为 0 表示该 DisplayObjectContainer 对象的显示列表的后(底)部。

例如,下例在索引位置 0、2、1 处分别显示 a、b、c 三个显示对象:

如果添加一个已将其它显示对象容器作为父项的子对象,则会从其它显示对象容器的子列表中删除该对象。

参数

child:DisplayObject — 要作为该 DisplayObjectContainer 实例的子项添加的 DisplayObject 实例。
 
index:int — 添加该子项的索引位置。 如果指定当前占用的索引位置,则该位置以及所有更高位置上的子对象会在子级列表中上移一个位置。

返回
DisplayObject — 在 child 参数中传递的 DisplayObject 实例。

事件
added:Event — 将显示对象添加到显示列表中时调度。

引发
RangeError — 在子级列表中不存在索引位置时引发。
 
ArgumentError — 在子项与父项相同时引发。 此外,当调用方是要添加的子项的子项(即孙项等) 时也会引发。

另请参见


示例
如何使用示例

下例创建一个 container 显示对象容器,并将一个显示对象 circle1 添加到其显示列表。 然后,通过调用 container.addChildAt(circle2, 0),它将 circle2 对象添加到索引位置零(后面),并将 circle1 对象移动到索引位置 1:
import flash.display.Sprite;

var container:Sprite = new Sprite();

var circle1:Sprite = new Sprite();
var circle2:Sprite = new Sprite();

container.addChild(circle1);
container.addChildAt(circle2, 0);

trace(container.getChildAt(0) == circle2); // true
trace(container.getChildAt(1) == circle1); // true
areInaccessibleObjectsUnderPoint()方法 
public function areInaccessibleObjectsUnderPoint(point:Point):Boolean

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

指示安全限制是否会导致出现以下情况:在列表中忽略了使用指定 point 点调用 DisplayObjectContainer.getObjectsUnderPoint() 方法时返回的所有显示对象。 默认情况下,一个域中的内容无法访问另一个域中的对象,除非通过调用 Security.allowDomain() 方法而允许它们这样做。

有关详细信息,请参阅以下部分:

point 参数位于舞台的坐标空间中,此坐标空间可能与显示对象容器的坐标空间不同(除非显示对象容器是舞台)。 您可以使用 globalToLocal()localToGlobal() 方法在这些坐标空间之间转换点。

参数

point:Point — 要查看其下方内容的点。

返回
Boolean — 如果点包含具有安全限制的子显示对象,则为 true

另请参见


示例
如何使用示例

下面的代码将创建一个名为 container 的显示对象容器。 下一个代码块使用 Loader 对象从远程文件服务器中加载一个名为“test.jpg”的 JPEG 文件。 请注意,LoaderContext 对象的 checkPolicyFile 属性(在 load() 方法中用作参数)被设置为 false。 加载了该文件后,代码将调用 loaded() 方法,后者又将调用 container.areInaccessibleObjectsUnderPoint(),这将返回 true 值,因为假定被加载的内容来自无法访问的域:
import flash.display.Sprite;
import flash.display.Loader;
import flash.system.LoaderContext;
import flash.net.URLRequest;
import flash.events.Event;
import flash.geom.Point;

var container:Sprite = new Sprite();

var urlReq:URLRequest = new URLRequest("http://localhost/RemoteFile.swf");
var ldr:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = false;
ldr.load(urlReq, context);

ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, urlNotFound);

function loaded(event:Event):void {
    var pt:Point = new Point(1, 1);
    trace(container.areInaccessibleObjectsUnderPoint(pt)); // true
}

function urlNotFound(event:Event):void {
    trace("The URL was not found."); 
}
此示例假定由此代码生成的 SWF 文件从与 JPEG 文件不同的域中加载,并且加载的 JPEG 文件占用点 (1, 1)。
contains()方法 
public function contains(child:DisplayObject):Boolean

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

确定指定显示对象是 DisplayObjectContainer 实例的子项还是该实例本身。 搜索包括整个显示列表(其中包括此 DisplayObjectContainer 实例)。 孙项、曾孙项等,每项都返回 true

参数

child:DisplayObject — 要测试的子对象。

返回
Boolean — 如果 child 对象是 DisplayObjectContainer 的子项或容器本身,则为 true;否则为 false

示例
如何使用示例

下例设置若干 Sprite 对象并将其中一些对象添加到其它对象的子级列表。 (Sprite 对象是一种显示对象容器。) 通过调用 contains() 方法可显示各种对象之间的关系:
import flash.display.Sprite;

var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();
var sprite3:Sprite = new Sprite();
var sprite4:Sprite = new Sprite();

sprite1.addChild(sprite2);
sprite2.addChild(sprite3);

trace(sprite1.contains(sprite1)); // true
trace(sprite1.contains(sprite2)); // true
trace(sprite1.contains(sprite3)); // true
trace(sprite1.contains(sprite4)); // false
getChildAt()方法 
public function getChildAt(index:int):DisplayObject

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

返回位于指定索引处的子显示对象实例。

参数

index:int — 子对象的索引位置。

返回
DisplayObject — 位于指定索引位置处的子显示对象。

引发
RangeError — 在子级列表中不存在索引时引发。
 
SecurityError — 此子显示对象属于您无权访问的沙箱。 通过让子影片调用 Security.allowDomain(),可以避免发生这种情况。

另请参见


示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后将 3 个显示对象添加到 container 对象的子级列表。 getChildAt() 方法的调用随后会显示这些子对象的位置:
import flash.display.Sprite;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();
var sprite3:Sprite = new Sprite();

container.addChild(sprite1);
container.addChild(sprite2);
container.addChildAt(sprite3, 0);

trace(container.getChildAt(0) == sprite3); // true
trace(container.getChildAt(1) == sprite1); // true
trace(container.getChildAt(2) == sprite2); // true
getChildByName()方法 
public function getChildByName(name:String):DisplayObject

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

返回具有指定名称的子显示对象。 如果多个子显示对象具有指定名称,则该方法会返回子级列表中的第一个对象。

getChildAt() 方法比 getChildByName() 方法快。 getChildAt() 方法从缓存数组中访问子项,而 getChildByName() 方法则必须遍历链接的列表来访问子项。

参数

name:String — 要返回的子项的名称。

返回
DisplayObject — 具有指定名称的子显示对象。

引发
SecurityError — 此子显示对象属于您无权访问的沙箱。 通过让子影片调用 Security.allowDomain() 方法,可以避免发生这种情况。

另请参见


示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后向该容器中添加两个子显示对象。 然后,代码会调用 getChildByName()getChildIndex() 方法来返回具有 name "sprite1"container 对象子项的索引位置。
import flash.display.Sprite;
import flash.display.DisplayObject;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";

container.addChild(sprite1);
container.addChild(sprite2);

var target:DisplayObject = container.getChildByName("sprite1"); 
trace(container.getChildIndex(target)); // 0
getChildIndex()方法 
public function getChildIndex(child:DisplayObject):int

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

返回 DisplayObject 的 child 实例的索引位置。

参数

child:DisplayObject — 要标识的 DisplayObject 实例。

返回
int — 要标识的子显示对象的索引位置。

引发
ArgumentError — 在子参数不是此对象的子项时引发。

示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后向该容器中添加两个子显示对象。 然后,代码会调用 getChildByName()getChildIndex() 方法来返回具有 name "sprite1"container 对象子项的索引位置。
import flash.display.Sprite;
import flash.display.DisplayObject;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";

container.addChild(sprite1);
container.addChild(sprite2);

var target:DisplayObject = container.getChildByName("sprite1"); 
trace(container.getChildIndex(target)); // 0
getObjectsUnderPoint()方法 
public function getObjectsUnderPoint(point:Point):Array

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

返回对象的数组,这些对象位于指定点下,并且是该 DisplayObjectContainer 实例的子项(或孙子项,依此类推)。 返回的数组中将省略出于安全原因而无法访问的任何子对象。 若要确定此安全限制是否影响返回的数组,请调用 areInaccessibleObjectsUnderPoint() 方法。

point 参数位于舞台的坐标空间中,此坐标空间可能与显示对象容器的坐标空间不同(除非显示对象容器是舞台)。 您可以使用 globalToLocal()localToGlobal() 方法在这些坐标空间之间转换点。

参数

point:Point — 要查看其下方内容的点。

返回
Array — 一个对象数组,这些对象位于指定点下方,并且是该 DisplayObjectContainer 实例的子项(或孙项等等)。

另请参见


示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后向该容器中添加两个重叠的子显示对象。 然后,代码会调用 getObjectsUnderPoint() 两次(第一次使用只接触到一个对象的点,第二次使用对象的重叠点),然后返回数组的 length 显示容器中每个点上的对象数量:
import flash.display.Sprite;
import flash.geom.Point;

var container:Sprite = new Sprite();

var square1:Sprite = new Sprite();
square1.graphics.beginFill(0xFFCC00);
square1.graphics.drawRect(0, 0, 40, 40);

var square2:Sprite = new Sprite();
square2.graphics.beginFill(0x00CCFF);
square2.graphics.drawRect(20, 0, 30, 40);

container.addChild(square1);
container.addChild(square2);

var pt:Point = new Point(10, 20);
var objects:Array = container.getObjectsUnderPoint(pt); 
trace(objects.length); // 1

pt = new Point(35, 20);
objects = container.getObjectsUnderPoint(pt);
trace(objects.length);  // 2
removeChild()方法 
public function removeChild(child:DisplayObject):DisplayObject

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

从 DisplayObjectContainer 实例的子列表中删除指定的 child DisplayObject 实例。 将已删除子项的 parent 属性设置为 null;如果没有对该子项的任何其它引用,则将该对象作为垃圾回收。 DisplayObjectContainer 中该子项之上的任何显示对象的索引位置都减去 1。

垃圾回收器是 Flash Player 用来重新分配未使用的内存空间的进程。 当在某处变量或对象不再被主动地引用或存储时,垃圾回收器将清理其过去占用的内存空间,并且如果不存在对该变量或对象的任何其它引用,则擦除此内存空间。

参数

child:DisplayObject — 要删除的 DisplayObject 实例。

返回
DisplayObject — 在 child 参数中传递的 DisplayObject 实例。

引发
ArgumentError — 在子参数不是此对象的子项时引发。

示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后向该容器中添加两个子显示对象。 将事件侦听器添加到 container 对象,以便在用户单击容器的子对象时,removeChild() 方法从容器的子级列表中删除单击的子项:
 
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;

var container:Sprite = new Sprite();
addChild(container);

var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFFCC00);
circle1.graphics.drawCircle(40, 40, 40);

var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00CCFF);
circle2.graphics.drawCircle(120, 40, 40);

container.addChild(circle1);
container.addChild(circle2);

container.addEventListener(MouseEvent.CLICK, clicked);

function clicked(event:MouseEvent):void {
    container.removeChild(DisplayObject(event.target)); 
}
removeChildAt()方法 
public function removeChildAt(index:int):DisplayObject

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

从 DisplayObjectContainer 的子列表中指定的 index 位置删除子 DisplayObject。 将已删除子项的 parent 属性设置为 null;如果没有对该子项的任何其它引用,则将该对象作为垃圾回收。 DisplayObjectContainer 中该子项之上的任何显示对象的索引位置都减去 1。

垃圾回收器是 Flash Player 用来重新分配未使用的内存空间的进程。 当在某处变量或对象不再被主动地引用或存储时,垃圾回收器将清理其过去占用的内存空间,并且如果不存在对该变量或对象的任何其它引用,则擦除此内存空间。

参数

index:int — 要删除的 DisplayObject 的子索引。

返回
DisplayObject — 已删除的 DisplayObject 实例。

引发
SecurityError — 此子显示对象属于执行调用的对象无权访问的沙箱。 通过让子影片调用 Security.allowDomain() 方法,可以避免发生这种情况。
 
RangeError — 在子级列表中不存在索引时引发。

示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后向该容器中添加两个子显示对象。 然后代码会显示:当您调用 removeChildAt() 方法以删除最低索引位置 (0) 上的子项时,列表中的任何其它子对象将会下移一个位置:
import flash.display.Sprite;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";

container.addChild(sprite1);
container.addChild(sprite2);

trace(container.numChildren) // 2
container.removeChildAt(0); 
trace(container.numChildren) // 1
trace(container.getChildAt(0).name); // sprite2
setChildIndex()方法 
public function setChildIndex(child:DisplayObject, index:int):void

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

更改现有子项在显示对象容器中的位置。 这会影响子对象的分层。 例如,下例在索引位置 0、1、2 处分别显示 a、b、c 三个显示对象:

在使用 setChildIndex() 方法并指定一个已占用的索引位置时,子项列表中占用该位置的子对象以及更高位置的所有对象在该列表中将上移一个位置。 例如,如果上例中的显示对象容器名为 container,则可以通过调用以下代码来交换带有 a 和 b 标记的显示对象的位置:

container.setChildIndex(container.getChildAt(1), 0);

该代码产生以下对象排列:

参数

child:DisplayObject — 要为其更改索引编号的 DisplayObject 子实例。
 
index:int — 生成的 child 显示对象的索引编号。


引发
RangeError — 在子级列表中不存在索引时引发。
 
ArgumentError — 在子参数不是此对象的子项时引发。

另请参见


示例
如何使用示例

下例创建一个名为 container 的显示对象容器,然后向该容器添加三个稍微重叠的子显示对象。 当用户单击这些对象中的任何一个对象时,clicked() 方法调用 setChildIndex() 方法,将单击的对象移动到 container 对象的子级列表中最上面的位置:
import flash.display.Sprite;
import flash.events.MouseEvent;

var container:Sprite = new Sprite();
addChild(container);

var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFF0000);
circle1.graphics.drawCircle(40, 40, 40);
circle1.addEventListener(MouseEvent.CLICK, clicked);
 
var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00FF00);
circle2.graphics.drawCircle(100, 40, 40);
circle2.addEventListener(MouseEvent.CLICK, clicked);
 
var circle3:Sprite = new Sprite();
circle3.graphics.beginFill(0x0000FF);
circle3.graphics.drawCircle(70, 80, 40);
circle3.addEventListener(MouseEvent.CLICK, clicked);
 
container.addChild(circle1);
container.addChild(circle2);
container.addChild(circle3);
addChild(container);
 
function clicked(event:MouseEvent):void {
    var circle:Sprite = Sprite(event.target);
    var topPosition:uint = container.numChildren - 1;
    container.setChildIndex(circle, topPosition);
}
swapChildren()方法 
public function swapChildren(child1:DisplayObject, child2:DisplayObject):void

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

交换两个指定子对象的 Z 轴顺序(从前到后顺序)。 显示对象容器中所有其它子对象的索引位置保持不变。

参数

child1:DisplayObject — 第一个子对象。
 
child2:DisplayObject — 第二个子对象。


引发
ArgumentError — 在子参数不是此对象的子项时引发。

示例
如何使用示例

下例创建一个名为 container 的显示对象容器,接着向该容器添加两个子显示对象,然后显示调用 swapChildren() 方法的效果:
import flash.display.Sprite;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";

container.addChild(sprite1);
container.addChild(sprite2);

trace(container.getChildAt(0).name); // sprite1
trace(container.getChildAt(1).name); // sprite2

container.swapChildren(sprite1, sprite2);

trace(container.getChildAt(0).name); // sprite2
trace(container.getChildAt(1).name); // sprite1
swapChildrenAt()方法 
public function swapChildrenAt(index1:int, index2:int):void

语言版本 : ActionScript 3.0
Player 版本 : Flash Player 9

在子级列表中两个指定的索引位置,交换子对象的 Z 轴顺序(前后顺序)。 显示对象容器中所有其它子对象的索引位置保持不变。

参数

index1:int — 第一个子对象的索引位置。
 
index2:int — 第二个子对象的索引位置。


引发
RangeError — 如果子级列表中不存在任何一个索引。

示例
如何使用示例

下例创建一个名为 container 的显示对象容器,接着向该容器添加三个子显示对象,然后显示对 swapChildrenAt() 方法的调用如何重新排列该显示对象容器的子级列表:
import flash.display.Sprite;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";
var sprite3:Sprite = new Sprite();
sprite3.name = "sprite3";

container.addChild(sprite1);
container.addChild(sprite2);
container.addChild(sprite3);

trace(container.getChildAt(0).name); // sprite1
trace(container.getChildAt(1).name); // sprite2
trace(container.getChildAt(2).name); // sprite3

container.swapChildrenAt(0, 2);

trace(container.getChildAt(0).name); // sprite3
trace(container.getChildAt(1).name); // sprite2
trace(container.getChildAt(2).name); // sprite1
示例 如何使用示例

下例使用类 DisplayObjectContainerExample 连续创建五个橙色正方形。 执行下列步骤可完成该任务:
  1. 构造函数调用 configureAssets() 方法。
  2. configureAssets() 方法创建 childlastChild Sprite 对象。
  3. for 循环创建 5 个橙色正方形并依次放置它们。
  4. 每次创建 CustomSprite 对象时,其构造函数都调用 CustomSprite 对象的 draw() 方法,该方法通过调用 Graphics 类的 beginFill()drawRect()endFill() 方法创建 50 x 50 像素的正方形。 addChild() 将每个正方形添加到显示列表。

package {
    import flash.display.DisplayObject;
    import flash.display.Sprite;

    public class DisplayObjectContainerExample extends Sprite {
        private var gutter:uint     = 5;
        private var childCount:uint = 5;

        public function DisplayObjectContainerExample() {
            configureAssets();
        }

        private function configureAssets():void {
            var child:Sprite = new CustomSprite();
            var lastChild:Sprite = child;
            for (var i:uint = 1; i <= childCount; i++) {
                child = new CustomSprite();
                child.x = lastChild.x + lastChild.width + gutter;
                addChild(child);
                lastChild = child;
            }
        }
    }
}

import flash.display.Sprite;

class CustomSprite extends Sprite {
    private var size:uint = 50;
    private var bgColor:uint = 0xFFCC00;

    public function CustomSprite() {
        draw(size, size);
    }

    private function draw(w:uint, h:uint):void {
        graphics.beginFill(bgColor);
        graphics.drawRect(0, 0, w, h);
        graphics.endFill();
    }
}




 

评论添加到页面后给我发送电子邮件 | 评论报告

当前页: http://livedocs.adobe.com/flash/9.0_cn/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html