My shape tween isn't reporting its size correctly
If I shape tween between two rectangles like so:
On frame 5 I have the following:
trace(width);
trace(height);
trace(getRect(this));
trace(getBoun开发者_运维百科ds(this));
I get back
49
49
(x=45, y=21, w=48, h=48)
(x=44.5, y=20.5, w=49, h=49)
All wrong! These would be right for frame 1 but the rectangle is much wider than this by frame 5. The shape tween seems to be confusing the results.
How can I get the actual rectangle of frame 5 without adding keyframes all across the timeline?
I made a post about how to get the bounds of a display Object, even with tranparent areas here on my blog. It explains the code below. http://plasticsturgeon.com/2010/09/as3-get-visible-bounds-of-transparent-display-object/
package util
{
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
public function getVisibleBounds(source:DisplayObject):Rectangle
{
// Updated 11-18-2010;
// Thanks to Mark in the comments for this addition;
var matrix:Matrix = new Matrix()
matrix.tx = -source.getBounds(null).x;
matrix.ty = -source.getBounds(null).y;
var data:BitmapData = new BitmapData(source.width, source.height, true, 0x00000000);
data.draw(source, matrix);
var bounds : Rectangle = data.getColorBoundsRect(0xFFFFFFFF, 0x000000, false);
data.dispose();
return bounds;
}
}
If you feed in the display Object that contains you shape tween, you will get accurate with and height as well as corner bounds.
Try multiplying by the scale to get the real width and height:
var realWidth:Number = scaleX * width;
var realHeight:Number = scaleY * height;
Edit:
How about calculating the dimensions yourself?
// Values roughly based on your screenshot, but you might need to adjust them
var startWidth = 10;
var endWidth = 100;
var startFrame = 5;
var endFrame = 10;
var currentWidth = startWidth + ((startFrame - currentFrame) / endFrame) * (endWidth - startWidth);
// Then the same for currentHeight
精彩评论