简体   繁体   English

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

[英]How to fix this NullPointerException from my Platformer Game?

I am trying to make a Platformer game where you can run around and jump off the base of rectangles and off the sides of them. 我正在尝试制作一个Platformer游戏,您可以在该游戏中跑来跑去,跳出矩形的底部并跳出矩形的侧面。

I am compiling this in Processing 2.2.1. 我正在处理2.2.1中对此进行编译。 The program does not run. 该程序无法运行。 I am also looking for help on how to implement a jump and when to change the character's image. 我也在寻求有关如何实现跳跃以及何时更改角色形象的帮助。

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;
  }

}

The NullPointerException is because you haven't initialized the Rectangles in the array yet. NullPointerException是因为您尚未在数组中初始化Rectangles So this line causes an error: 所以这行会导致错误:

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

First, you need to run something like this: 首先,您需要运行以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM