本文介绍了刷新WPF游戏窗口最聪明的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在为WPF创建一个游戏引擎。我正试图从DispatcherTimer转移到StopWatch,以获得更恒定的游戏速度(如果游戏中的对象太多,DispatcherTimer将跳过转动)。 我可以使用StopWatch编写正确更新游戏的代码(也就是说,我的游戏对象的速度和位置是正确计算的),但是当我省略DispatcherTimer时,GUI将永远不会重绘。 我知道有几种方法可以强制重绘窗口,但是什么是WPF中2D游戏的最佳选择?我只是在寻找一些简单而有效的方法而不会有太多麻烦。 这是我目前在屏幕上放置对象的代码,每秒多次: { 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!PetterWhat 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游戏窗口最聪明的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 16:46