缩放容器不会影响孩子的宽度

问题描述

| 我已经添加了一个带有孩子(精灵)的容器(精灵)。 当我缩放容器时,我想获得一个孩子的新宽度,但是当我追踪孩子的宽度时,我总是得到原始尺寸,即使一切都在视觉上进行了缩放。 如何覆盖儿童宽度? 更新: 我只是发现children.graphics-\“ dimensions \”可能导致了问题。当我离开图形时,一切似乎正常。是否可以与子精灵同时缩放图形?     

解决方法

您可以使用
DisplayObject.transform.pixelBounds.width
DisplayObject.transform.pixelBounds.height
来获取子显示对象的宽度和高度,如下所示:
package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE,init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            var squaresXml:XML = 

            <squares>
                <square name=\"redSquare\" color=\"0xFF0000\" />
                <square name=\"blueSquare\" color=\"0x00FF00\" />
                <square name=\"greenSquare\" color=\"0x0000FF\" />
            </squares>;

            var size:Number = 100;

            var squares:Sprite = new Sprite();

            for (var i:uint = 0; i < squaresXml.children().length(); i++)
            {
                var square:Square = new Square(squaresXml.square[i].@color,size);
                square.name = squaresXml.square[i].@name;
                square.x = i * size;
                squares.addChild(square);

            }// end for

            addChild(squares);

            squares.scaleX = squares.scaleY = 2;

            var redSquare:Square = squares.getChildByName(\"redSquare\") as Square;
            trace(redSquare.width); // output: 100
            trace(redSquare.transform.pixelBounds.width); // output : 200;

        }// end function

    }// end class

}// end package

import flash.display.Shape;
import flash.display.Sprite;

internal class Square extends Shape
{
    public function Square(color:uint,size:Number)
    {
        graphics.beginFill(color);
        graphics.drawRect(0,size,size);
        graphics.endFill();

    }// end function

}// end class
DisplayObject.transform.pixelBounds
基本上得到\“一个
Rectangle
对象,该对象定义舞台上显示对象的边界矩形\”。     ,子级宽度将始终是原始的完整比例值。如果要获取其当前宽度,只需将该值乘以容器的缩放比例即可。 因此,例如,如果您的孩子的宽度为100像素,并且将容器缩放为一半大小:100像素* 0.5 = 50像素。或输入代码:
actualChildWidth = container.scaleX * child.width;
    ,我还建议您使用显示对象转换的ѭ6。这将返回一个2D转换矩阵,将对象的父链乘以根级别。 获得矩阵对象后,可以检查其
a
d
属性,以分别查看其根级x和y缩放因子。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...