如何从我的平台游戏中修复这个NullPointerException?

时间:2015-06-05 14:27:31

标签: java animation processing game-physics

我正在尝试制作一个平台游戏,你可以跑来跑去,跳下矩形底座并从它们的侧面跳下来。

我正在处理2.2.1中编译它。该程序无法运行。我也在寻求有关如何实现跳转以及何时更改角色图像的帮助。

speed = 0;  
int numrects = 9;  
Player player1 = new Player();

//Rectangle[] platforms = new Rectangle[16];  
public static Rectangle[] platforms = new Rectangle[16];

class Player{  
PImage[] images = new PImage[10];

 int xMove, yMove, xSpeed, ySpeed, gravity, jumpheight, xSize, ySize;
 boolean a = true;
 boolean d = true;
 boolean w = true;
 int x, y;
 int numFrames = 7;  // The number of animation frames
 int frame = 0;  // The frame to display
 PImage img;


 Player (){
   xSpeed = 10;
   ySpeed = 10;
   xSize = 24;
   ySize = 51;
   x = width/2;
   y = height/2;
 }

 void jump() {
  for (int x = 0; x <= jumpheight; x++){
    y = y + gravity;
    image(images[frame], x, y);

  }
  for (int x = 0; x <= jumpheight; x++){
    y = y - gravity;
    image(images[frame], x, y);

  }
}

};

class Rectangle{

  int x, y, xSize, ySize;
  //.fill(127);
  Rectangle(){
    x = 1;
    y = 1;
    xSize = 10;
    ySize = 10;
  }
  void build(int newx, int newy, int newxSize, int newySize){
    x = newx;
    y = newy;
    xSize = newxSize;
    ySize = newySize;
   rect (x, y, xSize, ySize);
  }
};








void setup() {
size(1920, 1080);
frameRate(30);

player1.images[0] = loadImage("Staticl.png");
player1.images[3] = loadImage("Staticr.png");
player1.images[1] = loadImage("step1left.png");
player1.images[4] = loadImage("step1right.png"); 
player1.images[2] = loadImage("step2left.png");
player1.images[5] = loadImage("step2right.png");
//images[3] = loadImage("1.jpg");
//images[7] = loadImage("2.jpg");

player1.xSpeed = 10; 
player1.x = 500;
player1.y = 500;
platforms[0].build(0, 837, 1920, 244);
platforms[1].build(0, 765, 294, 23);
platforms[2].build(733, 725, 734, 39);
platforms[3].build(0, 765, 294, 23);
platforms[4].build(0, 765, 294, 23);
platforms[5].build(1306, 569, 161, 195);
platforms[6].build(558, 607, 653, 33);
platforms[7].build(0, 522, 496, 34);
platforms[8].build(477, 360, 173, 37);
platforms[9].build(690, 288, 445, 34);
platforms[10].build(1149, 174, 217, 40);
platforms[11].build(1390, 298, 243, 33);
platforms[12].build(1488, 490, 167, 30);
platforms[13].build(1690, 301, 138, 31);
platforms[14].build(1693, 426, 227, 27);
platforms[15].build(1866, 226, 54, 199);

}


   void checkforcollision(){
     for (int x = 0; x <= numrects; x++){
       if (player1.x + player1.xSize == platforms[x].x && player1.y <= platforms[x].y + platforms[x].ySize && player1.y + player1.ySize >= platforms[x].y){
     // right side of box hits left side of platform
     player1.d = false;
     player1.w = true;
   }
  else if(player1.x == platforms[x].x + platforms[x].xSize && player1.y <= platforms[x].y + platforms[x].ySize && player1.y + player1.ySize >= platforms[x].y){
    // left side of box hits right side of platform
    player1.a = false;
    player1.w = true;
  }
  else if(player1.y + player1.ySize == platforms[x].y && player1.x <= platforms[x].x + platforms[x].xSize && player1.x + player1.xSize >= platforms[x].x){
    // bottom of player hits top of box
    player1.w = false;
  }
  else if(player1.y == platforms[x].y + platforms[x].ySize && player1.x <= platforms[x].x + platforms[x].xSize && player1.x + player1.xSize >= platforms[x].x){
    // top of player hits bottom of box     
    player1.w = true;
    player1.a = true;
    player1.d = true;
  }
  else {
   player1.w = false;
   player1.a = true;
   player1.d = true; 
  }
  }
  }



void draw() {


     checkforcollision();   
    if (player1.d == true)
    player1.x = player1.x + player1.xSpeed;
    image(player1.images[player1.frame], player1.x, player1.y);
    if (player1.a == true)
    player1.x = player1.x - player1.xSpeed;
    image(player1.images[player1.frame], player1.x, player1.y);
    if (player1.w == true)
    player1.jump();
    //image(images[frame],  player1.x, player1.y);

}


void keyPressed() {
  if ((key == 'a') || (key == 'A')){
    player1.a = true;
  }
  else if ((key == 'd') || (key == 'D')){
    player1.d = true;
  }
  else if ((key == 'w') || (key == 'W')){
    player1.w = true;
  }
}

void keyReleased() {
  if ((key == 'a') || (key == 'A')){
    player1.a = false;
  }
  else if ((key == 'd') || (key == 'D')){
   player1.d = false;
  }

}

1 个答案:

答案 0 :(得分:1)

NullPointerException是因为您尚未初始化阵列中的Rectangles。所以这一行会导致错误:

platforms[0].build(0, 837, 1920, 244);

首先,你需要运行这样的东西:

for (int i=0; i<platforms.length; i++) {
  platforms[i] = new Rectangle();
}