0

I met something very strange and i can't understand why it happens. I make this enum :

 [Flags] public enum EnumMoveCommand { None = 0x0, Up = 0x1, Right = 0x2, Bottom = 0x4, Left = 0x8, LeftClick = 0x16, RightClick = 0x32, Vertical = Up | Bottom, Horizontal = Left | Right, Move = Up | Right | Left | Bottom } 

So i can use it like this :

 if ((commands & EnumMoveCommand.Left) != EnumMoveCommand.None) { MoveToDo.X -= this.speed.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } if ((commands & EnumMoveCommand.Right) != EnumMoveCommand.None) { MoveToDo.X += this.speed.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } if ((commands & EnumMoveCommand.Up) != EnumMoveCommand.None) { MoveToDo.Y -= this.speed.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } if ((commands & EnumMoveCommand.Bottom) != EnumMoveCommand.None) { MoveToDo.Y += this.speed.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } if ((commands & EnumMoveCommand.Horizontal) != EnumMoveCommand.None && (commands & EnumMoveCommand.Vertical) != EnumMoveCommand.None) { MoveToDo.X = (float)(Math.Cos(45) * MoveToDo.X); MoveToDo.Y = (float)(Math.Sin(45) * MoveToDo.Y); } 

But RightClick with value 0x32 doesn't work, for example :

((EnumMoveCommand.RightClick & EnumMoveCommand.Right) != EnumMoveCommand.None)=true 

How 0x32 & 0x2 != 0x0 ?

Thanks

EDIT

Okei so it s hex and not dec, now here is my code who works :

 None = 0x0, Up = 0x1, Right = 0x2, Bottom = 0x4, Left = 0x8, LeftClick = 0x10, RightClick = 0x20, Vertical = Up | Bottom, Horizontal = Left | Right, Move = Up | Right | Left | Bottom 

Thanks all

EDIT 2

 [Flags] public enum EnumMoveCommand { None = 0, Up = 1<<0, //1 Right = 1<<1, //2 Bottom = 1<<2, //4 Left = 1<<3, //8 LeftClick = 1<<4, //16 RightClick = 1<<5, //32 Vertical = Up | Bottom, Horizontal = Left | Right, Move = Up | Right | Left | Bottom } 

Is better, thanks kalten

4
  • 0x16 and 0x32 are a bad sort of flags. One is 22, another is 50. You probably meant 0x10 and 0x20. Commented Mar 12, 2016 at 22:19
  • 0x represents hex not dec so 0x16 != 16 dec, it is 0x10. Commented Mar 12, 2016 at 22:24
  • okey thanks ! I was 100% wrong :/ Commented Mar 12, 2016 at 22:31
  • If kaltens answer helped you solve your problem please accept it as an answer. Commented Mar 12, 2016 at 22:57

1 Answer 1

1

EnumMoveCommand.RightClick = 0x32 = 110010

EnumMoveCommand.Right = 0x2 = 000010

110010 & 000010 = 000010

So ((EnumMoveCommand.RightClick & EnumMoveCommand.Right) != EnumMoveCommand.None)==true

If you want avoid conflic between your enum values you can use the << operator.

[Flags] public enum EnumMoveCommand { None = 0, Up = 1<<0, //1 Right = 1<<1, //2 Bottom = 1<<2, //4 Left = 1<<3, //8 LeftClick = 1<<4, //16 RightClick = 1<<5, //32 Vertical = Up | Bottom, Horizontal = Left | Right, Move = Up | Right | Left | Bottom } 

By the way you could use the HasFlag function like :

commands.HasFlag(EnumMoveCommand.RightClick) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.