Jump to content

Button in p5.JS runs mousePressed function continuously

Smiffy_

Hey,

 

I have used a for loop to create a series of buttons from a list. I plan to use 'index' passed through to 'printMap' to determine which button was pressed. However, whenever I load the webpage it runs the mousePressed function continuosly and I do not know why.

 

Any help would be greatly appreciated and if you require any more infomration please do ask.

 

Cheers,

Ryan

 

function createButtons() {
	startCo = y+110
	mapPool.forEach( function(item, index) {
		//console.log(item, index);
		test = createButton(item);
		test.parent("main-container")
		test.style("height","35px");
		test.position(x+(cnvWidth/2), startCo);
		test.center('horizontal');
		test.mousePressed(printMap(index));
		startCo += 50;
	});
}

function printMap(index) {
	console.log("Click!");
	//console.log(mapPool[index]);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Can't tell without the rest of the code, but you probably need to pass a reference to the function

 

test.mousePressed(() => printMap(index))
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×