c# - 子元素不想移动
问题描述
我试图通过按上/下/左/右键使图像移动。我使用 wpf 和 canvas 作为面板控件。然后,我将图像命名为 pacman,并在未按下键时使用 onKeyDown,在按下键时使用 onKeyUp,在 xaml 代码中,当我尝试进行定义时,它链接到 xaml 中的正确方法。 CS。这是 xaml.cs 文件:
namespace Pacman
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer gameTimer = new DispatcherTimer();
Boolean goup, godown, goleft, goright, isGameOver;
int score, playerSpeed = 8, redGhostSpeed, yellowGhostSpeed, pinkGhostX, pinkGhostY;
public MainWindow()
{
InitializeComponent();
gameTimer.Interval = TimeSpan.FromMilliseconds(20);
gameTimer.Tick += Timer_Tick;
resetGame();
}
private void Timer_Tick(object sender, EventArgs e)
{
txtScore.Text = "Score : " + score;
if (goleft && Canvas.GetLeft(pacman) > 0)
{
Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) - playerSpeed);
}
if (goright && Canvas.GetLeft(pacman) + (pacman.Width * 2) < Application.Current.MainWindow.Width)
{
Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) + playerSpeed);
}
if (goup && Canvas.GetTop(pacman) > 0)
{
Canvas.SetTop(pacman, Canvas.GetTop(pacman) - playerSpeed);
}
if (godown && Canvas.GetTop(pacman) + (pacman.Height * 2) < Application.Current.MainWindow.Height)
{
Canvas.SetTop(pacman, Canvas.GetTop(pacman) + playerSpeed);
}
}
private void onKeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Up)
{
goup = true;
}
if(e.Key == Key.Down)
{
godown = true;
}
if (e.Key == Key.Left)
{
goleft = true;
}
if (e.Key == Key.Right)
{
goright = true;
}
}
private void onKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up)
{
goup = false;
}
if (e.Key == Key.Down)
{
godown = false;
}
if (e.Key == Key.Left)
{
goleft = false;
}
if (e.Key == Key.Right)
{
goright = false;
}
}
private void resetGame()
{
txtScore.Text = "Score : 0";
score = 0;
redGhostSpeed = 5;
yellowGhostSpeed = 5;
pinkGhostX = 5;
pinkGhostY = 5;
playerSpeed = 8;
isGameOver = false;
gameTimer.Start();
}
private void gameOver(string message)
{
}
}
}
我认为代码现在应该可以工作了,但是没有人知道我该怎么做?
编辑:这是 xaml 代码:
<Window x:Class="Pacman.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Pacman"
mc:Ignorable="d"
Title="Pacman Game" Height="700" Width="600" Background="Black"
xmlns:gif ="https://github.com/XamlAnimatedGif/XamlAnimatedGif">
<Canvas Background="Black" KeyDown="onKeyDown" KeyUp="onKeyUp" Focusable="True">
<Image x:Name="pacman" HorizontalAlignment="Left" Height="55" VerticalAlignment="Top" Width="55" gif:AnimationBehavior.SourceUri="Resources\right.gif"/>
</Canvas>
</Window>
我已经修复了这些家伙,我忘了给画布名称和MyCanvas.Focus();
我的 Timer_Tick()
太感谢了
解决方案
推荐阅读
- objective-c - 将 ClassKit 协议合并到 Objective-C++ 项目中
- c - 有人可以解释一下为什么这里要计算这个模数吗?
- python - 如何正确删除数据框(熊猫)中的列?
- python - 是否可以从 PyQt5 中的线程获取数组?
- xamarin.android - GetView 更新屏幕外的项目
- c++ - 如何清除输入行,而不仅仅是单个字符
- c# - 从对象列表中获取所有对象,其中对象属性之一在 c# 中为空或为空
- php - 在 laravel 中获取此操作是未经授权的
- neural-network - 回归还是分类?
- javascript - 如何在输入字段中填充所有 * 并在其上输入