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 は final であるため、これはおそらく不可能ですが、質問しても問題ないと思いました。Graphics を拡張できれば簡単そうです。とにかく、読んでくれてありがとう。

4

3 に答える 3

1

使い方は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);
    }
}
于 2011-06-28T22:59:55.173 に答える
0

with() {...}わかりました、 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
}

次のように使用します。

//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();
}

マクロはとても楽しいです!

于 2011-07-01T01:11:50.663 に答える