I've been looking at this for probably 5 hours now, I need help.
There are two classes:
Scroller and a subclass Cursor.
The goal is to make the background move underneath the Cursor object, and the Cursor will detect the color of a single pixel from the background it is standing over.
But for some reason the redCheck method in the Cursor class is stopping here:
int red = getWorld().getColorAt(getX(), getY() - 5).getRed();
and giving me a null pointer exception. I can't seem to understand what it's not finding. I've done a check to verify that the background is seen, and it is. It can't seem to find the red color, but i don't know why.
Here's the code for Scroller, it's mostly movement code that makes one call to the redCheck method in the Cursor object:
And here's the code for the Cursor:
Thanks to help earlier I solved one problem, but now I'm stuck on this one.
public void checkKeyPress() { timer++; if(Greenfoot.isKeyDown("W") && timer >= 10) { setLocation(getX(), getY() + MOVEDISTANCE); ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck(); if(whereAmI == "end") { setLocation(getX(), getY() - MOVEDISTANCE); } if(whereAmI == "forest" && forester == false) { setLocation(getX(), getY() - MOVEDISTANCE); } else if(whereAmI == "mountain" && mountaineer == false) { setLocation(getX(), getY() - MOVEDISTANCE); } else if(whereAmI == "water" && boat == false) { setLocation(getX(), getY() - MOVEDISTANCE); } //randomEncounter = randomEncounter + 1; timer = 0; } else if(Greenfoot.isKeyDown("A") && timer >= 10) { setLocation(getX() + MOVEDISTANCE, getY()); ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck(); if(whereAmI == "end") { setLocation(getX() - MOVEDISTANCE, getY()); } if(whereAmI == "forest" && forester == false) { setLocation(getX() - MOVEDISTANCE, getY()); } else if(whereAmI == "mountain" && mountaineer == false) { setLocation(getX() - MOVEDISTANCE, getY()); } else if(whereAmI == "water" && boat == false) { setLocation(getX() - MOVEDISTANCE, getY()); } //randomEncounter = randomEncounter + 1; timer = 0; } else if(Greenfoot.isKeyDown("S") && timer >= 10) { setLocation(getX(), getY() - MOVEDISTANCE); ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck(); if(whereAmI == "end") { setLocation(getX(), getY() + MOVEDISTANCE); } if(whereAmI == "forest" && forester == false) { setLocation(getX(), getY() + MOVEDISTANCE); } else if(whereAmI == "mountain" && mountaineer == false) { setLocation(getX(), getY() + MOVEDISTANCE); } else if(whereAmI == "water" && boat == false) { setLocation(getX(), getY() + MOVEDISTANCE); } //randomEncounter = randomEncounter + 1; timer = 0; } else if(Greenfoot.isKeyDown("D") && timer >= 10) { setLocation(getX() - MOVEDISTANCE, getY()); ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck(); if(whereAmI == "end") { setLocation(getX() + MOVEDISTANCE, getY()); } if(whereAmI == "forest" && forester == false) { setLocation(getX() + MOVEDISTANCE, getY()); } else if(whereAmI == "mountain" && mountaineer == false) { setLocation(getX() + MOVEDISTANCE, getY()); } else if(whereAmI == "water" && boat == false) { setLocation(getX() + MOVEDISTANCE, getY()); } //randomEncounter = randomEncounter + 1; timer = 0; } if(timer > 20) { timer = 10; } }
public void redCheck() { Background bg = ((Background)getWorld().getObjects(Background.class).get(0)); if(bg != null) { System.out.println("bg detected"); } int red = getWorld().getColorAt(getX(), getY() - 5).getRed(); System.out.println(red); if(red == 95) { whereAmI = "forest"; } else if(red == 195) { whereAmI = "cave"; } else if(red == 171) { whereAmI = "castle"; } else if(red == 0) { whereAmI = "water"; } else if(red == 198) { whereAmI = "town"; } else if(red == 220) { whereAmI = "path"; } else if(red == 166) { whereAmI = "mountain"; } else if(red == 145) { whereAmI = "end"; } }