Problems removing clips

I want to make sure clones don’t stack up when I revisit the same frame, but when I try to remove any previous instances I get this error

image

I have a controller holder clip called energyStores_mc with a series of scripted energy store simulations (vibration_mc, thermal_mc, etc) - one clip in each frame.

Using a menu you can advance the play head to whichever store you want to see the simulation for.

The simulations have moving particles, which are produced using clone.

This is part of the code in the vibration_mc, which clones a series of vibrating particles.

onEvent('load', function () {
    
    if (project.vibrationInitialised === 0){
        makeParticles();
        project.vibrationInitialised++;
    } else {
        removeParticles();
        makeParticles();
    }

    locateParticles();
});

function removeParticles(){
    for (var i = 1; i <= numberOfParticles; ++i){
        particles[i].remove();
    }    
}

function makeParticles(){

    particles[1] = randVibration_mc;
    
    for (var i = 2; i <= numberOfParticles; ++i){
        particles[i] = randVibration_mc.clone();
    }
}

It seems to be happy enough to produce endless clones every time I play the clip, but can’t find them when I try to remove them.

Any thoughts gratefully received.

I think particles is a local variable exclusive to makeParticles() so other functions can’t find them.

It’s hard to tell what the problem is from what you’ve shown. Have you tried console.log(particles) before calling removeParticles (you can see the console if you right click and inspect the page, let me know if you need help finding this).

It’s possible that the clones are automatically removed when you move to a different frame, I don’t remember if this is the case.

You might want to do the removing right before you leave the frame rather than when you return to the frame?

Thanks. Really helpful.