Silverlight 4 - 切换页面

时间:2014-06-13 11:15:21

标签: c# silverlight

我正在使用silverlight 4制作乒乓球游戏,但我有问题。我使用Content方法在页面之间切换,例如(this.Content = new Page()),页面是UserControl XAML页面。

除了游戏本身以外,所有菜单都可以正常使用。 当我尝试将内容切换到包含游戏的页面时,它全部空白! 我已经将游戏作为主页面进行了测试,但是当我从另一个页面进入游戏时,它只会变成空白。我会发布游戏代码

编辑:我做了一些调试,我修改了一些代码,现在我可以打开游戏窗口,通过调试程序,我可以看到该类完成了它应该做的一切。唯一的问题是它没有识别我的键盘,如果我从游戏板开始作为我的主页,它一切正常,但如果我从另一个菜单切换到它,游戏板显示,但控件不工作。任何提示?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Interop;

namespace Pong
{
    public partial class Page : UserControl
    {
        public int MaxScore = 5;
        public String [] names = new String [2];
        public bool Playing = false;
        public double MouseScaleY = 0.0;

        public bool[] KeysDown = new bool[256];

        public SilverlightHost appHost = Application.Current.Host;
        public Random random = new Random();
        public DispatcherTimer timer = new DispatcherTimer();


        //passar os nomes dos jogadores, a dificuldade e o max score no construtor de PAGE
        //depois é so actualizar a label do max Score e a dos nomes ( no metodo onde diz Player One wins !!) uheueh

        public Page() //String p1, String p2, int maxScore
        {
            InitializeComponent();

            //setmaxScore(maxScore);
            //names[0] = p1;
            //names[1] = p2;
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        public void setmaxScore(int score)
        {
            this.MaxScore = score;
        }

        public void Page_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Interval = TimeSpan.FromMilliseconds(20);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            this.KeyDown += new KeyEventHandler(Page_KeyDown);
            this.KeyUp += new KeyEventHandler(Page_KeyUp);

            //this.MouseMove += new MouseEventHandler(Page_MouseMove);
            //this.MouseLeftButtonUp += new MouseButtonEventHandler(Page_MouseLeftButtonUp);

            Round_Reset();
        }

        public void timer_Tick(object sender, EventArgs e)
        {
            Game_HandleInput();
            //Game_RunAI();

            PlayerOne.Move();
            PlayerTwo.Move();
            Ball.Move();

            Game_CheckCollisions();
        }

        #region AI
        public void Game_RunAI()
        {
            double intersectPoint = 300; //default to centre

            if (Ball.XVel > 0) //coming towards us
            {


                intersectPoint = Ball.Y + 10; // center of ball
            }

            if (intersectPoint <= (PlayerTwo.Y + 50))
            {
                PlayerTwo.Y -= Math.Min(10.0, (PlayerTwo.Y + 50) - intersectPoint);
            }
            if (intersectPoint >= (PlayerTwo.Y + 50))
            {
                PlayerTwo.Y += Math.Min(10.0, intersectPoint - (PlayerTwo.Y + 50));
            }

        }
        #endregion

        public void Game_HandleInput()
        {
            if (KeysDown[(int)Key.Up])
            {
                PlayerOne.Y -= 10.0;
            }
            if (KeysDown[(int)Key.Down])
            {
                PlayerOne.Y += 10.0;
            }
            if (KeysDown[(int)Key.W])
            {
                PlayerTwo.Y -= 10.0;
            }
            if (KeysDown[(int)Key.S])
            {
                PlayerTwo.Y += 10.0;
            }

            if (KeysDown[(int)Key.Space])
            {
                Game_HandleSpacekey();
            }
        }
        public void Game_CheckCollisions()
        {
            // Bouncing off the top and bottom walls
            if ((Ball.Y <= 0) || (Ball.Y >= 580))
                Ball.BounceY();

            // Has the ball gone far enough to left or right to score a point?
            if (Ball.X <= 0)//PlayerOne.X)
            {
                //ball is past playerOne's paddle, player two scores a point
                int playerTwoScore = Convert.ToInt32(PlayerTwoScore.Text);
                playerTwoScore++;
                PlayerTwoScore.Text = playerTwoScore.ToString();

                if (playerTwoScore >= MaxScore)
                {
                    GameWonMessage_Show("Player Two Wins!");
                }

                Round_Reset();
            }
            else if ((Ball.X + Ball.Width) >= 800) //(PlayerTwo.X + PlayerTwo.Width))
            {
                //ball is past player two's paddle, player one scores a point
                int playerOneScore = Convert.ToInt32(PlayerOneScore.Text);
                playerOneScore++;
                PlayerOneScore.Text = playerOneScore.ToString();

                if (playerOneScore >= MaxScore)
                {
                    GameWonMessage_Show("Player One Wins!");
                }

                Round_Reset();
            }

            //check whether Y values can intersect
            if ((Ball.Y + Ball.Height) >= PlayerOne.Y &&
                Ball.Y <= (PlayerOne.Y + PlayerOne.Height))
            {
                if (Ball.XVel < 0)
                {
                    double ballX = Ball.X;
                    if (Math.Abs(ballX - (PlayerOne.X + PlayerOne.Width)) < 10)
                    {
                        Ball.BounceX();
                        Ball.ApplyMoment(0.0, PlayerOne.Y - PlayerOne.PrevY);
                    }
                }
            }

            //check whether Y values can intersect
            if ((Ball.Y + Ball.Height) >= PlayerTwo.Y &&
                Ball.Y <= (PlayerTwo.Y + PlayerTwo.Height))
            {
                if (Ball.XVel > 0)
                {
                    double ballX = Ball.X + Ball.Width;
                    if (Math.Abs(ballX - PlayerTwo.X) < 10)
                    {
                        Ball.BounceX();
                        Ball.ApplyMoment(0.0, PlayerTwo.Y - PlayerTwo.PrevY);
                    }
                }
            }
        }
        public void Game_HandleSpacekey()
        {
            if (NewGameMessage.Opacity > 0.99999)
            {         
                NewGameMessage_SlideOut.Begin();
               // BlackOut_Hide();
            }
            else if (GameWonMessage.Opacity > 0.99999)
            {
                //GameWonMessage_Hide();
                this.Content = new Play();
               // Game_Reset();

                //EM VEZ DE FAZERMOS GAME RESET , VAMOS PARA O MENU PRINCIPAL
            }
            else if (GameWonMessage.Opacity > 0.00001 ||
                     NewGameMessage.Opacity > 0.00001)
            {
                //This space intentionally left blank.
            }
            else
            {
                Round_Start();
            }
        }
        public void Game_Reset()
        {
            PlayerOneScore.Text = "0";
            PlayerTwoScore.Text = "0";
            Round_Reset();
        }

        public void Round_Start()
        {
            if (!Playing)
            {
                double direction = 0.0;
                bool okDirection = false;

                int nrTries = 0;

                while (!okDirection && nrTries < 10)
                {
                    direction = random.NextDouble() * (2 * Math.PI);
                    okDirection = true;

                    if (Math.Abs(direction - (0.5 * Math.PI)) < 0.7 ||
                        Math.Abs(direction - (1.5 * Math.PI)) < 0.7)
                    {
                        okDirection = false;

                    }
                    nrTries++;
                }


                double speed = (random.NextDouble() + 1) * 10;

                Ball.XVel = speed * Math.Cos(direction);
                Ball.YVel = speed * Math.Sin(direction);

                //marker.X2 = 400 + Ball.XVel; marker.Y2 = 300 + Ball.YVel;

                Playing = true;
            }
        }
        public void Round_Reset()
        {
            Ball.X = 390;
            Ball.Y = 290;

            Ball.XVel = Ball.YVel = 0;

            PlayerOne.Y = PlayerTwo.Y = 250;
            Playing = false;
        }

        void Page_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Game_HandleSpacekey();
            e.Handled = true;
        }
        void Page_MouseMove(object sender, MouseEventArgs e)
        {
            PlayerOne.Y = (e.GetPosition(this).Y) - 50;
        }

        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            KeysDown[(int)e.Key] = true;
            e.Handled = true;
        }
        void Page_KeyUp(object sender, KeyEventArgs e)
        {
            KeysDown[(int)e.Key] = false;
            e.Handled = true;
        }

        public void GameWonMessage_Show(string text)
        {
            BlackOut_Show();
            GameWonMessage.WinnerText.Text = text;
            GameWonMessage.IsHitTestVisible = true;
            GameWonMessage_SlideIn.Begin();
        }
        public void GameWonMessage_Hide()
        {
            GameWonMessage_SlideOut.Begin();
            GameWonMessage.IsHitTestVisible = false;
            BlackOut_Hide();
        }

        public void BlackOut_Show()
        {
            BlackOut.IsHitTestVisible = true;
        }
        public void BlackOut_Hide()
        {
            BlackOut.IsHitTestVisible = false;
        }

        #region Obsolete - Resizing code
        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            Content_Resized(null, null);
        }
        void Content_Resized(object sender, EventArgs e)
        {
            if ((appHost.Content.ActualHeight >= 600) &&
                (appHost.Content.ActualWidth >= 800))
            {
                double scaleX = appHost.Content.ActualWidth / 800;
                double scaleY = appHost.Content.ActualHeight / 600;
                double scale = Math.Min(scaleX, scaleY);

                MouseScaleY = scaleY;

                this.Width = LayoutRoot.Width = appHost.Content.ActualWidth;
                this.Height = LayoutRoot.Height = appHost.Content.ActualHeight;

                //BoardScale.ScaleX = BoardScale.ScaleY = scale;
                //BoardTranslate.X = ((appHost.Content.ActualWidth / scale) - 800) / 2;
                //BoardTranslate.Y = ((appHost.Content.ActualHeight / scale) - 600) / 2;

                BlackOut.Width = LayoutRoot.Width;
                BlackOut.Height = LayoutRoot.Height;
                //BlackOutScale.ScaleX = scaleX;
                //BlackOutScale.ScaleY = scaleY;
                //BlackOutTranslate.X = -BoardTranslate.X;
                //BlackOutTranslate.Y = -BoardTranslate.Y;
            }
        }

0 个答案:

没有答案