改变对象变量的问题

时间:2018-10-11 13:49:11

标签: java processing

我的问题是,当一个球物体与屏幕的左侧,右侧或顶部碰撞时,或者与任一块砖块碰撞时,它都会反转所有球的速度,例如,如果球1撞击左壁,它将在相反的X方向弹回。但是,即使第2球不与任何东西碰撞,第2球也会做同样的事情(第2球碰到东西时第1球也会发生同样的事情)。

我正在使用Processing开发环境。但是,它使用Java,特别是PApplet类

这是我移动球的方法:

void moveBalls(){
PVector ballPos;
for(balls b : ballsCollection){
  ballPos = b.getPos();
  if(b.isMoving()) {
    //make the ball bounce off the walls  
    if(ballPos.y > height - blockSize){ //bottom
      if(ballsInMotion == ballsCollection.size()){
         ballPos.y = height - ballSize/2 - blockSize - 3;
         ballStartX = ballPos.x;
         ballStartY = height - blockSize - ballSize - ballSize/2;
         b.invertMoving();
         ballsInMotion -=1;
         println("FirstBall down" + ballPos);
      } else {
         b.invertMoving();
         ballsInMotion -=1;
         PVector p = new PVector(ballStartX,ballStartY);
         b.setPos(p);
         println("Next ball Down new P: " + p);
      }
      if(ballsInMotion == 0){
         setUpNextRound = true; 
      }
    }
    if (ballPos.y < ballSize + blockSize){ //top
       b.invertV("y");
       ballPos.y = ballSize+blockSize;
       println("ball " + b.getIndex() + " hit Top");
    }
    if (ballPos.x > width - ballSize/2){ //right
       b.invertV("x"); 
       ballPos.x = 2*width-ballSize-ballPos.x;
       println("ball " + b.getIndex() + " hit right");
    }
    if (ballPos.x < ballSize/2){ //left
       b.invertV("x"); 
      ballPos.x = ballSize-ballPos.x;
      println("ball " + b.getIndex() + " hit left");
    }

    for(blocks block : blocksCollection){
      int blockHealth = block.getBlockHealth();
      int blockX = block.getX();
      int blockY = block.getY();
      boolean hit = false;
       if(blockHealth > 0){
                 //     Bottom of the block                                                                                                                            Top of the Block       
         if(!hit && (blockBallCollision(blockX+blockSize/2, blockY+blockSize, blockSize-blockCournerSize, 0, ballPos.x-b.getV().x, ballPos.y-b.getV().y, 2*(ballSize)) || blockBallCollision(blockX+blockSize/2, blockY, blockSize-blockCournerSize, 0, ballPos.x-b.getV().x, ballPos.y-b.getV().y, 2*(ballSize)))) {

                println("Block Collision Top/Bottom: " + block.getIndex() + " by Ball: " + b.getIndex());

                b.invertV("y");
                block.blockHit();
                hit = true;
                 //     Left of the block                                                                                                  Right of the Block 
           } else if ( !hit && (blockBallCollision(blockX, blockY+blockSize/2, 0, blockSize-blockCournerSize, ballPos.x-b.getV().x, ballPos.y-b.getV().y, 2*(ballSize)) || blockBallCollision(blockX+blockSize, blockY+blockSize/2, 0, blockSize-blockCournerSize, ballPos.x-b.getV().x, ballPos.y-b.getV().y, 2*(ballSize)))){

               println("Block Collision Left/Right: " + block.getIndex() + " by Ball: " + b.getIndex());
               b.invertV("x");
               block.blockHit();
               hit = true;
           }
      }
    }
      b.movePosV();
      drawBalls();
    }

}

}

存储球对象:

    public class balls{
  private int index;
  private PVector b = new PVector();
  private PVector bV = new PVector();
  private boolean moving, needMoving;

  public balls(int Index, float X, float Y, boolean m, PVector ballV){
      index = Index;
      b.x = X;
      b.y = Y;
      moving = m;
      bV = ballV;
  }
  public void movePosV(){
     b.sub(bV); 
  }
  public void invertV(String xy){
      if(xy.equals("x")){
        bV.x = -bV.x;
      } else {
        bV.y = -bV.y;
      }
  }
  public void setPos(PVector newB){
    b = newB;  
  }
  public void setV(PVector newV){
     bV = newV; 
  }
  public boolean getNeedMoving(){
     return needMoving; 
  }
  public PVector getV(){
     return bV; 
  }
  public PVector getPos(){
     return b; 
  }
  public int getIndex(){
      return index;   
  }
  public boolean isMoving(){
      return moving;
  }
  public void invertMoving(){
      moving = !moving;
  }
  public void invertNeedMoving(){
      needMoving = !needMoving;
  }

}

0 个答案:

没有答案