众所周知,在 as3 中我们有一个 getBounds() 方法,它返回我们想要的 DisplayObject 容器中影片剪辑的精确尺寸和坐标。
事实上,这些数据是根据调用 getBounds() 时所在帧的 MC 中的图形状态计算的。
我想要的是真实的边界矩形,即整个动画影片剪辑将在其容器中占据的较大矩形。
我想到了两种办法:
1 - 一个我不知道的flash内置方法
2 - 遍历每一帧总是得到边界并最终返回最大的(但是如果它是一个很长的动画怎么办?我应该等待它完全播放才能得到我想要的东西吗?)
我希望我已经说清楚了。如果您需要示例,请告诉我!
最佳答案
您可以迭代每一帧,而不必等待动画播放:
假设您的剪辑名为 bob
:
var lifetimeBounds:Rectangle = new Rectangle();
bob.gotoAndStop(1);
for(var i:int=1;i<=bob.totalFrames;i++){
lifetimeBounds.width = Math.max(lifetimeBounds.width, bob.width);
lifetimeBounds.height = Math.max(lifetimeBounds.height, bob.height);
lifetimeBounds.x = Math.min(lifetimeBounds.x, bob.x);
lifetimeBounds.y = Math.min(lifetimeBounds.y, bob.y);
bob.nextFrame();
}
bob.gotoAndStop(1); //reset bob back to the beginning
这会增加 CPU 负担(因此,如果上述方法适合您的情况,我建议不要使用它),但您也可以在上面的示例中使用 getBounds()
并将返回的矩形与lifeBounds 矩形:
var tempRect:Rectangle;
var lifetimeBounds:Rectangle = new Rectangle();
bob.gotoAndStop(1);
for(var i:int=1;i<=bob.totalFrames;i++){
tmpRect = bob.getBounds(this);
lifetimeBounds.width = Math.max(lifetimeBounds.width, tempRect.width);
lifetimeBounds.height = Math.max(lifetimeBounds.height, tempRect.height);
lifetimeBounds.x = Math.min(lifetimeBounds.x, tempRect.x);
lifetimeBounds.y = Math.min(lifetimeBounds.y, tempRect.y);
bob.nextFrame();
}
关于actionscript-3 - AS3 MovieClip getRealBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13747077/