More errors to be fixed
in
Programming Questions
•
7 months ago
Hi,
This problem has mainly to do with ArrayLists; I really don't know how to use them. I've had to scrap a project that I spent a nice amount of time on because ArrayLists didn't co-operate, and I think I'm scrapping the next one. My inability to work with them has basically stopped me from learning how to code more complicated things. So, before I scrap this project, I was wondering if anyone knew how to fix the problems I made. Basically, I'm stuck on getting the mover (myFirstMover, from the class Mover) to go towards an ArrayList of "targets," (Classes Target and TargetManager) the first one being the mouse (Class MousePointer). Sadly, I can't give you much information on why the problem is happening, other than a nullPointerError in void Behavior in the Mover class, and probably the Mover just not going towards the mouse (If my current luck holds out). Again, I'm sorry for the probably very obvious error.
Anyways, here's the code:
Main sketch:
- Mover myFirstMover;
- TargetManager tManager;
- MousePointer pointer;
- void setup() {
- frameRate(15);
- myFirstMover = new Mover(width/2,height/2);
- tManager = new TargetManager();
- pointer = new MousePointer();
- size(300,300);
- }
- void draw() {
- background(0);
- myFirstMover.Display();
- myFirstMover.Update();
- myFirstMover.Movement();
- myFirstMover.Emoticon();
- myFirstMover.Behavior();
- tManager.Manage();
- pointer.Display();
- pointer.Update();
- }
Mover class:
- import java.util.Iterator;
- class Mover { //Class for the movers
- PVector location,targetLocation; //Location of the mover, and of the target location
- float hungerLevel,thirstLevel,happinessLevel; //How humgry, thirsty, and happy the mover is, the higher it gets the more likely the mover is to seek water/food, and if it is high enough, the mover will die
- float fear; //Various factors that affect the behavior of the mover
- ArrayList standing;
- int viewDistance; //Radius of detection for items such as food and water
- boolean target; //Whetehr or not the mover is activley moving towards something
- int speed,movementTimer,movementDir; //Speed relates to how fast the movement timer increases, and movementTimer/movementDir are used in Movement() to trigger a movement
- Mover(float x, float y) { //Constructor
- location = new PVector(x,y);
- hungerLevel = 5;
- thirstLevel = 0;
- happinessLevel = 5;
- standing = new ArrayList();
- viewDistance = 5;
- target = false;
- speed = round(random(4,8)); //Randomises the speed for each mover
- }
- void Display() { //Shows the mover
- if(happinessLevel<4) { //These conditionals change the color of the mover based on its happiness
- stroke(255,0,0);
- }
- else if(happinessLevel>=4&&happinessLevel<=8) {
- stroke(255,255,255);
- }
- else if(happinessLevel>8) {
- stroke(0,255,0);
- }
- rectMode(CENTER);
- rect(location.x,location.y,3,3); //Draws the point
- }
- void Update() { //Performs various changes in miscelanious values, including managing hunger, thirst, and happiness
- movementTimer+=speed;
- hungerLevel+=0.0005;
- thirstLevel+=0.0012;
- if(hungerLevel<2&&happinessLevel<10) {
- happinessLevel+=0.006;
- }
- else if(hungerLevel>5&&happinessLevel>0) {
- happinessLevel-=0.01;
- }
- if(thirstLevel<2&&happinessLevel<10) {
- happinessLevel+=0.006;
- }
- else if(thirstLevel>5&&happinessLevel>0) {
- happinessLevel-=0.012;
- }
- }
- void Behavior() { //Behavior regarding targets, other movers etc
- Iterator<Target> mTargets = tManager.targets.iterator();
- while(mTargets.hasNext()) {
- Target t = mTargets.next();
- //if((sq(t.location.x-location.x))+(sq(t.location.y-location.y))<sq(viewDistance)) {
- if(t.type=="Water"&&thirstLevel>5) {
- targetLocation.x=t.location.x-location.x;
- targetLocation.y=t.location.y-location.y;
- target=true;
- }
- else if(t.type=="Food"&&hungerLevel>5) {
- targetLocation.x=t.location.x-location.x;
- targetLocation.y=t.location.y-location.y;
- target=true;
- }
- else if(t.type=="Waypoint") {
- targetLocation.x=t.location.x-location.x;
- targetLocation.y=t.location.y-location.y;
- target=true;
- }
- else {
- target=false;
- //}
- }
- }
- }
- boolean Alive() { //True/false for whether or not the mover is alive, if it is false the mover is removed
- if(hungerLevel<10&&thirstLevel<10) { //Checks the hunger/thirst levels
- return true;
- }
- else {
- return false;
- }
- }
- void Movement() { //Performs movements for the mover
- if(movementTimer>50&&target==false) { //Counter for moving it every cerian ammount of time, and making sure it has no target, as this is for randomly moving
- movementDir=floor(random(5)); //Randomises the direction
- if(movementDir==0) { //Right
- location.x++;
- movementTimer=0;
- }
- else if(movementDir==1) { //Down
- location.y++;
- movementTimer=0;
- }
- else if(movementDir==2) { //Left
- location.x--;
- movementTimer=0;
- }
- else if(movementDir==3) { //Up
- location.y--;
- movementTimer=0;
- }
- else { //Mover is resting
- movementTimer=-100;
- }
- }
- else if(movementTimer>50&&target==true) {
- targetLocation.normalize();
- targetLocation.mult(1);
- location.add(targetLocation);
- }
- }
- void CheckEdges() { //Bounces the mover back if they try to wander off screen
- if(location.x<0) {
- location.x++;
- }
- else if(location.x>width) {
- location.x--;
- }
- if(location.y<0) {
- location.y++;
- }
- else if(location.y>height) {
- location.y--;
- }
- }
- void Emoticon() { //DIsplays an "emoticon" above the mover to show why it is happy/unhappy
- float emoticonCycler=0;
- if(thirstLevel>5&&emoticonCycler>0&&emoticonCycler<1) {
- stroke(#0F79CE,150);
- point(location.x,location.y-1);
- }
- if(hungerLevel>5/*&&emoticonCycler>1&&emoticonCycler<2*/) {
- stroke(#0F79CE,150);
- point(location.x,location.y-1);
- }
- emoticonCycler+=0.25;
- if(emoticonCycler>5) {
- emoticonCycler=0;
- }
- }
- }
Target class:
- class Target {
- PVector location;
- float urgency;
- String type;
- boolean active;
- Target() {
- }
- }
TargetManager class:
- import java.util.Iterator;
- class TargetManager {
- ArrayList<Target> targets;
- TargetManager() {
- targets = new ArrayList<Target>();
- targets.add(new MousePointer());
- }
- void Manage() {
- Iterator<Target> mTargets = targets.iterator();
- while(mTargets.hasNext()) {
- Target t = mTargets.next();
- }
- }
- }
MousePointer class:
- class MousePointer extends Target {
- MousePointer() {
- location = new PVector(mouseX,mouseY);
- type = "Waypoint";
- }
- void Display() {
- stroke(255);
- line(mouseX,mouseY-5,mouseX,mouseY+5);
- line(mouseX-5,mouseY,mouseX+5,mouseY);
- }
- void Update() {
- location.x=mouseX;
- location.y=mouseY;
- }
- }
1