hello everyone im working on a project with posenet and p5js and i want when my hand is above my shoulder i append a number inside of an array the porblem is its keep adding the number multiple times before stoping and my goal is to just add it one time anyone here have an idea to stop it.
1 Like
Hi,
The solution is to use a boolean value :
var inserted = false;
if(hands above your shoulder){
// If you didn't already inserted a number
if(!inserted){
// Insert your number here
inserted = true;
}
}else{ //Otherwise set inserted to false
inserted = false;
}
2 Likes
thank you for the fast reply i will try it rn
sorry its not working maybe if i remove the part where the inserted var is equal to false again but that way i can’t add any other number inside the array
Check this example, it works fine (it only adds one element when the mouse is on the upper part of the canvas) :
var inserted = false;
var array = [];
function setup() {
createCanvas(400, 400);
noStroke();
}
function draw() {
background(220);
fill(255, 0, 0);
rect(0, 0, width, height/2);
fill(255);
rect(0, height/2, width, height/2);
text("inserted : " + inserted, 50, 50);
text("array : [" + array + "]", 50, 100);
if(mouseY < height/2){
// If you didn't already inserted a number
if(!inserted){
array.push(floor(random(10)));
inserted = true;
}
}else{ //Otherwise set inserted to false
inserted = false;
}
}
2 Likes
thank you so much worked like charm
1 Like