Hi there!
Working on a project for uni and have encountered an issue. Not sure whether i've done something wrong, whether the processing.js website im using is wrong or something else all together.
Essentially i have the code below which runs perfectly fine within processing the program (using the JavaScript mode, this emulates processing.js correct?) however when I transfer it to the website to run through actual processing.js it appears to break. The error I get is "TypeError: a is undefined" This error started to appear after i changed the ArrayLists from arrays and also after I implemented the separate method within the Butterfly Class.
Any help would be great. If you need any more info just ask.
Sorry about the messy script :P
float sizeRatio = 1; //ratio for all sizes. This scales the animation including how many butterflies there are, how much food // how fast each should move (randomness factor is scaled down), turn speed, physical scales etc. // 1 = 1000x414 like the front page should be.
color lightBlue = color(66, 168, 237); color darkBlue = color(0, 102, 153); color orange = color(204, 102, 0); int[] colors = { lightBlue, darkBlue, orange }; PGraphics backgroundPattern;
PFont chunkfive; String tagline = "Learn to Program. Creatively.";
int numB = floor(4*sizeRatio); //number of Butterflys int numF = ceil(numB/2); // Number of Food in the system, proportinate to the number of butterflys so as to keep a balance. int totalDeaths = 0; Food[] foods = new Food[numF]; //create array for the food.
int foodCreated = numF; // how much food is in the system.
boolean drawBackground = true;
class Food { PVector location; boolean eaten; int r;
Food() { location = new PVector(random(width), random(height)); eaten = false; r = round(random(5, 25)); }
void recreate() { if (eaten) { location = new PVector(random(width), random(height)); r = round(random(5, 25)); eaten = false; for (int k = 0; k < butterflys.size(); k++) { Butterfly b = butterflys.get(k); int closestFood = b.distanceToClosest(); b.setFoodSource(foods[closestFood]); } } }
class Butterfly { PVector location; PVector velocity; PVector acceleration;
Food foodSource;
int life; float distance; float maxSpeed; float maxForce; float[] distances = new float[numF]; boolean dead; float lifeOpacity; float angle;
Butterfly() { location = new PVector(random(width), random(height)); acceleration = new PVector(0, 0); velocity = new PVector(0, 0); life = 10; //life for each butterfly maxSpeed = random(3, 10)*sizeRatio; maxForce = 0.5; dead = false; }
void update() { if (!dead) { lifeOpacity = map(life, 0, 10, 0, 1); velocity.add(acceleration); velocity.limit(maxSpeed); location.add(velocity); acceleration.mult(0); lifeCheck(); if (!foodSource.isEaten()) { seek(foodSource.location); } if (frameCount % 100 == 0) { // every 2 seconds remove a life point. life--; }
distance = PVector.dist(location, foodSource.location); // used to calculate hit test. } else { reCreate(); //if dead reCreate. } } void seek(PVector target) { PVector desired = PVector.sub(target, location); desired.normalize(); desired.mult(maxSpeed); //PVector steer = PVector.sub(desired, velocity); desired.sub(velocity); desired.limit(maxForce); applyForce(desired); }
void separate(ArrayList<Butterfly> butterflys) { //println("test"); float desiredseparation = 50; PVector sum = new PVector(); int count = 0; for (Butterfly other : butterflys) { float d = PVector.dist(location, other.location);
// void fastedRoute() { //Perhaps add width or height to the location of the food. //send the dot towards the new foods location while retaining the old foods location image. //once it hits the side it would transfer over and then force it to find the closest food.
void checkEdges() { if (location.x >= width) { location.x = 1; int foodNumber = distanceToClosest(); setFoodSource(foods[foodNumber]); } else if (location.x <= 0) { location.x = width-1; int foodNumber = distanceToClosest(); setFoodSource(foods[foodNumber]); } if (location.y >= height) { location.y = 1; int foodNumber = distanceToClosest(); setFoodSource(foods[foodNumber]); } else if (location.y <= 0) { location.y = height-1; int foodNumber = distanceToClosest(); setFoodSource(foods[foodNumber]); } } } //End dot/butterfly Class float xpos; PGraphics pg; ArrayList<backgroundDot> dots; //craete the arraylist with object type backgroundDot ArrayList<Butterfly> butterflys; //create the arraylist with object type Butterfly void setup() { size(1000, 414); background(255);
frameRate(50); pg = createGraphics(width, height); pg.beginDraw(); pg.rect(0, 0, width, height); pg.endDraw(); // backgroundPattern = createGraphics(1000, 414); butterflys = new ArrayList<Butterfly>(); dots = new ArrayList<backgroundDot>(); // dots = new ArrayList<backgroundDot>(); for (int i = 0; i < numB; i++) { butterflys.add(new Butterfly());
dots.add(new backgroundDot());
backgroundDot d = dots.get(i); Butterfly b = butterflys.get(i); d.getButterfly(b); } for (int i = 0; i < foods.length; i++) { foods[i] = new Food(); foodCreated++; } for (int k = 0; k < butterflys.size(); k++) { Butterfly b = butterflys.get(k); int closestFood = b.distanceToClosest();
b.setFoodSource(foods[closestFood]); }
int taglineHeight = 3*height/40; chunkfive = createFont("/media/css/Chunkfive-webfont.ttf", taglineHeight); }
void draw() { //String time = round(millis()/1000); //Normal timer. Testing how long it can run for. Should be infinite. //String framerate = (round(frameRate)); // Fade the background noStroke(); fill(255, 255); rect(width/2, height/2, width, height);
if (drawBackground) { image(pg, 0, 0); } else { } for (int i = 0; i<butterflys.size(); i++) { Butterfly b = butterflys.get(i); b.separate(butterflys); b.run(); }
// every once in a while create some food. if (frameCount % 200 == 0 && foodCreated<numF) { createFood(); } for (int i = 0; i< foods.length; i++) { foods[i].display(); }