Problem with platformer

edited January 2014 in Questions about Code

Hello,

I'm attempting to create a 2d platformer in Processing in Eclipse. I get this error and the rectangle won't move though:

Exception in thread "Animation Thread" java.lang.NullPointerException
    at processing.core.PApplet.handleDraw(PApplet.java:2337)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:237)
    at processing.core.PApplet.run(PApplet.java:2177)
    at java.lang.Thread.run(Unknown Source)

If it helps here is my code:

package spacefelt.main;

import processing.core.*;

public class eqd extends PApplet {

    private static final long serialVersionUID = 1L;

PImage collideimg;
 PImage displayimg;

 float playerX = 100;
 float playerY = 100;
 float playerXvelocity = 0;
 float playerYvelocity = 0;
 float playerJumpSpeed = -2;
 float playerSpeed = 4;
 float playerSize = 10;
 float up;
 float right;
 float down;
 float left;
 boolean isonGround;
 float grvty = .3f;
 final int W = 800;
 final int H = 600;

 boolean isLTNOverlapping(float lft, float top, float rght, float btm,
         float otherlft, float othertop, float otherrght, float otherbtm) {
     return !(left > otherrght || right < otherlft || top > otherbtm || btm < othertop);
 }


 public  void setup() {
     size(W, H);
     collideimg = requestImage("C:\\Users\\abilitage\\workspace\\Spacefelt3\\imgs\\collide.png");
     displayimg = requestImage("C:\\Users\\abilitage\\workspace\\Spacefelt3\\imgs\\disp.png");
 }

 public void draw() {
     fill(0);

     playerXvelocity += grvty;
     playerXvelocity = (right - left) * playerSpeed;
     float nxtY = playerY + playerYvelocity;
     float nxtX = playerX + playerXvelocity;

     boolean templateOnGround = false;
     if(collideimg.width > 0) {
         image(collideimg, 0, 0, width, height);
     }

     if(displayimg.width > 0) {
         image(displayimg, 0, 0, width, height);
     }

     collideimg.loadPixels();

     for(int y = 0; y < collideimg.height; y += 1) {
         for(int x = 0; x < collideimg.width; x += 1) {
             int pixc = color(collideimg.pixels[y*collideimg.width+x]);
             float scaledifference = width / collideimg.width;
             float px = nxtX;
             float py = playerY;
             float platX = x * (int)scaledifference;
             float platY = y * (int)scaledifference;
             float tiles = 10;
             float playerSize = 10;
             if(isLTNOverlapping(platX, platY, platX + tiles, platY + tiles, px, py, px + playerSize, py + playerSize) && red(pixc) == 0) {
                 if(playerXvelocity < 0 && playerX >= platX + tiles) {
                     playerXvelocity = 0;
                 }

                 if (playerXvelocity > 0 && playerX < platX)
                  {
                    playerXvelocity = 0;
                  }
             }
        px = playerX;
        py = nxtY;

        if (isLTNOverlapping(platX, platY, platX + tiles, platY + tiles, px, py, px + playerSize, py + playerSize) && red(pixc) == 0)
        {
            fill(255, 0, 0);
         }

        if(playerYvelocity > 0 && playerY < platY) {
            playerYvelocity = 0;
            templateOnGround = true;
        }
     }
 }

     playerX += playerXvelocity;
     playerY += playerYvelocity;
     rect(playerX, playerY, playerSize, playerSize);
     isonGround = templateOnGround;
}

 void keyrelease() {
     if (key == CODED)
      {
        if (keyCode == LEFT)
        {
          left = 0;
        }
        if (keyCode == RIGHT)
        {
          right = 0;
        }
        if (keyCode == UP)
        {
          up = 0;
        }
        if (keyCode == DOWN)
        {
          down = 0;
        }
     }
 }

 public void keypressed()
 {
   if (key == ' ')
   {
     if (isonGround == true)
     {
       playerYvelocity = playerJumpSpeed;
     }
   }


   if (key == CODED)
   {
     if (keyCode == LEFT)
     {
       left = 1;
     }
     if (keyCode == RIGHT)
     {
       right = 1;
     }
     if (keyCode == UP)
     {
       up = 1;
     }
     if (keyCode == DOWN)
     {
       down = 1;
     }
   }
 }
}

Answers

  • Not sure why you get an exception, but with names keypressed() and keyrelease(), I don't think you will get correct keyboard handling!

  • does loadPixels (line 57) work with requestImage (line 36)?

    what happens when the image hasn't loaded? is pixels[] empty? null?

    try using loadImage() instead. i'd argue that you should use loadImage anyway as loading the two main graphics is something that should block until it's finished.

  • Make sure that your main class is set to visible (this.setVisible()) before running init on your PApplet sketch.

Sign In or Register to comment.