0

我正在开发一个简单的游戏,比如BSD robots。这个游戏包含一个相当大的棋盘(超过 40 个单元格),即使在 10 英寸平板电脑上看起来也不好看。我的决定是滚动(移动)表面视图,而不是缩放每个像手指点一样大小的图形。我已经阅读了大量有关表面视图的文章,但我不明白如何移动它?

我的活动代码:

package ru.onyanov.robots;
public class MainActivity extends Activity {

public BoardView board;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    board = new BoardView(this);
    setContentView(board);
}
}

班级委员会:

package ru.onyanov.robots;
public class BoardView extends SurfaceView {
private GameThread mThread;
private boolean running = false;
public final int sizeX = 50;
public final int sizeY = 35;
public final int cellSize = 64;

private int robotCount = 4;

public ArrayList<Cell> cells = new ArrayList<Cell>();
private ArrayList<Robot> robots = new ArrayList<Robot>();
private Hero hero;
public Bitmap imageCell;
public Bitmap imageRobot;
public Bitmap imageHero;

public BoardView(Context context) {
    super(context);     

    makeGraphic();
    constructCells();
    constructRobots();      
    hero = new Hero(this, 3, 2, imageHero);

    mThread = new GameThread(this);

    getHolder().addCallback(new SurfaceHolder.Callback() {
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            mThread.setRunning(false);
            while (retry) {
                try {
                    mThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            mThread.setRunning(true);
            mThread.start();
        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {


        }
    });
}

private void makeGraphic() {
    Bitmap cellImageSource = BitmapFactory.decodeResource(getResources(),
            R.drawable.cell);
    imageCell = Bitmap.createScaledBitmap(cellImageSource, cellSize,
            cellSize, false);
    imageRobot = BitmapFactory.decodeResource(getResources(),
            R.drawable.robot);
    imageHero = BitmapFactory.decodeResource(getResources(),
            R.drawable.hero);

}

private void constructRobots() {
    Robot robot;
    Random rand = new Random();
    for (int r = 0; r < robotCount; r++) {
        int x = rand.nextInt(sizeX);
        int y = rand.nextInt(sizeY);
        robot = new Robot(this, x, y, imageRobot);
        robots.add(robot);
    }
    return;
}

private void constructCells() {
    Cell cell;
    for (int y = 0; y < sizeY; y++) {
        for (int x = 0; x < sizeX; x++) {
            cell = new Cell(this, x, y, imageCell);
            cells.add(cell);
        }
    }
    return;
}

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    Cell cell;
    for (int c = 0; c < cells.size(); c++) {
        cell = cells.get(c);
        cell.onDraw(canvas);
    }
    Robot robot;
    for (int r = 0; r < robots.size(); r++) {
        robot = robots.get(r);
        robot.onDraw(canvas);
    }
    hero.onDraw(canvas);
}

public boolean onTouchEvent(MotionEvent e) {
    int shotX = (int) e.getX();
    int shotY = (int) e.getY();

    if (e.getAction() == MotionEvent.ACTION_MOVE){
        //TODO move board
        showToast("move Board");
    } else if (e.getAction() == MotionEvent.ACTION_UP) {
        showToast("touchPoint: " + shotX + ", "+shotY);
        hero.moveByDirection(shotX, shotY);
        this.scrollTo(shotX, shotY);
    }
    return true;
}

public void showToast(String mes) {
    Toast toast = Toast.makeText(getContext(), mes, Toast.LENGTH_LONG);
    toast.show();
}

public class GameThread extends Thread {
    private BoardView view;

    public GameThread(BoardView view) {
        this.view = view;

    }

    public void setRunning(boolean run) {
        running = run;
    }

    public void run() {
        while (running) {
            Canvas canvas = null;
            try {
                canvas = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    onDraw(canvas);                     
                }
                canvas.scale(300, 300);
            } catch (Exception e) {
            } finally {
                if (canvas != null) {
                    view.getHolder().unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}
}

这里的一些神奇数字是暂时的。现在我只想移动 BoardView;

4

1 回答 1

0

问题是向 SurfaceView 添加xy字段,更改它们onTouchEvent并使用 x 和 y 偏移在画布中绘制所有元素。奇怪的是我没有收到任何答案...

于 2012-12-02T18:21:09.857 回答