hi,
the array seems to not work for every other object; on the second click, another object is spawned even if the click is outside of the contains/bounds of the shape.
thanks to anyone who takes the time to read/respond to this!
var breads=[];
var stars=[];
let title;
let boom;
function preload(){
roll = loadImage("bread.PNG");
title = loadImage("IMG_1247.PNG");
boom = loadImage("IMG_0187.GIF");
}
function setup() {
createCanvas(600, 600);
imageMode(CENTER);
for (let i=0;i<1;i++){
let x = random(width);
let y = random(height);
let r = (35);
let b = new Rolly(x,y,r);
breads.push(b);
}
}
function draw() {
background(0);
image(title,width/2,height/2,600,600);
for (var i = 0; i < stars.length; i++){
stars[i].show();
}
for (let i = 0; i < breads.length; i++){
breads[i].show();
if (breads[i].contains(mouseX,mouseY))
{
cursor(HAND);
} else {
cursor(ARROW);
}
}
}
function mouseClicked(){
for (let i = breads.length - 1; i >= 0; i--) {
if (breads[i].contains(mouseX,mouseY))
{
breads.splice(i,1);
// stars.push(new Star(mouseX,mouseY));
}
for (let i=0; breads.length < 1; i++){
let x = random(width);
let y = random(height);
let r = (35);
let b = new Rolly(x,y,r);
breads.push(b);
stars.push(new Star(mouseX,mouseY));
}
}
}
function mouseReleased(){
boom.reset();
stars.pop();
}
class Star{
constructor(x,y){
this.x = x;
this.y = y;
}
show(){
scale(0.8);
stroke(255);
fill(125);
image(boom,this.x,this.y-70);
}
}
class Rolly {
constructor(x,y,r){
this.x = x;
this.y = y;
this.r = r;
}
contains(px,py){
let d = dist(px,py,this.x,this.y);
if (d < this.r){
return true;
} else {
return false;
}
}
show(){
stroke(255);
strokeWeight(4);
// ellipse(this.x,this.y,this.r*2);
image(roll,this.x,this.y,120,120);
}
}