Code edit (1 edits merged)
Please save this source code
Code edit (16 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'var angle = Matn.atan2(dy, dx);' Line Number: 89
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (19 edits merged)
Please save this source code
User prompt
On mouse down, rather than just increasing the particle speed. Do the logic to do an explosion with the mouse as the center with particles closer to the mouse being impacted more.
Code edit (1 edits merged)
Please save this source code
Code edit (17 edits merged)
Please save this source code
User prompt
In particle update, please also rotate the particles related to the direction.
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: Can't find variable: targetX' in or related to this line: 'var dx = targetX - self.x;' Line Number: 27
Code edit (1 edits merged)
Please save this source code
Initial prompt
Particle toy
/**** * Classes ****/ //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Particle class to represent each particle in the game var Particle = Container.expand(function () { var self = Container.call(this); // Create and attach a simple circle shape for the particle var particleGraphics = self.attachAsset('particle', { anchorX: 0.5, anchorY: 0.5 }); // Initialize particle properties self.vx = 0; // Velocity in x direction self.vy = 0; // Velocity in y direction // Update function to move the particle self.targetX = 0; self.targetY = 0; particleGraphics.blendMode = 1; self.update = function () { // Calculate the direction towards the mouse var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); var speed = Math.sqrt(self.vx, self.vy); // Normalize the direction and apply a simple gravity effect if (distance > 0) { dx /= distance; dy /= distance; } // Apply gravity towards the mouse self.vx += dx / 2; self.vy += dy / 2; //Spped modifier var speedModifier = 1 + distance / 300; // Update particle position self.x += self.vx * speedModifier; self.y += self.vy * speedModifier; if (speed > 2) { self.vx *= 1 / distance; self.vy *= 1 / distance; } else { self.vx += (Math.random() - .5) / 1000; self.vy += (Math.random() - .5) / 1000; } // Rotate the particle based on the direction self.rotation = Math.atan2(dy, dx); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize particles array var particles = []; // Create 1000 particles and add them to the game for (var i = 0; i < 5000; i++) { var particle = new Particle(); particle.x = Math.random() * 2048; // Random initial x position particle.y = Math.random() * 2732; // Random initial y position particles.push(particle); game.addChild(particle); } // Handle mouse move events to update particle gravity game.move = function (x, y, obj) { // Update each particle with the current mouse position particles.forEach(function (particle) { particle.targetX = x; particle.targetY = y; }); }; game.down = function (x, y, obj) { particles.forEach(function (particle) { var dx = x - particle.x; var dy = y - particle.y; var angle = Math.atan2(dy, dx); var distance = Math.sqrt(dx * dx + dy * dy); particle.vx += Math.cos(angle) * distance / 50; particle.vy += Math.sin(angle) * distance / 50; }); };
===================================================================
--- original.js
+++ change.js
@@ -37,10 +37,10 @@
// Update particle position
self.x += self.vx * speedModifier;
self.y += self.vy * speedModifier;
if (speed > 2) {
- self.vx *= .99;
- self.vy *= .99;
+ self.vx *= 1 / distance;
+ self.vy *= 1 / distance;
} else {
self.vx += (Math.random() - .5) / 1000;
self.vy += (Math.random() - .5) / 1000;
}
@@ -81,8 +81,9 @@
particles.forEach(function (particle) {
var dx = x - particle.x;
var dy = y - particle.y;
var angle = Math.atan2(dy, dx);
- particle.vx += Math.cos(angle) * 10;
- particle.vy += Math.sin(angle) * 10;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ particle.vx += Math.cos(angle) * distance / 50;
+ particle.vy += Math.sin(angle) * distance / 50;
});
};
\ No newline at end of file