我对 Android 开发很陌生,我正在尝试制作一个游戏,让我的角色在屏幕上的瓷砖上移动。由于每个图块都是它自己的位图,我在角色移动后重绘它。
现在由于某种原因,每次我绘制角色精灵时,整个视图都会闪烁。似乎在每一次偶数绘制中,我创建的背景都是可见的,而在每一次奇数绘制中,背景都是黑色的。此外,角色精灵会留下痕迹,但只有一半是可见的,这取决于它是偶数还是奇数。我的猜测是,由于某种原因,画布正在绘制两个视图或其他东西。
我会上传图片但我不能:(有没有人知道我做错了什么?非常感谢任何帮助。
这是移动角色的方法:
// Moves sprite on screen
private void MoveSprite()
{
// Run as long as the sprite's location didn't reach its destination
while (m_gameView.m_playerControl.m_MoveDestination.x != m_gameView.m_playerControl.m_CharSprite.m_SpriteLocationOnMatrix.x ||
m_gameView.m_playerControl.m_MoveDestination.y != m_gameView.m_playerControl.m_CharSprite.m_SpriteLocationOnMatrix.y)
{
// Draw sprite with FPS control
Canvas c = null;
startTime = System.currentTimeMillis();
try
{
c = m_gameView.getHolder().lockCanvas();
synchronized (m_gameView.getHolder())
{
// Draws player and refreshes the tiles it was previously on
m_gameView.m_playerControl.onDraw(c);
}
}
finally
{
if (c != null)
{
m_gameView.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try
{
if (sleepTime > 0)
Thread.sleep(sleepTime);
else
Thread.sleep(10);
}
catch (Exception e)
{
}
}
// After drawing char movement, changing flag to false
m_fIsCharMoved = false;