actionscript-3 - AS3 MovieClip getRealBounds

标签 actionscript-3 movieclip bounds

众所周知,在 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/

相关文章:

actionscript-3 - 通过 Actionscript 3.0 使用 HTTP POST 上传 zip 文件

flash - 如何从 Flash 中的 MovieClips 获取多边形?

actionscript-3 - AS3 使用蒙版从 Movieclip 获取位图

ios - 动态获取imageview的位置

正则表达式检查字符串中的前 2 个字符是否为字母

actionscript-3 - 如何无缝循环FLV

actionscript-3 - 如何将MovieClip转换为bitmapData

ios - 使用另一个 UIImageView 的 UIImageViews 坐标范围

java - 在 java 中获取字符串大小(没有可用的 Graphics 对象)

apache-flex - Flex、AIR : Search for . pdf 文件位于指定文件夹中