繁体   English   中英

检测多平台- c# wpf

[英]Detecting multiple platforms- c# wpf

我正在为我的 A Level 编程项目制作平台游戏,玩家在触摸平台时会自动跳到平台上。 但是,我在碰撞检测方面遇到了麻烦,因为它只在一个平台上弹跳并正好穿过 rest。它们都有相同的标签,所以我知道这不是问题所在。

它的编码方式是它会检测与任何带有“平台”标签的矩形的碰撞,但它只检测与 1 个矩形的碰撞

下面的代码示例:

 public partial class MainWindow : Window
    {
        private DispatcherTimer GameTimer = new DispatcherTimer();
        private bool LeftKeyPressed, RightKeyPressed, gravity; 
        double score;
        //this value will increase to be 5x the highest Y value so the score increases the higher Meke gets
        private float SpeedX, SpeedY, FrictionX = 0.88f, Speed = 1, FrictionY = 0.80f;
        //SpeedX controls horizontal movement, SpeedY controls vertical movement

        private void Collide(string Dir)
        {
            foreach (var x in GameScreen.Children.OfType<Rectangle>())
            {
                if (x.Tag != null)
                {

                    var platformID = (string)x.Tag;
                    if (platformID == "platform")
                    {


                        x.Stroke = Brushes.Black;
                        Rect MeekHB = new Rect(Canvas.GetLeft(Meek), Canvas.GetTop(Meek), Meek.Width, Meek.Height);
                        Rect PlatformHB = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height);
                        int Jumpcount = 1;
                        if (MeekHB.IntersectsWith(PlatformHB))
                        {

                            if (Dir == "y")
                            {
                                while (Jumpcount != 700)
                                {
                                    gravity = false;
                                    Jumpcount = Jumpcount + 1;
                                }
                            }
                        }

                        else
                        {
                            gravity = true;
                        }
                    }
                }
            }
        }



        private void KeyboardUp(object sender, KeyEventArgs e)
        {
            //this is what detects when the 'A' key is being pressed
            if (e.Key == Key.A)
            {
                LeftKeyPressed = false;
            }

            if (e.Key == Key.D)
            {
                RightKeyPressed = false;
            }
        }

        private void KeyboardDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.A)
            {
                LeftKeyPressed = true;
            }

            if (e.Key == Key.D)
            {
                RightKeyPressed = true;
            }
        }




        public MainWindow()
        {
            InitializeComponent();
            GameScreen.Focus();

            GameTimer.Interval = TimeSpan.FromMilliseconds(16);
            GameTimer.Tick += GameTick;
            GameTimer.Start();
        }
        private void GameTick(Object Sender, EventArgs e)
        {
            txtScore.Content = "Score: " + score;

            if (LeftKeyPressed)
            {
                SpeedX -= Speed;
            }

            if (RightKeyPressed)
            {
                SpeedX += Speed;
            }

            if (gravity == true)
            {
               SpeedY += Speed;
            }

            else if (gravity == false)
            {
                SpeedY -= Speed+50;
            }


            SpeedX = SpeedX * FrictionX;
            SpeedY = SpeedY * FrictionY;


            Canvas.SetLeft(Meek, Canvas.GetLeft(Meek) + SpeedX);
            Collide("x");
            Canvas.SetTop(Meek, Canvas.GetTop(Meek) + SpeedY);
            Collide("y");



            double maxY = 0;
            if (Canvas.GetBottom(Meek) > maxY)
            {
                maxY = Canvas.GetBottom(Meek);
            }

            score = maxY;
        }

        
    }
}

我认为您的gravity = true逻辑存在错误。 您为所有平台检查循环中的交叉点,因此即使循环中的第一个平台将重力设置为 false,循环中的下一个平台将再次将重力设置为 true,逻辑将不起作用。 据我了解,您的逻辑仅适用于检测 GameScreen.Children.OfType() 中的最后一个平台。 也许尝试break; 找到交叉路口后循环。

为了将来——向您的应用程序添加一些日志,以便能够调试此类问题。 您的游戏库很可能提供了一些打印调试消息的方法 - 将它们添加到您的代码中,以便您可以看到发生了什么。

而且你的while (Jumpcount != 700)循环看起来没用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM