有多种方法调整 Rectangle 对象的大小和进行重新定位。
您可以通过更改 Rectangle 对象的
x
和
y
属性直接重新定位该对象。此更改不会影响 Rectangle 对象的宽度或高度。
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.x = 20;
rect1.y = 30;
trace(rect1); // (x=20, y=30, w=100, h=50)
如下面的代码所示,当更改 Rectangle 对象的
left
或
top
属性时,将重新定位该矩形。矩形的
x
和
y
属性分别与
left
和
top
属性匹配。然而,Rectangle 对象的左下角的位置不发生改变,因此调整了该对象的大小。
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.left = 20;
rect1.top = 30;
trace(rect1); // (x=20, y=30, w=80, h=20)
同样,如下面的示例所示,如果更改 Rectangle 对象的
bottom
或
right
属性,该对象左上角的位置不发生改变。矩形相应地调整了大小:
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.right = 60;
trect1.bottom = 20;
trace(rect1); // (x=0, y=0, w=60, h=20)
也可以使用
offset()
方法重新定位 Rectangle 对象,如下所示:
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.offset(20, 30);
trace(rect1); // (x=20, y=30, w=100, h=50)
offsetPt()
方法工作方式类似,只不过它是将 Point 对象作为参数,而不是将
x
和
y
偏移量值作为参数。
还可以使用
inflate()
方法调整 Rectangle 对象的大小,该方法包含两个参数,
dx
和
dy
。
dx
参数表示矩形的左侧和右侧距离中心的像素数。
dy
参数表示矩形的顶部和底部距离中心的像素数:
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.inflate(6,4);
trace(rect1); // (x=-6, y=-4, w=112, h=58)
inflatePt()
方法作方式类似,只不过它是将 Point 对象作为参数,而不是将
dx
和
dy
的值作为参数。