locked
How can I control the DispatcherTimer?

    Question

  • Hi,
    I am trying to learn how to make games using XAML.
    I made a game like FlappyBirds.
    I uploaded the project at:

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4733&authkey=!ALt-OalRC1w_eRA&ithint=file%2czip

    Use SPACE to move the bird.

    This is working.

      private void ButtonStart_Click(object sender, RoutedEventArgs e)
            {
                    loadGame();

                    gameTimer.Start();
                    gameTimer1.Start();
                    gameTimer2.Start();
                    gameTimer3.Start();
          
                    txtGameOver.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    btnNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
           
            }

    This is not working.

         private void btnRestart_Click(object sender, RoutedEventArgs e)
            {
               loadGame();

                gameTimer1.Interval = new TimeSpan(0, 0, 0, 0, 5); // Reset Timer
     
                gameTimer.Start();
                gameTimer1.Start();
                gameTimer2.Start();
                gameTimer3.Start();

                txtGameOver.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Visibility.Collapsed;
                Ball.Visibility = Visibility.Visible;

                txtGameOver.Visibility = Visibility.Collapsed;
                btnRestart.Visibility = Visibility.Visible;
            }
    How can I restart the game?

    Thanks


    ADRIAN DIBU

    Saturday, February 07, 2015 4:46 PM

Answers

  • Hi,

    Problem solved. I reset data for X axe.

      private void btnRestart_Click(object sender, RoutedEventArgs e)
            {
                removeChildren();
              //  loadTimers();

                gameTimer.Stop();
                gameTimer1.Stop();
                gameTimer2.Stop();
                gameTimer3.Stop();

                pipe11X = 0;
                pipe22X = 0;
                pipe33X = 0;

                pipe1X = 0;
                pipe4X = 0;

                pipe5X = 0;
                pipe8X = 0;

                pipe9X = 0;
                pipe12X = 0;

                ballVelocityY = 0;
                horizontalSpeed1 = 0;

                ballVelocityY = 3;
                horizontalSpeed1 = 1;

                loadnum();

               loadInit(num1);

                loadPipesS(num1, num11, num2, num22, num3, num33, num4, num44);

                changeBird(ballVelocityY);

           
               gameTimer.Start();
               gameTimer1.Start();
                gameTimer2.Start();
                gameTimer3.Start();

               gameSurfaceS.Children.Add(Ball);
               gameSurfaceS.UpdateLayout();

                txtGameOver.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Visibility.Collapsed;
          
                Ball.Visibility = Visibility.Visible;

                txtGameOver.Visibility = Visibility.Collapsed;
                btnRestart.Visibility = Visibility.Collapsed;
     
            }

    Best regards


    ADRIAN DIBU

    • Marked as answer by adibu Tuesday, February 10, 2015 4:09 PM
    Tuesday, February 10, 2015 4:07 PM

All replies

  • Hi,

    You should edit you codes like below:

    private void btnRestart_Click(object sender, RoutedEventArgs e)
             {
                loadGame();
    
                gameTimer1.Interval = new TimeSpan(0, 0, 0, 0, 5); // Reset Timer
                 gameTimer1.Tick += DispatcherTimerTick;
                 gameTimer.Start();
                 gameTimer1.Start();
                 gameTimer2.Start();
                 gameTimer3.Start();
    
    
             }
    private void DispatcherTimerTick(object sender, object e)
    {
     txtGameOver.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                 btnNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                 btnNew.Visibility = Visibility.Collapsed;
                 Ball.Visibility = Visibility.Visible;
    
    }

    The DispatcherTimer is given an interval of 5 milliseconds, meaning the DispatcherTimer will try (potential delays described earlier in this post) to fire the Tick event approximately 200 times per second. Finally we tell the DispatcherTimer to start by calling the Start method.

    You can see the link to get more information:

    https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Monday, February 09, 2015 4:05 AM
  • Hi,
    Thanks for answer.
    I tried is not working.

    On the Y axe problem solved with posting on the middle of  PipeDifferentY.

      private void loadInit(int num10)
            {
                ballX = 25.0;
                Ball.SetValue(Canvas.LeftProperty, ballX);            Ball.SetValue(Canvas.TopProperty, num10 + PipeDifferentY / 2 - Ball.Height / 2); 
                Ball.Visibility = Visibility.Visible;
            }

    Now on the X axe when Die() acts it removes all children.

    When I restart I don't know why rec1 and rec4 are not restarting from
    startDiff = 201(I removed them in Die()) and they are posted at the position where collision take place and when I gameTimer1.Start() they collide again.

    I tried with no result:

    private void btnRestart_Click(object sender, RoutedEventArgs e)
            {
            
                gameTimer.Interval = new TimeSpan(0, 0, 2); // 2sec
                gameTimer1.Interval = new TimeSpan(0, 0, 2); // 2sec
      
                loadGame();

                gameTimer.Interval = new TimeSpan(0, 0, 0, 0, 5); // Reset Timer
                gameTimer1.Interval = new TimeSpan(0, 0, 0, 0, 5); // Reset Timer

                gameTimer.Start();
                gameTimer1.Start();
                gameTimer2.Start();
                gameTimer3.Start();

                txtGameOver.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Visibility.Collapsed;
                Ball.Visibility = Visibility.Visible;


                txtGameOver.Visibility = Visibility.Collapsed;
                btnRestart.Visibility = Visibility.Visible; 

    }

    Thanks


    ADRIAN DIBU

    Monday, February 09, 2015 9:20 PM
  • Hi,

    Problem solved. I reset data for X axe.

      private void btnRestart_Click(object sender, RoutedEventArgs e)
            {
                removeChildren();
              //  loadTimers();

                gameTimer.Stop();
                gameTimer1.Stop();
                gameTimer2.Stop();
                gameTimer3.Stop();

                pipe11X = 0;
                pipe22X = 0;
                pipe33X = 0;

                pipe1X = 0;
                pipe4X = 0;

                pipe5X = 0;
                pipe8X = 0;

                pipe9X = 0;
                pipe12X = 0;

                ballVelocityY = 0;
                horizontalSpeed1 = 0;

                ballVelocityY = 3;
                horizontalSpeed1 = 1;

                loadnum();

               loadInit(num1);

                loadPipesS(num1, num11, num2, num22, num3, num33, num4, num44);

                changeBird(ballVelocityY);

           
               gameTimer.Start();
               gameTimer1.Start();
                gameTimer2.Start();
                gameTimer3.Start();

               gameSurfaceS.Children.Add(Ball);
               gameSurfaceS.UpdateLayout();

                txtGameOver.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                btnNew.Visibility = Visibility.Collapsed;
          
                Ball.Visibility = Visibility.Visible;

                txtGameOver.Visibility = Visibility.Collapsed;
                btnRestart.Visibility = Visibility.Collapsed;
     
            }

    Best regards


    ADRIAN DIBU

    • Marked as answer by adibu Tuesday, February 10, 2015 4:09 PM
    Tuesday, February 10, 2015 4:07 PM