2012-12-02 2 views
0

BSD robots과 같은 간단한 게임을 개발 중입니다. 이 게임은 40 개가 넘는 대형 보드를 포함하고 있으며 10 인치 태블릿에서도 멋지지 않습니다. 내 결정은 표면보기를 스크롤 (이동)하고 손가락 크기와 같은 크기의 각 그림을 크기 조정하지 않기로 결정한 것입니다. 표면보기에 대한 기사를 읽었지만 이동 방법을 이해할 수 없습니까?Android에서 SurfaceView 이동

내 활동 코드 :

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를 옮기고 싶습니다.

답변

0

문제는 x 및필드를 SurfaceView에 추가하고 onTouchEvent으로 변경하고 캔버스의 모든 요소를 ​​x 및 y 오프셋으로 그립니다. 내가 답을 얻지 못했다는 것이 이상하다 ...