Flex - 创建新的基元

发布于 2024-11-01 04:51:17 字数 1937 浏览 0 评论 0原文

首先,最好说的是我对 Flex / OOP 总体来说是新手。我一直在尝试添加一个基于 StrokedElement 的自定义类,以实现一个简单的网格(不像现有的 Flex 网格 - 这只是用于显示 - 不保存元素等...)

如下所示:

package ui.helpers
{
    import flash.display.Graphics;

    import spark.primitives.supportClasses.StrokedElement;

    public class SGrid extends StrokedElement
    {
        public function SGrid()
        {
            super();
        }

        private var _gridSize:Number;
        [Inspectable(category="General", minValue="1.0")]

        public function get gridSize():Number 
        {
            return _gridSize;
        }

        public function set gridSize(value:Number):void
        {        
            if (value != _gridSize)
            {
                _gridSize = value;
                invalidateSize();
                invalidateDisplayList();
                invalidateParentSizeAndDisplayList();
            }
        }

        override protected function draw(g:Graphics):void {

            for(var x:int; x < width; x+= _gridSize) {
                g.moveTo(x,0);
                g.lineTo(x,height);
            }
            for(var y:int; y < height; y+= _gridSize) {
                g.moveTo(0,y);
                g.lineTo(width,y);
            }

        }

    }
} 

我当前的类 是从 Flex Spark.primatives.rect 抄袭的 - 一切正常 - 但是当我将它添加到我的应用程序中时,我希望这样做:

<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
        </s:stroke>
    </helpers:SGrid>

但实际上这有效:

<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
        <helpers:stroke>
            <s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
        </helpers:stroke>
    </helpers:SGrid>

如果我使用 s:lines ,那么我会收到错误。显然我很高兴它有效 - 但我试图理解为什么这里有差异?

To start - its best to say im new to Flex / OOP in general. I have been trying to add a custom class based on StrokedElement in order to implement a simple grid (not like the existing Flex Grids - this would just be for display - not holding elements etc...)

My current class looks like this:

package ui.helpers
{
    import flash.display.Graphics;

    import spark.primitives.supportClasses.StrokedElement;

    public class SGrid extends StrokedElement
    {
        public function SGrid()
        {
            super();
        }

        private var _gridSize:Number;
        [Inspectable(category="General", minValue="1.0")]

        public function get gridSize():Number 
        {
            return _gridSize;
        }

        public function set gridSize(value:Number):void
        {        
            if (value != _gridSize)
            {
                _gridSize = value;
                invalidateSize();
                invalidateDisplayList();
                invalidateParentSizeAndDisplayList();
            }
        }

        override protected function draw(g:Graphics):void {

            for(var x:int; x < width; x+= _gridSize) {
                g.moveTo(x,0);
                g.lineTo(x,height);
            }
            for(var y:int; y < height; y+= _gridSize) {
                g.moveTo(0,y);
                g.lineTo(width,y);
            }

        }

    }
} 

Which is cribbed from the Flex spark.primatives.rect - everything works OK - but when I add it to my application I would expect to do this:

<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
        </s:stroke>
    </helpers:SGrid>

but in actual fact this works instead:

<helpers:SGrid id="gridOne" width="100" height="200" gridSize="10">
        <helpers:stroke>
            <s:SolidColorStroke color="0xCCCCCC" alpha="0.8" />
        </helpers:stroke>
    </helpers:SGrid>

If I use the s:stroke then I get errors. Obviously i'm pleased it works - but i'm trying to understand why the difference here?

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

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

发布评论

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

评论(1

黎歌 2024-11-08 04:51:17

这与类的声明名称空间有关。

SGridhelpers: 命名空间的一部分,而不是 s: 命名空间。

因此,在设置其属性时,您需要通过 helpers: 命名空间引用该属性。

该属性本身是在 SGrid 的基类上声明的(在您的例子中为 StrokedElement),这并不重要,它是 SGrid 实例的属性。

它与以下内容相同:

var grid:SGrid = new SGrid();
grid.stroke = new SolidColourStroke(); 

即使在基类上声明了描边,您也可以通过 SGrid 类引用它。

It's to do with the declared namespace of the class.

SGrid is part of the helpers: namespace, not the s: namespace.

Therefore, when setting it's properties you need to reference the property via the helpers: namespace.

It doesn't matter that the property itself was declared on a baseclass of SGrid (in your case, StrokedElement), it's a property of the instance of SGrid.

It's the same as:

var grid:SGrid = new SGrid();
grid.stroke = new SolidColourStroke(); 

Even though stroke is declared on the base class, you reference it through the SGrid class.

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