Help with instance mode?


#1

Hello,

I am having trouble using instance mode. I would like to convert this code for a mini game into instance mode so that I can reference it in a bigger game file that I am coding. I am not sure if I am using the instance mode right at all. I am using the coding train as reference.

Update: I have updated my code but I am still receiving an error.

let sketch = function( p) {

  let blob;

  let blobs = [];
  let zoom = 1;

  let timer = 20;
  let hits = false;

  let score = 0;

p.setup = function() {
  p.createCanvas(600, 600);
  blob = new Blob(0, 0, 64);
  for (let i = 0; i < 300; i++) {
    let x = p.random(-p.width,p.width);
    let y = p.random(-p.height,p.height);
    blobs[i] = new Blob(x, y, 15);
  }
};

p.draw = function() {
  p.background(0);
  p.stroke(0, 100, 100);
  p. line(800, 150, 800, 650);
  p.textSize(200);
  p.text("SCORE:", 10,180);
  p.textSize(200);
  p.text(p.score, 200,380);

  p.translate(width/2, height/2);
 	let newzoom = 64 / blob.r;
  p.zoom = p.lerp(zoom, newzoom, 0.1);
  p.scale(zoom);
  p.translate(-blob.pos.x, -blob.pos.y);

  for (var i = blobs.length-1; i >=0; i--) {
    blobs[i].show();
    if (blob.eats(blobs[i])) {
      blobs.splice(i, 1);
    }
  }
  
  if (frameCount % 60 == 0 && timer > 0) { // if the frameCount is divisible by 60, then a second has passed. it will stop at 0
    p.timer --;
  }
  
  if (timer == 0 && score >=250) {
    p.text("You Win", 0, 0);
		p.noLoop();
  }
  
  if (blob.eats){
    p.console.log("hit");
  }
  		if (timer == 0 && score <= 250){
		p.text("You Lose", 0, 0);
		p.textSize(200);
		p.noLoop();
		}



  blob.show();
  blob.update();

};
  
};
let myp5 = new p5(sketch);

#2

p5js.org/examples/instance-mode-instantiation.html