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

发布于 2024-11-10 02:00:29 字数 204 浏览 3 评论 0原文

我有一个容器(精灵),其中添加了子元素(精灵)。 当我缩放容器时,我想获得子级的新宽度,但是当我追踪子级的宽度时,我总是得到原始尺寸,即使所有内容都在视觉上按其应有的比例缩放。 如何覆盖children-width?

更新: 我刚刚发现children.graphics-“dimensions”可能导致了这个问题。当我离开图形时,一切似乎都很好。是否可以与子精灵同时缩放图形?

I've got a container(sprite) with children(sprites) added to it.
When I scale the container, I would like to get the new width of a child, but when I trace the children's width I always get the original dimensions, even though everything gets visually scaled as it should.
How do I overwrite the children-width ?

Update:
I just found out that the children.graphics-"dimensions" could have caused the problem. When I leave the graphics, everything seems ok. Is it possible to scale the graphics at same time as the child-sprite?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清泪尽 2024-11-17 02:00:29

您可以使用 DisplayObject.transform.pixelBounds.widthDisplayObject.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, 0, size, size);
        graphics.endFill();

    }// end function

}// end class

DisplayObject.transform.pixelBounds 基本上获取“一个定义舞台上显示对象的边界矩形的 Rectangle 对象”。

You can use DisplayObject.transform.pixelBounds.width or DisplayObject.transform.pixelBounds.height to get the width and height of the child display objects like in the following:

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, 0, size, size);
        graphics.endFill();

    }// end function

}// end class

DisplayObject.transform.pixelBounds basically gets "a Rectangle object that defines the bounding rectangle of the display object on the stage".

新一帅帅 2024-11-17 02:00:29

子级宽度始终是原始的全尺寸值。如果你想获得它们的当前宽度,只需将该值乘以容器的缩放比例即可。

例如,如果您的子宽度为 100 像素,并且您将容器缩放到一半大小:100 像素 * 0.5 = 50 像素。或者使用代码:actualChildWidth = container.scaleX * child.width;

The children width is always going to be what the original, full scale values are. If you want to get their current width just multiply the value with the scaling ratio of the container.

So for example if your child width is 100 pixels and you scale the container to half size: 100 pixels * 0.5 = 50 pixels. Or with code: actualChildWidth = container.scaleX * child.width;

娇纵 2024-11-17 02:00:29

我还建议使用显示对象转换的concatenatedMatrix。这将返回一个二维变换矩阵乘以对象的父链到根级别。

获得生成的 Matrix 对象后,您可以检查其 ad 属性以分别查看其根级 x 和 y 缩放因子。

I would also recommend using the concatenatedMatrix of your display object's transform. That will return a 2D transformation matrix multiplied up the object's parent chain to the root level.

Once you have the resulting Matrix object, you can inspect its a or d property to see its root-level x and y scaling factors respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文