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.color = 0xFFFFFF; // Default color white particleGraphics.tint = self.color; 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.98; // fade out more slowly self.scaleX *= 0.99; self.scaleY *= 0.99; if (self.alpha < 0.05) { self.destroy(); } }; }); // LaunchTrail class var LaunchTrail = Container.expand(function () { var self = Container.call(this); self.trailParticles = []; self.createTrail = function (x, y, targetY) { var trailLength = 10; for (var i = 0; i < trailLength; i++) { var trailParticle = new FireworkParticle(); trailParticle.color = 0xAAAAAA; // Trail color trailParticle.x = x; trailParticle.y = y + i * (targetY - y) / trailLength; trailParticle.speedY = (targetY - y) / trailLength; trailParticle.createAsset('fireworkParticle', 'Trail particle', 0.5, 0.5).tint = trailParticle.color; self.trailParticles.push(trailParticle); game.addChild(trailParticle); } }; self.update = function () { for (var i = self.trailParticles.length - 1; i >= 0; i--) { self.trailParticles[i].y += self.trailParticles[i].speedY; self.trailParticles[i].alpha *= 0.95; // fade out if (self.trailParticles[i].alpha < 0.05) { self.trailParticles[i].destroy(); self.trailParticles.splice(i, 1); } } }; }); // Firework class var Firework = Container.expand(function () { var self = Container.call(this); self.particles = []; self.explode = function (x, y) { var colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]; var randomColor = colors[Math.floor(Math.random() * colors.length)]; for (var i = 0; i < 50; i++) { var particle = new FireworkParticle(); var angle = Math.random() * Math.PI * 2; var speed = Math.random() * 8 + 4; particle.color = randomColor; // Assign random color to particle particle.speedX = Math.cos(angle) * speed; particle.speedY = Math.sin(angle) * speed; particle.x = x; particle.y = y; particle.createAsset('fireworkParticle', 'Firework particle', 0.5, 0.5).tint = particle.color; // Apply color tint to particle 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(); var launchTrail = new LaunchTrail(); launchTrail.createTrail(pos.x, pos.y, pos.y - 300); // Create a trail that goes 300 pixels up firework.explode(pos.x, pos.y - 300); // Start the explosion 300 pixels above the click fireworks.push({ firework: firework, trail: launchTrail }); }); // Update function for the game LK.on('tick', function () { for (var i = fireworks.length - 1; i >= 0; i--) { var fireworkObj = fireworks[i]; fireworkObj.firework.update(); fireworkObj.trail.update(); if (fireworkObj.firework.particles.length === 0 && fireworkObj.trail.trailParticles.length === 0) { fireworks.splice(i, 1); } } });
===================================================================
--- original.js
+++ change.js
@@ -25,23 +25,32 @@
});
// LaunchTrail class
var LaunchTrail = Container.expand(function () {
var self = Container.call(this);
- self.startX = 0;
- self.startY = 2732; // Start at the bottom of the screen
- self.endX = 0;
- self.endY = 0;
- self.progress = 0;
- self.duration = 3000; // 3 seconds to reach the target
- self.update = function (delta) {
- self.progress += delta;
- var t = Math.min(self.progress / self.duration, 1);
- self.x = self.startX + (self.endX - self.startX) * t;
- self.y = self.startY + (self.endY - self.startY) * t;
- if (t === 1) {
- self.destroy();
+ self.trailParticles = [];
+ self.createTrail = function (x, y, targetY) {
+ var trailLength = 10;
+ for (var i = 0; i < trailLength; i++) {
+ var trailParticle = new FireworkParticle();
+ trailParticle.color = 0xAAAAAA; // Trail color
+ trailParticle.x = x;
+ trailParticle.y = y + i * (targetY - y) / trailLength;
+ trailParticle.speedY = (targetY - y) / trailLength;
+ trailParticle.createAsset('fireworkParticle', 'Trail particle', 0.5, 0.5).tint = trailParticle.color;
+ self.trailParticles.push(trailParticle);
+ game.addChild(trailParticle);
}
};
+ self.update = function () {
+ for (var i = self.trailParticles.length - 1; i >= 0; i--) {
+ self.trailParticles[i].y += self.trailParticles[i].speedY;
+ self.trailParticles[i].alpha *= 0.95; // fade out
+ if (self.trailParticles[i].alpha < 0.05) {
+ self.trailParticles[i].destroy();
+ self.trailParticles.splice(i, 1);
+ }
+ }
+ };
});
// Firework class
var Firework = Container.expand(function () {
var self = Container.call(this);
@@ -88,30 +97,24 @@
// Event listener for mouse or touch down
game.on('down', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
- var trail = new LaunchTrail();
- trail.startX = 1024; // Center X of the screen
- trail.endX = pos.x;
- trail.endY = pos.y;
- game.addChild(trail);
- LK.setTimeout(function () {
- var firework = new Firework();
- firework.explode(pos.x, pos.y);
- fireworks.push(firework);
- }, 3000);
+ var firework = new Firework();
+ var launchTrail = new LaunchTrail();
+ launchTrail.createTrail(pos.x, pos.y, pos.y - 300); // Create a trail that goes 300 pixels up
+ firework.explode(pos.x, pos.y - 300); // Start the explosion 300 pixels above the click
+ fireworks.push({
+ firework: firework,
+ trail: launchTrail
+ });
});
// Update function for the game
LK.on('tick', function () {
- var delta = 1000 / 60; // Assuming 60 FPS for tick
- game.children.forEach(function (child) {
- if (child instanceof LaunchTrail) {
- child.update(delta);
- }
- });
for (var i = fireworks.length - 1; i >= 0; i--) {
- fireworks[i].update();
- if (fireworks[i].particles.length === 0) {
+ var fireworkObj = fireworks[i];
+ fireworkObj.firework.update();
+ fireworkObj.trail.update();
+ if (fireworkObj.firework.particles.length === 0 && fireworkObj.trail.trailParticles.length === 0) {
fireworks.splice(i, 1);
}
}
});
\ No newline at end of file