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);
// Normalize the direction and apply a simple gravity effect
if (distance > 0) {
dx /= distance;
dy /= distance;
}
// Apply gravity towards the mouse
self.vx += dx * 0.5;
self.vy += dy * 0.5;
//Spped modifier
var speedModifier = .95 + distance / 1000;
// Update particle position
self.x += self.vx * speedModifier;
self.y += self.vy * speedModifier;
if (distance > 20) {
self.vx *= .999;
self.vy *= .999;
}
// 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 < 3000; 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;
});
}; ===================================================================
--- original.js
+++ change.js
@@ -57,9 +57,9 @@
****/
// Initialize particles array
var particles = [];
// Create 1000 particles and add them to the game
-for (var i = 0; i < 1000; i++) {
+for (var i = 0; i < 3000; 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);