   How to animate buttons (sprite)?
  I'm writing a GUI system in XNA using Lua, and so far everything is working correctly, except for animations. They refuse to play whenever I handle an event (a button being clicked in this case). I thought maybe running the animation in a separate thread would work, but it does not.Here is how I handle a click:   public void Update()   {  LuaInterfaceManager.CallFunction("Update");  //TODO: Check if a button is clicked, and call the Lua function ButtonHandler()  m_CurrentMouseState = Mouse.GetState();  foreach (UIButton Button in m_Buttons)  { if (m_CurrentMouseState.X >= Button.X && m_CurrentMouseState.X <= (Button.X + Button.Width) &&m_CurrentMouseState.Y > Button.Y && m_CurrentMouseState.Y < (Button.Y + Button.Texture.Height)) {if (m_CurrentMouseState.LeftButton == ButtonState.Pressed &&    m_PreviousMouseState.LeftButton == ButtonState.Released){    ThreadPool.QueueUserWorkItem(new WaitCallback(NextFrame), Button);    LuaInterfaceManager.CallFunction("ButtonHandler", Button);} }  }  m_PreviousMouseState = Mouse.GetState();   }   private static void NextFrame(object State)   {  UIButton Btn = (UIButton)State;  Btn.CurrentFrame++;   }The funny thing is that when I click a button that is not assigned to an 'event' in Lua, the frame is increased (and you can see it). I'm thinking in order to see the animation before the event is triggered, I need to pause the update thread.I tried puttingThread.Sleep(10)after ThreadPool.QueueUserWorkItem(new WaitCallback(NextFrame), Button);but it didn't seem to have any effect besides making things somewhat more unresponsive.Here's what a spritesheet looks like:and here's how I render a button:  foreach (UIButton Button in m_Buttons)  { SBatch.Draw(Button.Texture, new Vector2(Button.X, Button.Y),new Rectangle(Button.CurrentFrame, 0, Button.Width, Button.Texture.Height), Color.White);   }I tried searching for some examples on Google, but I couldn't really find what I was looking for. Does anyone have an idea?Edit: Here's how I handle a click in Lua:function ButtonHandler(Button)--Button.CurrentFrame = Button.CurrentFrame + 1if Button.StrID == "ExitButton" thenLuaFunctions.ApplicationExit(0)elseif Button.StrID == "CreditsButton" thenCreditsScreen = UIScreen(ScreenManager, "uigraphics\\credits\\credits.dat")ScreenManager:RemoveScreen(PersonSelectionScreen)ScreenManager:AddScreen(CreditsScreen, "gamedata\\luascripts\\credits.lua")elseif Button.StrID == "CreateButton1" thenCASScreen = UIScreen(ScreenManager, "uigraphics\\personselection\\personselection.dat")ScreenManager:RemoveScreen(PersonSelectionScreen)ScreenManager:AddScreen(CASScreen, "gamedata\\luascripts\\personselectionedit.lua")endend
  Ok, so I found out I was doing something wrong in my calculations, for starters.I changed the accessor for CurrentFrame from:   public int CurrentFrame   {  get  { if (m_CurrentFrame == 0)return 0; elsereturn m_Width / m_CurrentFrame;  }  set  { //Frames go from 0 to 3. if(value < 4) {m_CurrentFrame = value; }  }   }to   public int CurrentFrame   {  get  { if (m_CurrentFrame == 0)return 0; elsereturn m_Texture.Width / m_CurrentFrame;  }  set  { //Frames go from 0 to 3. if(value < 4) {m_CurrentFrame = value; }  }   }I figured I would change to Frame number 2 when hovering over a button, so I did:   public void Update()   {  LuaInterfaceManager.CallFunction("Update");  //TODO: Check if a button is clicked, and call the Lua function ButtonHandler()  m_CurrentMouseState = Mouse.GetState();  foreach (UIButton Button in m_Buttons)  { if (m_CurrentMouseState.X >= Button.X && m_CurrentMouseState.X <= (Button.X + Button.Width) &&m_CurrentMouseState.Y > Button.Y &&m_CurrentMouseState.Y < (Button.Y + Button.Texture.Height)) {Button.CurrentFrame = 2;if (m_CurrentMouseState.LeftButton == ButtonState.Pressed &&    m_PreviousMouseState.LeftButton == ButtonState.Released){    Button.CurrentFrame--;    LuaInterfaceManager.CallFunction("ButtonHandler", Button);} } elseButton.CurrentFrame = 0;  }  m_PreviousMouseState = Mouse.GetState();   }But I still need to figure out how to utilize frame number 1 (or 2, if you start counting at 1)!Button.CurrentFrame--;Doesn't have any effect. 