6.3.向前或向后移动对象

问题
我要改变对象在屏幕上的显示顺序
解决办法
使用DisplayObectContainer 类的setChildIndex( ) 方法改变项目的位置,getChildIndex( ) 和getChildAt( ) 方法得到项目在显示列表中的位置
讨论
6.1 节和6.2 节介绍了可视化对象列表如何处理堆栈顺序( 深度), 基本上每个DisplayObjectContainer 实例都有一个子对象列表,列表中子对象的顺序代表在屏幕上的绘制顺序,每个子对象有个位置索引,范围从0到numChildren -1,这很像数组。位置0的对象绘制在最底层,该列表不会出现空位置,比如三个对象分别在0,1,2 (不会出现0,1,6).

DisplayObjectContainer的setChildIndex( ) 方法用来重新设置列表中对象的位置,它需要两个参数:对象引用和合法的位置。非法的位置会抛出RangeError 异常。

下面的例子创建了三个不同颜色的圆,蓝色最上面,现在用setChildIndex( ) 方法把蓝色的圆移动最底下去:
+展开
-ActionScript
package {
import flash.display.*;
public class SetChildIndexExample extends Sprite {
public function SetChildIndexExample( ) {
// 创建三个圆
var red:Shape = createCircle( 0xFF0000, 10 );
red.x = 10;
red.y = 20;
var green:Shape = createCircle( 0x00FF00, 10 );
green.x = 15;
green.y = 25;
var blue:Shape = createCircle( 0x0000FF, 10 );
blue.x = 20;
blue.y = 20;
addChild( red );
addChild( green );

addChild( blue );
// 把蓝色圆移到最底层
setChildIndex( blue, 0 );
}
public function createCircle( color:uint, radius:Number ):Shape {
var shape:Shape = new Shape( );
shape.graphics.beginFill( color );
shape.graphics.drawCircle( 0, 0, radius );
shape.graphics.endFill( );
return shape;
}
}
}

setChildIndex( ) 方法需要明确知道把子对象移到哪里,把它移到最底层就指定为0,把它移动到最顶层就指定为numChildren-1,但如果我要移动到某个对象之后呢?

举个例子,假设我有两个圆,一个绿色一个蓝色,事先我不知道他们的位置,现在我要把蓝色的移动到绿色的前面,setChildIndex( ) 就做不到了,解决的办法就是用getChildIndex( )方法获得对象的位置,然后用setChildIndex( )方法设置,getChildIndex( ) 方法只能返回队列中对象位置,否则就会抛出ArgumentError 异常

下面的例子创建了两个圆,绿色和蓝色的,getChildIndex( )得到绿色圆的位置来设置蓝色圆的位置:
+展开
-ActionScript
package {
import flash.display.*;
public class GetChildIndexExample extends Sprite {
public function GetChildIndexExample( ) {
// 创建圆
var green:Shape = createCircle( 0x00FF00, 10 );
green.x = 25;
green.y = 25;
var blue:Shape = createCircle( 0x0000FF, 20 );
blue.x = 25;
blue.y = 25;
addChild( green );

addChild( blue );
// 把蓝色圆放在绿色圆的后面
setChildIndex( blue, getChildIndex( green ) );
}
public function createCircle( color:uint, radius:Number ):Shape {
var shape:Shape = new Shape( );
shape.graphics.beginFill( color );
shape.graphics.drawCircle( 0, 0, radius );
shape.graphics.endFill( );
return shape;
}
}
}

如果对象a在b前,下面的代码直接把a移到b后面:
+展开
-ActionScript
setChildIndex( a, getChildIndex( b ) );

如果a在b后面,上面的代码就把a移到b前面了。

如果事先没获得子对象引用的话就只能用getChildAt( ) 方法了。

getChildAt( ) 方法需要一个参数既列表子对象位置,返回该对象引用,如果给出的位置不合法则抛出RangeError 异常。
下面的例子创建了多个各种颜色,不同大小不同位置的圆,每次鼠标点击最底层的子对象就会移到最上层:
+展开
-ActionScript
package {
import flash.display.*;
import flash.events.*;
public class GetChildAtExample extends Sprite {
public function GetChildAtExample( ) {
var color:Array = [ 0xFF0000, 0x990000, 0x660000, 0x00FF00,
0x009900, 0x006600, 0x0000FF, 0x000099,
0x000066, 0xCCCCCC ];
for ( var i:int = 0; i < 10; i++ ) {
var circle:Shape = createCircle( color[i], 10 );
circle.x = i;

circle.y = i + 10; // the + 10 adds padding from the top
addChild( circle );
}
stage.addEventListener( MouseEvent.CLICK, updateDisplay );
}
public function updateDisplay( event:MouseEvent ):void {
setChildIndex( getChildAt(0), numChildren - 1 );
}
public function createCircle( color:uint, radius:Number ):Shape {
var shape:Shape = new Shape( );
shape.graphics.beginFill( color );
shape.graphics.drawCircle( 0, 0, radius );
shape.graphics.endFill( );
return shape;
}
}
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(110)喜欢(0)flash/flex/fcs/AIR