Tibix
Tibix

Reputation: 390

Particle Animation (Multiple particles dont work)

I coded some JS and achived what i wanted (First Link). Then i started making loops to be able to have multiple Particles.

But that was the point the Code broke. Still only one Element is moving and the others only get pushed away if the first Particle is near them. Its hard to explain, best would be if you test it on your own.

I tried to debug the Code for hours but i was not able to fix it. So im just gonna Upload the Code for a single particle (working) and the Code for multiple particles (broken).

Working Code (Single Element): Check it out on CodePen

Broken Code (With multiple Elements): Check it out on CodePen

.

Upvotes: 0

Views: 67

Answers (1)

artemnih
artemnih

Reputation: 4209

Your code in first pen was okay. You can create a function and pass reference to a particle. Use the same logic from 1st pen.

Also, you can save resources by saving jquery reference to a variable instead of querying multiple times: var particle = $(".part" + j);

function doWork(particle) { /* logic from 1st pen */ }

for (j = 0; j < particles; j++) {
    var particle = $(".part" + j);
    doWork(particle);
}

Here is a fork with multiple particles CodePen

Upvotes: 1

Related Questions