enumsflagshaxeenum-flagsnme

Haxe - sending enum as flags to a function


I'm just trying to convert my code from C# to Haxe NME. I use enums as flags.

[Flags]
enum State
{
    StateOne    = 1,
    StateTwo    = 2,
    StateThree  = 4
}

And use it

if (someObj.HasState(State.StateOne | State.StateTwo))
{
    // Contains both the states. Do something now.
}

I had no idea on how to do this in Haxe NME.

Thanks.


Solution

  • I've managed to find it. I had trouble using enums but I had been successful using constants. This is the simple test file I used.

    package ;
    
    class FlagsTest
    {
    
        static inline var FLG_1:Int = 1;
        static inline var FLG_2:Int = 2;
    
        public static function main() : Void
        {
            var flag:Int = FLG_1;
            if (hasFlag(flag, FLG_1))
            {
                trace ("Test 1 passed");
            }
            flag |= FLG_2;
            if (hasFlag(flag, FLG_2))
            {
                trace ("Test 2 passed");
            }
        }
    
        public static function hasFlag( flags:Int, flag:Int ) : Bool
        {
            return ((flags & flag) == flag) ? true : false;
        }
    
    }
    

    Output:

    FlagsTest.hx line 14: Test 1 passed
    FlagsTest.hx line 19: Test 2 passed