刷新WPF游戏窗口最聪明的方法是什么? [英] What's the smartest way to refresh the window for a WPF game?
问题描述
我正在为WPF创建一个游戏引擎。我正试图从DispatcherTimer转移到StopWatch,以获得更恒定的游戏速度(如果游戏中的对象太多,DispatcherTimer将跳过转动)。
我可以使用StopWatch编写正确更新游戏的代码(也就是说,我的游戏对象的速度和位置是正确计算的),但是当我省略DispatcherTimer时,GUI将永远不会重绘。
我知道有几种方法可以强制重绘窗口,但是什么是WPF中2D游戏的最佳选择?我只是在寻找一些简单而有效的方法而不会有太多麻烦。
这是我目前在屏幕上放置对象的代码,每秒多次:< ($ i $ 0; i< Bodies.NumberOfBodies();
{
Body body = Bodies}
Hi,
I am creating a game engine for WPF. I am trying to move away from DispatcherTimer to StopWatch, to get a more constant speed of the game (as DispatcherTimer skips turns if there are too many objects in the game).
I can write the code for updating the game correctly using a StopWatch (that is, my game objects' speeds and positions are calculated correctly), but when I omit the DispatcherTimer the GUI will never redraw.
I know there are several ways to force a redraw of the window, but what would be my best option for a 2D game in WPF? I'm just looking for something simple that works rather efficiently without a lot of hassle.
This is my current code for placing objects onscreen, many times per second:
for (int i = 0; i < Bodies.NumberOfBodies();
{
Body body = Bodies.Bodylist[i];
var shape = body.GetShape();
game.Children.Add(shape); //each movable body's shape is added again.
Canvas.SetLeft(shape, body.PosX - offsetX);
Canvas.SetTop(shape, body.PosY - offsetY);
}
谢谢!
Petter
我试过的:
我已经阅读了这个主题,但有很多解决方案,我不知道什么是最合适的。
Thanks!
Petter
What I have tried:
I have read up on the subject, but there are many solutions and I don't know what would be the most suitable.
推荐答案
我找到了一个与我同事合作的答案。点击这里查看答案:
在WPF中添加eventhandler(compositiontarget.rendering) [ ^ ]
I found an answer working with a colleague of mine. Check here for answer:
Adding an eventhandler (compositiontarget.rendering) in WPF[^]
这篇关于刷新WPF游戏窗口最聪明的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!