User prompt
make an launch trail object
User prompt
make a launch trail object that will spawn at the bottom of the screen and move to the location of the mouse click in 3 seconds
User prompt
spawn the fireworks 3 seconds after the mouse click
User prompt
keep one 3 fireworkobjects at the location of the mouseclick with minimal movement
User prompt
make the outer fireworks move faster
User prompt
make the firework a different color each time
User prompt
make it longer until the fireworks dissappear
Initial prompt
fireworks simulator
/****
* Classes
****/
// Firework particle class
var FireworkParticle = Container.expand(function () {
var self = Container.call(this);
var particleGraphics = self.createAsset('fireworkParticle', 'Firework particle', 0.5, 0.5);
self.speedX = 0;
self.speedY = 0;
self.alpha = 1;
self.scaleX = 1;
self.scaleY = 1;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
self.alpha *= 0.95; // fade out
self.scaleX *= 0.99;
self.scaleY *= 0.99;
if (self.alpha < 0.05) {
self.destroy();
}
};
});
// Firework class
var Firework = Container.expand(function () {
var self = Container.call(this);
self.particles = [];
self.explode = function (x, y) {
for (var i = 0; i < 50; i++) {
var particle = new FireworkParticle();
var angle = Math.random() * Math.PI * 2;
var speed = Math.random() * 5 + 2;
particle.speedX = Math.cos(angle) * speed;
particle.speedY = Math.sin(angle) * speed;
particle.x = x;
particle.y = y;
self.particles.push(particle);
game.addChild(particle);
}
};
self.update = function () {
for (var i = self.particles.length - 1; i >= 0; i--) {
self.particles[i].update();
if (self.particles[i].alpha < 0.05) {
self.particles.splice(i, 1);
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize fireworks array
var fireworks = [];
// Event listener for mouse or touch down
game.on('down', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
var firework = new Firework();
firework.explode(pos.x, pos.y);
fireworks.push(firework);
});
// Update function for the game
LK.on('tick', function () {
for (var i = fireworks.length - 1; i >= 0; i--) {
fireworks[i].update();
if (fireworks[i].particles.length === 0) {
fireworks.splice(i, 1);
}
}
});