2017-01-10 46 views
0

我正在研究一个小型的简单游戏,其中从顶部出来的障碍物有一个只能在x轴上移动的静态球。当障碍物从顶部出来,用户必须移动球以避免碰撞。如何在android游戏中一个接一个地从y轴生成位图

我一次放置3个移动障碍物,但我的问题是他们一起出来,即所有三个障碍物都具有相同的y轴值。我希望它以一定的距离一个接一个出来。

我该如何做到这一点。 这里是我的GamePanel类:

public class GamePanel extends SurfaceView implements Runnable { 

    private Thread thread = null; 
    private Ball ball; 
    private SurfaceHolder surfaceHolder; 
    private Paint paint; 
    private Canvas canvas; 

    volatile boolean playing = true; 

    private int hurdleCount = 3; 
    private Hurdles[] hurdles; 

    private int screenX, screenY; 
    private Rect ball_detectCollision; 

    public GamePanel(Context context, final int screenX, final int screenY) { 
     super(context); 

     ball = new Ball(context, screenX, screenY); 
     surfaceHolder = getHolder(); 

     this.screenX = screenX; 
     this.screenY = screenY; 

     paint = new Paint(); 
     canvas = new Canvas(); 

     hurdles = new Hurdles[hurdleCount]; 
     for (int i = 0; i < hurdleCount; i++) { 
      hurdles[i] = new Hurdles(context, screenX, screenY); 
     } 

     ball_detectCollision = new Rect(ball.getBall_x(), ball.getBall_y(), ball.getBitmap().getWidth(), ball.getBitmap().getHeight()); 
     surfaceHolder.addCallback(new SurfaceHolder.Callback() { 

      @Override 
      public void surfaceCreated(SurfaceHolder holder) { 

       System.out.println("Surface Created"); 

      } 

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

       System.out.println("Surface Changed"); 
       thread = new Thread(GamePanel.this); 
       thread.start(); 
      } 

      @Override 
      public void surfaceDestroyed(SurfaceHolder holder) { 

       System.out.println("Surface Destroyed"); 
      } 
     }); 
    } 


    private void draw() { 

     canvas = surfaceHolder.lockCanvas(); 
     canvas.drawColor(Color.RED); 
     canvas.drawBitmap(ball.getBitmap(), updatedValue, ball.getBall_y(), paint); 
     ball.setBall_x(updatedValue); 

     ball_detectCollision.left = ball.getBall_x(); 
     ball_detectCollision.top = screenY - ball.getBitmap().getHeight() - 260; 
     ball_detectCollision.right = ball.getBall_x() + ball.getBitmap().getWidth(); 
     ball_detectCollision.bottom = screenY - ball.getBitmap().getHeight() - 260 + ball.getBitmap().getHeight(); 

     for (int i = 0; i < hurdleCount; i++) { 
      canvas.drawBitmap(hurdles[i].getBitmap(), hurdles[i].getX(), hurdles[i].getY(), paint); 
     } 
     surfaceHolder.unlockCanvasAndPost(canvas); 
    } 

    @Override 
    public void run() { 

     while (playing) { 
      update(); 
      draw(); 
      control(); 
     } 
    } 

    private void update() { 

     for (int i = 0; i < hurdleCount; i++) { 
      hurdles[i].update(); 

      if (Rect.intersects(getBall_detectCollision(), hurdles[i].getDetectCollision())) { 
       System.out.println("Collision Detected"); 

       playing = false; 
       Handler handler = new Handler(Looper.getMainLooper()); 
       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         showGameOverMessage(); 

        } 
       }); 
      } 
     } 
    } 


    public void pause() { 

     playing = false; 
     try { 
      thread.join(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

这是我的跨栏类

public class Hurdles { 

     private Bitmap bitmap; 
     private int x; 
     private int y; 
     private int speed = 20; 

     private int maxX; 
     private int minX; 

     private int maxY; 
     private int minY; 

     private Rect detectCollision; 

     public Hurdles(Context context, int screenX, int screenY) { 

      bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.hurdle); 
      maxX = screenX - bitmap.getWidth(); 
      maxY = screenY; 
      minX = 0; 
      minY = 0; 
      Random generator = new Random(); 
      detectCollision = new Rect(x, y, bitmap.getWidth(), bitmap.getHeight()); 

      x = generator.nextInt(maxX); 
      y = minY; 
     } 

     public void update() { 
      y += speed; 

      if (y > maxY - getBitmap().getHeight()) { 
       Random generator = new Random(); 
       y = minY; 
       x = generator.nextInt(maxX); 
      } 

      detectCollision.left = x; 
      detectCollision.right = x + bitmap.getWidth(); 
      detectCollision.top = y; 
      detectCollision.bottom = y + bitmap.getHeight(); 
     } 

回答

1

如果你想增加障碍之间的延迟/差距,你可以在你的GamePanel构造,如:

public GamePanel(Context context, final int screenX, final int screenY) { 
    super(context); 

    int gapBetweenHurdles = 100; 
    int gap = 0; 
    for (int i = 0; i < hurdleCount; i++) { 
     //(screenY - gap) will move your hurdle above the screen 
     hurdles[i] = new Hurdles(context, screenX, screenY - gap); 
     //increment the gap 
     gap += gapBetweenHurdles; 
    } 
...... 
} 

所以,栏间的差距为100个像素,因为我已经写随机。如果你想要特定的差距,你可以将gapBetweenHurdles设置为屏幕高度的某个百分比。

编辑: 你必须初始XY位置传递给关构造函数,然后在跨栏类增量Y值和跨栏类,getY()应该返回Y的更新方法。

+0

当它穿过球对象 – AbhayBohra

+0

你的球对象在屏幕右下角时,它的工作但障碍消失? –

+0

号码从屏幕底部向上移动 – AbhayBohra

0

尝试改变这样的代码:

handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         showGameOverMessage(); 

        } 
       },timeOffsetInMillis); 

如果间隙可以是相同的,你可以设置timeOffsetInMillis = i * gapInMillis

相关问题