Reputation: 104
I am creating a card game and I need a methods execution to pause while a card is being animated accross the screen. Currently I have this and it animates the card fine but will carry on playing the game whilst the card is being animated. Not sure how to go about it. I have tried Thread.Sleep() but it still continues all execution possible and then pauses.
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
string input = Microsoft.VisualBasic.Interaction.InputBox("Please enter your betting amount (£3.00 minimum bet)", "Play", "3.00", -1, -1);
bet = double.Parse(input);
if (Globals.Balance > bet)
{
btnHit.Enabled = true;
btnStick.Enabled = true;
Globals.Balance -= bet;
lblBalance.Text = Globals.Balance.ToString();
Play();
}
else
{
throw new Exception("You don't have enough money!");
}
}
catch (FormatException)
{
MessageBox.Show("Incorrect format for betting amount");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Play()
{
ClearDetails();
DealPlayerCard();
Classes.Deck.NextCard(Deck);
DealPlayerCard();
UpdatePlayerTotal();
}
private void DealPlayerCard()
{
//does stuff here
switch (cardNum)
{
case 3:
pb.Location = new Point(120, 0);
timerCard.Enabled = true;
timerCard_Tick(null, null);
break;
case 4:
pb.Location = new Point(180, 0);
timerCard.Enabled = true;
timerCard_Tick(null, null);
break;
case 5:
pb.Location = new Point(240, 0);
timerCard.Enabled = true;
timerCard_Tick(null, null);
break;
}
AddPlayerCard(pb);
AddToHand("Player");
}
private void timerCard_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
//sets x and y in a switch statement here
if ((CardBack.Location.X == x) && (CardBack.Location.Y == y))
{
timerCard.Enabled = false;
CardBack.Visible = false;
CardBack.Location = new Point(775, 247);
this.ResumeLayout();
}
else if ((CardBack.Location.X > 417) && (CardBack.Location.Y < 434))
{
CardBack.Location = new Point(CardBack.Location.X - 1, CardBack.Location.Y + 1);
timerCard_Tick(null, null);
}
else if ((CardBack.Location.X > 417) && (CardBack.Location.Y == 434))
{
CardBack.Location = new Point(CardBack.Location.X - 1, CardBack.Location.Y);
timerCard_Tick(null, null);
}
else if ((CardBack.Location.X == 417) && (CardBack.Location.Y < 434))
{
CardBack.Location = new Point(CardBack.Location.X, CardBack.Location.Y + 1);
timerCard_Tick(null, null);
}
}
Upvotes: 0
Views: 113
Reputation: 3231
Enabling the timer returns immediately, and the timer events occur separately, which is why you see the game continuing.
Instead move the rest of the game to the timer event, after the animation is finished. Like this (pseudocode):
private void DealPlayerCard()
{
//does stuff here
timer.Enabled = true;
// no more code
}
private void timerCard_Tick(object sender, EventArgs e)
{
switch (location)
case first:
DoSomeStuff();
case next:
DoSomeOtherStuff();
case finished:
timer.enabled = false;
continueGame();
}
I guess you have some sort of game loop that wasn't in the sample, which you may also have to pause.
Upvotes: 1