Hi,
I am creating a board game and I want the board piece to jump to the next "box"/"slot" with a delay. For example I roll a 6 then I want the character to go to slot 1, slot 2, slot 3 etc. until slot 6 with a small delay as it was a real game.
However I can't get it to work. Right now I have this code in Draw:
[code] protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle(0,0, 800, 800), Color.White);
spriteBatch.End();
/* for (int x = 0; x < 40; x++)
{
spriteBatch.Begin();
spriteBatch.Draw(background, gameBoard.board[x], Color.Red);
spriteBatch.End();
}
*/
spriteBatch.Begin();
spriteBatch.Draw(benderPiece, new Rectangle((int)benderPos.X, (int)benderPos.Y, 50, 50), Color.White);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.DrawString(spriteFont, dice.ToString(), new Vector2(125, 125), Color.Black);
spriteBatch.End();
spriteBatch.Begin();
if (playerMoved == false && dice != 0)
{
for (int i = 0; i <= dice; i++)
{
benderPos.X = gameBoard.board[benderSquarePos].X;
benderPos.Y = gameBoard.board[benderSquarePos].Y;
benderSquarePos += 1;
spriteBatch.Draw(benderPiece, new Rectangle((int)benderPos.X, (int)benderPos.Y, 50, 50), Color.White);
Thread.Sleep(1250);
}
dice = 0;
}
spriteBatch.End();
base.Draw(gameTime);
}
}[/code]
However what happens is that it delays and then it the number of the dice but very fast.
How can I make so that the delay is between every movement?
I have tried to have it in the Update as well but I can't get it to work.
Best regards,
Tomas
Loading

FroglegPosted Dec 29, 2011, 3:36 PM
ie:
bool pieceMoving = false, pieceMove = false;
int pieceCount = 0;
//In Update put
if (pieceMoving )
{
if (pieceCount < 20)
{
pieceCount= pieceCount + 1;
}
else if (pieceCount > 20)
{
pieceMoving = false;
pieceMove = true;
piececount = 0 ;
}
}
This is basic and may be streamlined, but see if you can work it out
Sam HobbsPosted Dec 26, 2011, 6:59 PM
One critical concept to understand is that you need to let Windows draw when it wants to. You should not do the drawing directly yourself. What you can do is to draw into a bitmap that Windows puts into the window when it needs to. Another critical concept is that you need to use a timer (not Sleep) to change the graphics.
So you need to add a Timer to the form. The Timer will not show on the form; it will however be in the form. Set the interval to whatever interval you want the delay to be. Then you need to have a member of the form that is the slot number. Use the designer to add a tmer tick event handler, so that the event handler executes at the interval that you set. You can start and stop the timer; normally it would be stopped and it would be started only when you need the "board piece to jump". In the timer tck event handler, just change the slot number (without drawing) so it goes to the next slot. What you do next depends on which of two or more things that you do.
You can use a PictureBox on your form. Then you can draw the board into a bitmap and then set the PictureBox to the bitmap.
Or you can draw directly to the window. If you do that, then you do not want to draw in the timer tick event; you want undraw the old slot and draw a new slot. That is a bit more complicated, so I assume you will want to learn about that later and do the bitmap way for now.
You should read some articles in this web site; there are many relevant articles that will help you very much. Note that I say "just change the slot number (without drawing) so it goes to the next slot". I think that can be very confusing. I understand. You need to read some articles that help you understand that; it is better that you read something that someone has already written instead of me trying to write something that has alredy been written many times.