使用图形(例如)方法而不给实例变量添加前缀

发布于 2024-11-17 17:52:39 字数 456 浏览 4 评论 0原文

我是 AS3 和 Haxe 的新手,但希望找到一种方法来使用最终类(图形)中的方法,而不必总是添加实例变量前缀。

而不是这样的东西:

var gr:Graphics = flash.Lib.current.graphics;

gr.clear();
gr.beginFill(0xffffff, 1);
gr.drawRect(0,0,400,400);

我希望得到像processing.org一样工作的东西,但我想那里的很多便利来自于预处理。我查看了关于高级类型的Haxe参考,但我无法做任何事情工作到目前为止。这可能是不可能的,因为图形是最终的,但我认为问一下也没什么坏处。如果我能扩展 Graphics 的话,似乎会很容易。不管怎样,感谢您的阅读。

I'm new to AS3 and Haxe, but was hoping to find a way to use methods from a final class (Graphics) without always prefixing an instance variable.

Instead of something like this:

var gr:Graphics = flash.Lib.current.graphics;

gr.clear();
gr.beginFill(0xffffff, 1);
gr.drawRect(0,0,400,400);

I was hoping to get something that works like processing.org, but I guess a lot of the convenience there comes from the preprocessing. I looked at the Haxe reference on advanced types, but I haven't been able to make anything work so far. This probably isn't possible since Graphics is final, but I thought it wouldn't hurt to ask. It seems it would be easy if I could extend Graphics. Anyway, thanks for reading.

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

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

发布评论

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

评论(3

霓裳挽歌倾城醉 2024-11-24 17:52:40

使用“with”关键字有帮助吗?

var someSprite:Sprite = new Sprite();
with( someSprite.graphics )
{
    beginFill(0xC0FFEE, 1);
    drawRect( -10, -10, 20, 20 );
    endFill();
}

Does using the 'with' keyword help any?

var someSprite:Sprite = new Sprite();
with( someSprite.graphics )
{
    beginFill(0xC0FFEE, 1);
    drawRect( -10, -10, 20, 20 );
    endFill();
}
丿*梦醉红颜 2024-11-24 17:52:40

好的,这是在 Haxe 上实现非常简单的 with() {...} 模拟的代码:

//simple.hx
class Simple 
{

    @:macro public static function with(subject:Expr, block:Expr)
    {
        return with_impl(subject, block);
    }

    #if macro
    static function with_impl(subject:Expr, block:Expr)
    {
        function mk(e, pos) return { expr:e, pos:pos };

        //this is the main function here. It's going to find a variable  the expression so it uses our subject
        function changeIdent(identExpr:Expr, changeExpr)
        {
            return switch(identExpr.expr)
            {
                case EConst(c):
                switch(c)
                {
                    case CIdent(s):
                    mk(EField(changeExpr, s), identExpr.pos);

                    default:
                    identExpr;
                }

                case EField(e, f):
                mk(EField(changeIdent(e, changeExpr), f), identExpr.pos);

                case EType(e, f):
                mk(EType(changeIdent(e, changeExpr), f), identExpr.pos);

                default: //fallba
                identExpr;
            }
        }

        return switch(block.expr)
        {
            case EBlock(exprs):
            var newblock = [];
            for (statement in exprs)
            {
                switch(statement.expr)
                {
                    case ECall(e, params):
                    newblock.push(mk(ECall(changeIdent(e, subject), params), statement.pos));
                    default:
                    newblock.push(statement);
                }
            }

            mk(EBlock(newblock), block.pos);

            case EDisplay(e, iscall):
            mk(EDisplay(with_impl(subject, e), iscall), block.pos);

            default:
            changeIdent(block, subject);
        }
    }
    #end
}

您可以像这样使用它:

//Main.hx
class Main 
{

    static function main() 
    {
        Simple.with (Lib.current.graphics,
        {
            beginFill(0xC0FFEE, 1);
            drawRect( -10, -10, 20, 20 );
            endFill();
        });
    }

}

它不会更改范围,而是查找调用表达式并添加每个表达式中的函数(主题)的第一个参数。所以上面的代码就相当于:

{
    Lib.current.graphics.beginFill(0xC0FFEE, 1);
    Lib.current.graphics.drawRect( -10, -10, 20, 20 );
    Lib.current.graphics.endFill();
}

宏真好玩!

Ok, here's the code for implementing a very simple with() {...} emulation on Haxe:

//simple.hx
class Simple 
{

    @:macro public static function with(subject:Expr, block:Expr)
    {
        return with_impl(subject, block);
    }

    #if macro
    static function with_impl(subject:Expr, block:Expr)
    {
        function mk(e, pos) return { expr:e, pos:pos };

        //this is the main function here. It's going to find a variable  the expression so it uses our subject
        function changeIdent(identExpr:Expr, changeExpr)
        {
            return switch(identExpr.expr)
            {
                case EConst(c):
                switch(c)
                {
                    case CIdent(s):
                    mk(EField(changeExpr, s), identExpr.pos);

                    default:
                    identExpr;
                }

                case EField(e, f):
                mk(EField(changeIdent(e, changeExpr), f), identExpr.pos);

                case EType(e, f):
                mk(EType(changeIdent(e, changeExpr), f), identExpr.pos);

                default: //fallba
                identExpr;
            }
        }

        return switch(block.expr)
        {
            case EBlock(exprs):
            var newblock = [];
            for (statement in exprs)
            {
                switch(statement.expr)
                {
                    case ECall(e, params):
                    newblock.push(mk(ECall(changeIdent(e, subject), params), statement.pos));
                    default:
                    newblock.push(statement);
                }
            }

            mk(EBlock(newblock), block.pos);

            case EDisplay(e, iscall):
            mk(EDisplay(with_impl(subject, e), iscall), block.pos);

            default:
            changeIdent(block, subject);
        }
    }
    #end
}

you use it like this:

//Main.hx
class Main 
{

    static function main() 
    {
        Simple.with (Lib.current.graphics,
        {
            beginFill(0xC0FFEE, 1);
            drawRect( -10, -10, 20, 20 );
            endFill();
        });
    }

}

Instead of changing the scope, it will look for call expressions and just add the first argument of the function (subject) to be in every expression. So the code above is equivalent to:

{
    Lib.current.graphics.beginFill(0xC0FFEE, 1);
    Lib.current.graphics.drawRect( -10, -10, 20, 20 );
    Lib.current.graphics.endFill();
}

macros are so fun!

初心 2024-11-24 17:52:39

使用时可以尝试使用mixin

例如创建一个 GraphicHelper 类:

class GraphicHelper {
    inline public static function drawRect(sp:Sprite, x:Float, y:Float, w:Float, h:Float) {
        sp.graphics.drawRect(x,y,w,h);
    }
}

然后在你的 Sprite 类中:

using GraphicHelper;

class Square extends flash.display.Sprite {
    public function new():Void {
        super();
        drawRect(0,0,10,10); //this is GraphicHelper.drawRect(this,0,0,10,10); and since it is inline, actually is this.graphics.drawRect(0,0,10,10);
    }
}

Use can try using mixin.

For example create a class GraphicHelper:

class GraphicHelper {
    inline public static function drawRect(sp:Sprite, x:Float, y:Float, w:Float, h:Float) {
        sp.graphics.drawRect(x,y,w,h);
    }
}

And then in your Sprite class:

using GraphicHelper;

class Square extends flash.display.Sprite {
    public function new():Void {
        super();
        drawRect(0,0,10,10); //this is GraphicHelper.drawRect(this,0,0,10,10); and since it is inline, actually is this.graphics.drawRect(0,0,10,10);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文