Canvas似乎在两个不同的视图上绘制

时间:2012-09-15 12:29:25

标签: android surfaceview android-canvas

我对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;

1 个答案:

答案 0 :(得分:0)

Check out screen width and height based drawing.

你应该在屏幕内画画。

I think u r drawing the characters out of the screen width or height