1
我目前正在編寫一個龐的遊戲。我已經完成了大部分遊戲,但是我遇到了一個煩人的問題。我使用dispatcherTimer
,優先級設置爲發送,時間跨度設置爲1毫秒。我使用達到dx = 9和dy = 9的動畫矩形爲了使球移動得足夠快。由於大的像素跳躍,球會跳過屏幕而不是平滑移動。根據每個週期1毫秒的數學,這個球應該比現在快得多。我需要更頻繁地更新球並將其移動更少...如何在C#中使用WPF快速平滑動畫
有沒有關於更好的方法來做到這一點的任何建議?這裏是什麼,我有一個片段...
pongballTimer = new DispatcherTimer(DispatcherPriority.Send);
pongballTimer.Tick += new EventHandler(pongballTimer_Tick);
pongballTimer.Interval = new TimeSpan(0, 0, 0, 0, _balldt);
private void pongballTimer_Tick(object sender, EventArgs e)
{
double pongtop = Canvas.GetTop(PongBall);
double pongleft = Canvas.GetLeft(PongBall);
double paddletop = Canvas.GetTop(RightPaddle);
double paddleleft = Canvas.GetLeft(RightPaddle);
if (pongleft + PongBall.Width > paddleleft)
{
if (((pongtop < paddletop + RightPaddle.Height) && (pongtop > paddletop)) ||
((pongtop + PongBall.Height < paddletop + RightPaddle.Height) &&
(pongtop + PongBall.Height > paddletop)))
{
_dx *= -1;
SetBalldy(pongtop, PongBall.Height, paddletop, RightPaddle.Height);
_rightpoint++;
lblRightPoint.Content = _rightpoint.ToString();
meHitSound.Play();
}
else // The ball went past the paddle without a collision
{
RespawnPongBall(true);
_leftpoint++;
lblLeftPoint.Content = _leftpoint.ToString();
meMissSound.Play();
if (_leftpoint >= _losepoint)
LoseHappened("You Lost!!");
return;
}
}
if (pongleft < 0)
{
meHitSound.Play();
_dx *= -1;
}
if (pongtop <= _linepady ||
pongtop + PongBall.Height >= PongCanvas.Height - _linepady)
{
meDeflectSound.Play();
_dy *= -1;
}
Canvas.SetTop(PongBall, pongtop + _dy);
Canvas.SetLeft(PongBall, pongleft + _dx);
}
只是一個隨機的想法,但如果你正在更新滴答處理程序的視覺效果,你可能會嘗試在畫布上或球后調用InvalidateVisual – JerKimball 2013-03-19 17:00:05
老兄,我不知道你在做什麼,但這不是你應該在WPF中編碼。你的代碼看起來太Winforms,也沒有辦法你會有1毫秒刷新。任何用戶界面的正常費率在30到60幀/秒之間。 – 2013-03-19 17:01:24
正常最小定時器分辨率爲15ms。 1ms將等於1000fps。嘗試一些更真實的,如50至100毫秒,這將是20或10 fps尊重。您仍然會遇到一些重複性問題 – 2013-03-19 17:08:57