【问题标题】:WPF- Why keyDown function in my project does not work?WPF-为什么我的项目中的 keyDown 功能不起作用?
【发布时间】:2020-10-04 23:45:46
【问题描述】:

我想加入这个游戏:https://www.mooict.com/wpf-c-tutorial-create-a-fun-balloon-popping-game-in-visual-studio/

一个函数 ColorKeyIsDown() 在按下某些键时激活:Y、R、B、G、O。 因此,如果按下这些键中的任何一个,将从画布中删除一个特定的气球。 我为每个制作的新气球制作了 5 个可能的标签, 因为ballonSkins的整数值是1-5,每个数字都声明了newBallon的不同背景。

在 ColorKeyIsDown() 我想通过它们的标签来识别气球, 但它不起作用。

如何识别每个不同的气球(黄色/红色/蓝色气球)? 我可以通过它的背景来识别气球吗?

*在 XAML 中:

 <Canvas Name="MyCanvas" Focusable="True" MouseLeftButtonDown="popBalloons" Background="White" KeyDown="ColorKeyIsDown">

    <Label Name="scoreLabel" FontSize="24" Content="Score: 0" Foreground="black" FontWeight="ExtraBold" Canvas.Top="527" />

</Canvas>

*在 C# 代码中:

    private void ColorKeyIsDown(object sender, KeyEventArgs e)
    {
        if (gameisactive)
        {
            foreach(var x in MyCanvas.Children.OfType<Rectangle>())
            {
                switch (e.Key)
                {
                    case Key.Y:
                        if ((string)x.Tag == 3.ToString()) { MyCanvas.Children.Remove(x); score++; }
                        break;
                    case Key.R:
                        if ((string)x.Tag == 1.ToString()) { MyCanvas.Children.Remove(x); score++; }
                        break;
                    case Key.B:
                        if ((string)x.Tag == 5.ToString()) { MyCanvas.Children.Remove(x); score++; }
                        break;
                    case Key.G:
                        if ((string)x.Tag == 4.ToString()) { MyCanvas.Children.Remove(x); score++; }
                        break;
                    case Key.O:
                        if ((string)x.Tag == 2.ToString()) { MyCanvas.Children.Remove(x); score++; }
                        break;
                }
            }
        }
    }

*在C#中-在gameEngine()函数中:

          // check which skin number is selected and change them to that number
            switch (balloonSkins)
            {
                case 1:
                    balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon1.png"));
                    break;
                case 2:
                    balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon2.png"));
                    break;
                case 3:
                    balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon3.png"));
                    break;
                case 4:
                    balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon4.png"));
                    break;
                case 5:
                    balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon5.png"));
                    break;
            }



            // make a new rectangle called new balloon
            // inside this it has a tag called bloon, height 70 pixels and width 50 pixels and balloon image as the background
            Rectangle newBalloon = new Rectangle
            {
                
                Tag = balloonSkins.ToString(),
                Height = 70,
                Width = 50,
                Fill=balloonImage
                
            };

【问题讨论】:

  • 您当前的代码不起作用怎么办?
  • 起初,当我按下一些键时:Y、B、O、G、R 什么都没有发生......所以 ColorKeyIsDown() 没有做任何事情...... cmets 中的某个人写道在 Canvas 中做 KeyDown,在 Window 中做,因为它连接到窗口浏览器,所以现在它可以工作了✌????
  • 您可以回答自己的问题,向其他人表明问题已解决

标签: c# wpf xaml wpf-controls


【解决方案1】:

有人写道,而不是在 Canvas 中执行 KeyDown 事件, 在窗口浏览器中执行此操作。解决方案:onkeydown event not working on canvas?

*我改了,效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2021-04-01
    相关资源
    最近更新 更多