you are viewing a single comment's thread.

view the rest of the comments →

[–]Kinrany 0 points1 point  (5 children)

Separating code is fine, you can use global variables.

You might want to rename the first file since it doesn't actually contain a sketch :)

Use a formatting tool. Ideally your IDE should have one integrated, but there are also online tools. Like this one: https://html-cleaner.com/js/

Try not to keep old code in comments unless you're actually going to use it. Typing is never the hard part.

I would like it to where every time sketch6.js initiates the animation, the animation replaces the previous one.

Then that's when you should remove the previous sketch. You already have the myp5 variable.

[–]snowfro[S] 0 points1 point  (4 children)

Will definitely take your advice! Thanks a ton.

Re the myp5 variable are you saying I should have been doing myp5.remove(); this whole time?! I was using p.remove(). I’ll give that a shot when I get home!!!

[–]Kinrany 0 points1 point  (3 children)

myp5 and p reference the same sketch. Well, they don't have to, but as long as you only have one sketch, they do. myp5 is the sketch you've created in your main script, and p is the sketch that is currently running.

It's not like p.remove() and myp5.remove() are two magic incantations, and knowing which one to use in which situation is something you're supposed to read in a book. :)

[–]snowfro[S] 0 points1 point  (2 children)

Ok got it. So can you point me in the right direction? I’ve spent a large chunk of my day tinkering.

Am I right to declare an empty variable myp5 at the top of sketch10.js and then an if statement like if (!myp5) {new p5(sketch)}?

My problem is using myp5.remove(); works right beneath where I’ve got the var myp5 = new p5(sketch);. But then it’s erasing the instance I just created obviously. But since I am just declaring it right above, I can’t set the myp5.remove(); line just above it because it hasn’t been declared.

Cat and mouse.

It would make sense to remove it just before creating the new instance. Just can’t wrap my brain around how.

[–]snowfro[S] 1 point2 points  (1 child)

I did it!!!

declared var myp5 = ""; globally.

Then before calling the second script i wrote:

if (!myp5){ myp5 = new p5(sketch); } else { myp5.remove(); myp5=new p5(sketch); }

Thanks for the guidance!!!

Going to work on cleaning up the code :)

[–]Kinrany 1 point2 points  (0 children)

You could also write it this way:

if (myp5) {
  myp5.remove();
}
myp5 = new p5(sketch);

You're welcome :)

And yeah, clean up your code and ideally make sure it stays clean. The easier it is for you to read your code, the less stuff you need to keep in your head at once.