xxxxxxxxxx
let smily;
let shooterX, shooterY;
let projectiles;
let wowCounter = 0;
let gunSound, ouchSound;
let hits = 0
function preload() {
smily = createSprite(100,100,100,100);
let smilyAnimation = smily.addAnimation("smily", "smily0.png", "smily3.png");
smilyAnimation.frameDelay = 10;
let smilyWowAnimation = smily.addAnimation("smilyWow", "smilyWow0.png");
gunSound = loadSound('gun.wav');
ouchSound = loadSound('ouch.wav');
}
function setup() {
createCanvas(windowWidth, windowHeight);
shooterX = windowWidth/2;
shooterY = windowHeight - 30;
projectiles = new Group(); // Creates a group that can hold many projectiles
}
// Make the smily move back and forth from 100,100 to 900,100
function moveSmily()
{
if (dist(smily.position.x,smily.position.y,100,100)<20) {
smily.attractionPoint(2.0,900,100);
}
if (dist(smily.position.x,smily.position.y,900,100)<20) {
smily.attractionPoint(2.0,100,100);
}
}
function shootSprites()
{
// The projectile is a 10x10 square. You could add your own sprite here instead. If we don't set the color, it is
// set to a random color.
let projectile = createSprite(shooterX, shooterY, 10, 10);
projectile.attractionPoint(13,mouseX,mouseY);
projectile.life = 90;
projectiles.push(projectile);
gunSound.play();
}
function draw() {
background(100);
// The "gun" is just a red circle near the bottom of the screen
fill(255,0,0);
ellipse(shooterX,shooterY,30,30);
// Move the smily
moveSmily();
if (keyIsDown(LEFT_ARROW)) {
shooterX = shooterX - 5;
}
else if (keyIsDown(RIGHT_ARROW)) {
shooterX = shooterX + 5;
}
// Shoot
if (mouseIsPressed) {
shootSprites();
}
// Check for collision between projectile and smily
// Loop through each projectile in projectiles
for (let i = 0; i < projectiles.length; i++) {
if (projectiles[i].overlap(smily)) {
smily.changeAnimation('smilyWow');
wowCounter = 20;
ouchSound.play();
hits++;
}
}
if (wowCounter > 0) {
wowCounter--;
if (wowCounter == 0)
smily.changeAnimation('smily');
}
drawSprites();
fill(255,255,255);
textSize(24);
text("Hits: " + hits, 10, 50);
}
p5 had problems creating the global function "Animation", possibly because your code is already using that name as a variable. You may want to rename your variable to something else.
You just changed the value of "camera", which was a p5 function. This could cause problems later if you're not careful.