I am trying to display an animation when a collision occurs between sprites. I have asteroids that I shoot and when I shoot them the asteroids disappear when the bullet collides or overlaps the asteroid. I would like to play an animation after the asteroid has been collided with. how can I go about this ?
see code bellow
function createAsteroids() { // code to spawn asteroids at random locations
for (let i = 0; i < numAsteroids; i++) {
asteroid = createSprite(random(0, 1000), random(-50, -350), 40, 40);
asteroid.addImage(asteroidSprite)
asteroidSprite.resize(65, 65);
asteroid.maxSpeed = random(1, 3);
rock.add(asteroid);
asteroidArray.push(asteroid);
}
}
function drawAsteroid() { // draws asteroids moving down and checking collision
rock.overlap(turret, explode)
for (let i = 0; i < asteroidArray.length; i++) {
if (asteroidArray[i].position.y > height) {
asteroidArray[i].position.y = 0;
asteroidArray[i].position.x = random(0, 800);
}
asteroidArray[i].addSpeed(3, 90); // code to move asteroids towards the turrets using attracttion point
if (i % 3 == 0) {
asteroidArray[i].attractionPoint(1, 100, 740);
}
if (i % 3 == 1) {
asteroidArray[i].attractionPoint(1, 500, 740);
}
if (i % 3 == 2) {
asteroidArray[i].attractionPoint(1, 1200, 740);
}
}
}
function createBullets() { // code to create bullet
for (let i = 0; i < numBullets; i++) {
bullets = createSprite(500, 740, 25, 25);
bullets.addImage(bulletSprite)
bullets.maxSpeed = 6;
pew.add(bullets)
bulletArray.push(bullets)
}
}
function drawBullets() { // code to fire bullets
pew.overlap(rock, explode)
explodeSound.play()
bulletArray[bullCount].attractionPoint(4, mouseX, mouseY);
bullCount++;
}
function explode(sprite, obstical) { //code for explosion and collision and removal of sprites on collision
sprite.remove()
obstical.remove()
explodeSound.play()
}
I believe I would need to implement something in the drawBullets() function, or the explode function. but not sure how to actually get the animation to play when the bullet collides with the asteroid.
explosion = loadAnimation('assets/0.png', 'assets/1.png', 'assets/2.png', 'assets/3.png', 'assets/5.png', 'assets/6.png',
'assets/7.png', 'assets/8.png', 'assets/9.png', 'assets/10.png', 'assets/11.png', 'assets/12.png', 'assets/13.png',
'assets/14.png', 'assets/15.png', 'assets/16.png', 'assets/17.png', 'assets/18.png', 'assets/19.png', 'assets/20.png',
'assets/21.png', 'assets/22.png', 'assets/23.png', 'assets/24.png', 'assets/25.png', 'assets/26.png', 'assets/27.png',
'assets/28.png', 'assets/29.png', 'assets/30.png', 'assets/31.png', 'assets/32.png', 'assets/33.png', 'assets/34.png',
'assets/35.png', 'assets/36.png', 'assets/37.png', 'assets/38.png', 'assets/39.png', 'assets/40.png', 'assets/41.png',
'assets/42.png', 'assets/43.png', 'assets/44.png', 'assets/45.png', 'assets/46.png', 'assets/47.png');