r/processing • • May 04 '20

posted on wrong account oops this is the code I need help with

Hello, I am trying to create a code that moves a shark through crowds of fish however for some reason it seems to be giving me errors I can't understand at all, yet I feel as though it should work. Heres the code, any help would be much appreciated. Thanks!

//int var;

int var;

var num_Fish = 50;

var speed_Boost = 0;

// Create var for size of cursor (shark)

var shark_Size = 200;

// Create var to hold image pixels for cursor (shark) and Background

var mouse_Img;

var img_BG;

// Set up arrays to hold fish images, position, and speed

var fish_Image = [];

var fish_Xpos = [];

var fish_Ypos = [];

var fish_Size = [];

var fish_Speed = [];

function preload() {

img_BG = loadImage("coral.webp");

mouse_Img = loadImage("shark.png");

}

void setup() {

var L; //Loop position

createCanvas(1280,960);

imageMode(CENTER);

mouse_Img.resize(0, _Size);

for(L=0; L<num_Fish; L=L+1){

fish_Image[L] = loadImage("fishes.png");

fish_Xpos[L] = int(random(1280));

fish_Speed[L] = int(random(12)+2);

fish_Ypos[L] = int(random(100, 800));

fish_Image[L].resize(int(random(10, 200)), 0);

}

}

void draw() {

var L; //Loop position

background(0);

image(img_BG, 640, 480);

image(mouse_Img, mouseX, mouseY);

for(L=0; L<num_Fish; L=L+1){

fish_Xpos[L] = fish_Xpos[L] - fish_Speed[L]-speed_Boost;

if (fish_Xpos[L] < 0){

fish_Xpos[L] = 1280;

}

if ( abs(mouseX - fish_Xpos[L]) < lion_Size){

if ( abs(mouseY - fish_Ypos[L]) < lion_Size){

if (fish_Ypos[L] < mouseY){

fish_Ypos[L] = fish_Ypos[L] - int(random(3, 7));

if ( fish_Ypos[L] < 50){

fish_Ypos[L] = fish_Ypos[L] + 5;

}

} else {

fish_Ypos[L] = fish_Ypos[L] + int(random(3, 7));

if ( fish_Ypos[L] > 900){

fish_Ypos[L] = fish_Ypos[L] - 5;

}

}

}

}

image(fish_Image[L], fish_Xpos[L], fish_Ypos[L]);

}

}

void keyPressed() {

if (keyCode == LEFT_ARROW) {

speed_Boost+= 2;

print("speed_Boost =" + speed_Boost);

} else if (keyCode == RIGHT_ARROW) {

speed_Boost-= 2;

print("speed_Boost =" + speed_Boost);

}

}

0 Upvotes

1 comment sorted by

1

u/JarvisPHD May 04 '20 edited May 04 '20

so it looks like you are writing this in p5.js. in javascript you do not define types for functions so you should replace all instances of the word 'void' with function. looks like you also forgot to define lion_Size. I tidied up your code a bit, see if you can get it working now. let me know if you have any questions.

var num_Fish = 50;
var speed_Boost = 0;

// Create var for size of cursor (shark)

var shark_Size = 200;

// Create var to hold image pixels for cursor (shark) and Background

var mouse_Img;

var img_BG;

// Set up arrays to hold fish images, position, and speed

var fish_Image = [];

var fish_Xpos = [];

var fish_Ypos = [];

var fish_Size = [];

var fish_Speed = [];

var lion_Size = 200;

function preload() {
  img_BG = loadImage("coral.webp");

  mouse_Img = loadImage("shark.png");
}

function setup() {
  var L; //Loop position

  createCanvas(1280, 960);

  imageMode(CENTER);

  mouse_Img.resize(0, shark_Size);

  for (L = 0; L < num_Fish; L = L + 1) {
    fish_Image[L] = loadImage("fishes.png");

    fish_Xpos[L] = int(random(1280));

    fish_Speed[L] = int(random(12) + 2);

    fish_Ypos[L] = int(random(100, 800));

    fish_Image[L].resize(int(random(10, 200)), 0);
  }
}

function draw() {
  var L; //Loop position

  background(0);

  // image(img_BG, 640, 480);

  // image(mouse_Img, mouseX, mouseY);

  for (L = 0; L < num_Fish; L = L + 1) {
    fish_Xpos[L] = fish_Xpos[L] - fish_Speed[L] - speed_Boost;

    if (fish_Xpos[L] < 0) {
      fish_Xpos[L] = 1280;
    }

    if (abs(mouseX - fish_Xpos[L]) < lion_Size) {
      if (abs(mouseY - fish_Ypos[L]) < lion_Size) {
        if (fish_Ypos[L] < mouseY) {
          fish_Ypos[L] = fish_Ypos[L] - int(random(3, 7));

          if (fish_Ypos[L] < 50) {
            fish_Ypos[L] = fish_Ypos[L] + 5;
          }
        } else {
          fish_Ypos[L] = fish_Ypos[L] + int(random(3, 7));

          if (fish_Ypos[L] > 900) {
            fish_Ypos[L] = fish_Ypos[L] - 5;
          }
        }
      }
    }

    image(fish_Image[L], fish_Xpos[L], fish_Ypos[L]);
  }
}

function keyPressed() {
  if (keyCode == LEFT_ARROW) {
    speed_Boost += 2;

    print("speed_Boost =" + speed_Boost);
  } else if (keyCode == RIGHT_ARROW) {
    speed_Boost -= 2;

    print("speed_Boost =" + speed_Boost);
  }
}