精灵图像在屏幕上移动时留下阴影/尾巴

时间:2013-12-23 04:01:50

标签: android game-engine

我的Android游戏程序有精灵在屏幕上移动,但它们留下了痕迹/尾巴/阴影。我尝试了不同的FPS也失败了也试过canvas.drawARGB(100,0,0,0);其他线程建议,它删除尾部但暗淡其他精灵。这是代码

public class GameView extends SurfaceView {

    public GameView(Context context) {
        super(context);
        gameloopThread = new GameLoopThread(this);
        holder = getHolder();
        holder.addCallback(new Callback() {

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                // TODO Auto-generated method stub

            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                createSprites();
                gameloopThread.setRunning(true);
                gameloopThread.start();
                }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                boolean retry = true;
                gameloopThread.setRunning(false);
                while (retry) {
                       try {
                             gameloopThread.join();
                             retry = false;
                       } catch (InterruptedException e) {
                       }
                }
            }
        }); 


    }


    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawColor(color.black);
        for (int i = temps.size() -1; i>=0; i--) {
            temps.get(i).onDraw(canvas);
        }
        for (Sprite sprite : sprites) {
        //  canvas.drawARGB(8,8,8,8);  // mine
        sprite.onDraw(canvas);
        }
    }

    }
}

继承游戏循环课程

public class GameLoopThread extends Thread {
    static final long FPS = 10;  

    @Override
    public void run(){
        long ticksPS = 1000/FPS;
        long startTime;
        long sleepTime;
        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                  c = view.getHolder().lockCanvas();
                  synchronized (view.getHolder())
                  {

                      view.onDraw(c);
                  }
            } finally {
                if (c != null) {
                    view.getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS-(System.currentTimeMillis()-startTime);
            try {
                  if (sleepTime > 0)
                         sleep(sleepTime);
                  else
                      sleep(10); // sleep causes it to change speed and jerk, hi sleep hi jerk
            } catch (Exception e) {}
        }

    }

和sprite类......

public class Sprite {

     private GameView gameView;


    protected void onDraw(Canvas canvas)
    {
        update();
        int srcX = currentFrame * width;
        int srcY = getAnimationRow() * height;
        Rect src = new Rect(srcX, srcY, srcX+width, srcY + height);
        Rect dst = new Rect(x, y, x + width, y + height);

        canvas.drawBitmap(bmp, src, dst, null);
    }
    // direction = 0 up, 1 left, 2 down, 3 right
    // animation = 3 back, 1 left, 0 front, 2 right
    private int getAnimationRow() {

        return DIRECTION_TO_ANIMATION_MAP[direction];
    }

}

我无法找到任何方法在精灵更改位置后重新创建画布。帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

问题是因为您调用onDraw()方法的频率。您需要通过更改传递给FPS中的常量static final long FPS = 10;long ticksPS = 1000/FPS;

的值来增加每秒帧数

增加此值的副作用是它需要更多的处理器能力,并且您的位图将更快地移动并降低其速度,您需要更改{和{1}}中x和y的变化率

答案 1 :(得分:0)

只需添加canvas.drawColor(Color.BLACK); 或任何颜色 我遇到了同样的问题,而且代码保存了我