i started to implement a pacman prototype. the static images (likes borders and stuff like this) are drawed very well. But if i try to move the "pacman" the images doesn't repaint correctly. See the following to pictures... the first i made when the game is started and the second after i move the pacman one step upwards


i implemented this by extending SurfaceView:
Using java Syntax Highlighting
- package src.pacman.view;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import src.pacman.controller.PacmanGame;
- import src.pacman.controller.R;
- import src.pacman.model.Level;
- import src.pacman.util.DrawThread;
- import src.pacman.util.Position;
- import src.pacman.util.Speed;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class GameBoard extends SurfaceView implements SurfaceHolder.Callback {
- private Pacman pacman;
- private PacmanGame pacmanGame;
- private Canvas fCanvas;
- private DrawThread drawThread;
- private Map<Integer, Bitmap> bitmapCache = new HashMap<Integer, Bitmap>();
- private ArrayList<GameObject> staticGameObjects = new ArrayList<GameObject>();
- public GameBoard(PacmanGame pacmanGame) {
- super(pacmanGame);
- this.pacmanGame = pacmanGame;
- }
- public void init() {
- drawThread = new DrawThread(getHolder(), this);
- fillBitmapCache();
- drawLevel();
- initPacman();
- }
- private void initPacman() {
- pacman = new Pacman(this, R.drawable.pacman, Level.getInstance().getPacmanStartPosition(), new Speed(10, Speed.UP));
- }
- private void drawLevel() {
- for (int x = 0; x < Level.getInstance().getLevel().length; x++) {
- for (int y = 0; y < Level.getInstance().getLevel().length; y++) {
- if (Level.getInstance().getLevel()[x][y] == Level.BORDER) {
- addStaticGameObject(new Border(this, R.drawable.border, new Position( ((x + 1) * 10) - 5, (y + 1) * 10)));
- } else if (Level.getInstance().getLevel()[x][y] == Level.FREE) {
- addStaticGameObject(new Border(this, R.drawable.free, new Position( ((x + 1) * 10) - 5, (y + 1) * 10)));
- } else if (Level.getInstance().getLevel()[x][y] == Level.PILLS) {
- addStaticGameObject(new Border(this, R.drawable.pill, new Position( ((x + 1) * 10) - 5, (y + 1) * 10)));
- }
- }
- }
- }
- @Override
- public void onDraw(Canvas canvas) {
- fCanvas = canvas;
- /**
- for (GameObject g: staticGameObjects) {
- g.drawObject();
- }
- **/
- pacman.drawObject();
- Log.e("PACMAN", "DRAW");
- }
- private void fillBitmapCache() {
- bitmapCache.put(R.drawable.border, BitmapFactory.decodeResource(getResources(), R.drawable.border));
- bitmapCache.put(R.drawable.free, BitmapFactory.decodeResource(getResources(), R.drawable.free));
- bitmapCache.put(R.drawable.pill, BitmapFactory.decodeResource(getResources(), R.drawable.pill));
- bitmapCache.put(R.drawable.pacman, BitmapFactory.decodeResource(getResources(), R.drawable.pacman));
- }
- public void addStaticGameObject(GameObject gameObject){
- staticGameObjects.add(gameObject);
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- drawThread.setRunning(true);
- drawThread.start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- boolean retry = true;
- drawThread.setRunning(false);
- while (retry) {
- try {
- drawThread.join();
- retry = false;
- } catch (InterruptedException e) {
- }
- }
- }
- public Canvas getCanvas() {
- return fCanvas;
- }
- public Map<Integer, Bitmap> getBitmapCache() {
- return bitmapCache;
- }
- public void updatePositions() {
- pacman.updatePosition();
- }
- }
Parsed in 0.064 seconds, using GeSHi 1.0.8.4
the drawing thread is the following:
Using java Syntax Highlighting
- package src.pacman.util;
- import src.pacman.view.GameBoard;
- import android.graphics.Canvas;
- import android.view.SurfaceHolder;
- public class DrawThread extends Thread {
- private SurfaceHolder surfaceHolder;
- private GameBoard gameBoard;
- private boolean isRunning = false;
- public DrawThread(SurfaceHolder surfaceHolder, GameBoard gameBoard) {
- this.surfaceHolder = surfaceHolder;
- this.gameBoard = gameBoard;
- }
- public SurfaceHolder getSurfaceHolder() {
- return surfaceHolder;
- }
- public void setSurfaceHolder(SurfaceHolder surfaceHolder) {
- this.surfaceHolder = surfaceHolder;
- }
- public GameBoard getGameBoard() {
- return gameBoard;
- }
- public void setGameBoard(GameBoard gameBoard) {
- this.gameBoard = gameBoard;
- }
- public boolean isRunning() {
- return isRunning;
- }
- public void setRunning(boolean running) {
- isRunning = running;
- }
- @Override
- public void run() {
- Canvas c;
- while (isRunning()) {
- try {
- sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- c = null;
- try {
- c = surfaceHolder.lockCanvas(null);
- synchronized (surfaceHolder) {
- gameBoard.updatePositions();
- gameBoard.onDraw(c);
- }
- } finally {
- // do this in a finally so that if an exception is thrown
- // during the above, we don't leave the Surface in an
- // inconsistent state
- if (c != null) {
- surfaceHolder.unlockCanvasAndPost(c);
- }
- }
- }
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
thanks for any hints!

