xxxxxxxxxx
// Credits
// Coin Sound Effect: https://freesound.org/people/cabled_mess/sounds/350873/
let player, coin;
let coinSound;
let coins;
let score = 0;
function preload() {
coinSound = loadSound("coin.wav");
}
function setup() {
//no gravity necessary :D
new Canvas(400, 400);
player = new Sprite(0, height/2);
coin = new Sprite(width, height/2);
coin.color = "yellow";
coin.d = 20;
coin.stroke = "none";
coins = new Group();
coins.color = 'yellow';
coins.diameter = 10;
// Create all the coins that will go in the group
for (let i = 0; i < 50; i++) {
let c = new coins.Sprite();
c.x = random(width);
c.y = random(height);
}
}
function draw() {
clear();
background("rgb(127,127,255)")
player.moveTowards(mouse, 0.1);
if (player.overlaps(coin)) {
coin.remove();
coinSound.play();
}
// To collide with a group, the syntax is
// similar
// if (player.overlaps(coins)) {
// coinSound.play();
// // This code does play the sound, but does not remove the coin!
// To accomplish that, we need to use a "callback" function, which
// means that we give the name of the function we want to call when
// the collision happens as the second argument to the overlaps function
// }
// Here's what we do instead:
player.overlaps(coins, collectCoin);
player.text = score;
}
function collectCoin(p, c) {
coinSound.play();
c.remove();
score++;
}