Reputation: 13
I have a program that makes a fish every time you click on the screen. I want to display how many fish have been made by counting and displaying player clicks.Can I get some code that could do this.
I'm new to JS and only know some minor things. I've tried to fiddle with onClick and mouseClicked but to no avail.
This is my fish generating code, variables not included.
var drawFish=function(centerX,centerY) {
noStroke();
fill(random(255),random(255),random(255));
ellipse(centerX, centerY, bodyLength, bodyHeight);
var tailWidth = bodyLength/4;
var tailHeight = bodyHeight/2;
fill(random(255),random(255),random(255));
triangle(centerX-bodyLength/2, centerY,
centerX-bodyLength/2-tailWidth, centerY-tailHeight,
centerX-bodyLength/2-tailWidth, centerY+tailHeight);
fill(random(255),random(255),random(255));
ellipse(centerX+bodyLength/4, centerY, bodyHeight/5, bodyHeight/5);
};
mouseClicked=function(){
drawFish(mouseX,mouseY);
};
I've tried to display the amount of clicks using
fill(0,0,0); //For visibility on background
text('mouseClicked',160,30,40,40); //My bad attempt at counting clicks
but it clearly hasn't worked.
I expected to get a number displaying how many times I've clicked but I've gotten 0 results with what I've tried.
Sorry for the long post.
Upvotes: 0
Views: 103
Reputation: 2276
var clicks = 0;
var el = document.getElementById('module');
el.onclick = function() {
clicks++;
console.log(clicks);
};
This will increment clicks
by one each time a user clicks on the page, and log the click count on every click.
Upvotes: 1