Clearing cloned objects from the canvas

I am working in Adobe Animate. I have been able to load an asset using the following code:

F.loadAssets("cook.png", "asset/");

I can also clone the image with the following code:

asset("cook.png").clone().sca(.15).pos(350,30,CENTER,TOP).addTo(page1).drag();

and this is what I get ..... which is just what I wanted.

But now I need to be able to clear the canvas so I can repeat the process with other images. Can you help me with the code that I can use to clear (delete) the images?

Thank you

Rod

Any object can be removed from its container with object.removeFrom(). If you never want to use it again then use object.dispose();

You often store objects in variables or properties or an array so you can access them and, for example, remove them later. You can also use container.getChildAt(index) to access any child of a container, including the stage, if you have not stored their name in a variable. Any container, including the stage, also has removeAllChildren() and disposeAllChildren() methods.

So what I often do, is add objects that have something in common to a Container. Then if you no longer want them, you can use container.disposeAllChildren();

const monsters = new Container(W,H).addTo();
new Circle().loc(rand(W), rand(H), monsters);
new Circle().loc(rand(W), rand(H), monsters);

timeout(2, ()=>{monsters.disposeAllChildren();});

Note - addTo(), center(), centerReg(), loc()_and pos() all have container parameters.

This is a real breakthrough for me - understanding. More about the role of containers and how to work with them. Really helpful. Thank you.

1 Like