Code edit (1 edits merged)
Please save this source code
Code edit (20 edits merged)
Please save this source code
User prompt
Set the initial target X and target Y of each particle to the center of the screen.
User prompt
Make the initial particle X and particle Y the center of the screen.
User prompt
to target X and target Y of all particles, the center of the screen.
Code edit (1 edits merged)
Please save this source code
/**** * 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 var speedModifier = 1 + distance / 300; self.vx += dx; self.vy += dy; //Spped modifier // Update particle position self.x += self.vx; self.y += self.vy; var speed = Math.sqrt(self.vx * self.vx + self.vy * self.vy); if (distance > 2) { self.vx *= .995; self.vy *= .995; } else { //self.vx += (Math.random() - .5) / 1000; //self.vy += (Math.random() - .5) / 1000; } // Rotate the particle based on the direction self.rotation = Math.atan2(self.vy, self.vx); self.scaleY = Math.max(speed / 100, 0); }; }); /**** * 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 / (25 + Math.random()) + .3); particle.vy += Math.sin(angle) * (distance / (25 + Math.random()) + .3); }); };
/****
* 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
var speedModifier = 1 + distance / 300;
self.vx += dx;
self.vy += dy;
//Spped modifier
// Update particle position
self.x += self.vx;
self.y += self.vy;
var speed = Math.sqrt(self.vx * self.vx + self.vy * self.vy);
if (distance > 2) {
self.vx *= .995;
self.vy *= .995;
} else {
//self.vx += (Math.random() - .5) / 1000;
//self.vy += (Math.random() - .5) / 1000;
}
// Rotate the particle based on the direction
self.rotation = Math.atan2(self.vy, self.vx);
self.scaleY = Math.max(speed / 100, 0);
};
});
/****
* 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 / (25 + Math.random()) + .3);
particle.vy += Math.sin(angle) * (distance / (25 + Math.random()) + .3);
});
};