Hi there!
I’m a UI developer for work, but now I’m teaching beginner coders with employee-to-employee quick classes.
What are people’s suggestions for array-like things? My heart is a bit old school, so I don’t mind regular old arrays with for() loops, but there isn’t a super easy syntax for deleting arbitrary elements. Or I could use objects (making up keys) but then iterating them is a little wonky?
I don’t see if this topic is clearly handled in Examples or Learn sections of the site, but it’s so fundamental! (In fact in my old Processing days, a similar issue came back to bite me because I was using ArrayList or something that got deprecated in later versions of Processing… and using Iterator objects was pretty clunky)
Also for the sake of my class I’m assembling a “good parts” reference page - I’ve always loved how Processing/P5 makes it so easy to try out these little baby examples all over its reference- a true strength of the platform and community - but I think now the sheer number of functions is daunting to the beginner, who might be better served with something focused on lines, rects, and ellipses responding to mouse state
Huh. So I copied the code and put it into https://editor.p5js.org/kirkjerk/sketches/y_Nj7Z31q along with some diagnostic print(); statements. What I don’t understand at all, is it seems like the ball is already removed BEFORE mousePressed()? Like the first thing I do in mousePressed() is print the array out (I made it size 3 not 10), but by the first line of mousePressed it’s already only 2 items?
It’s making be feel rather stupid! Feels like a timing issue? Like somehow the console statement is executing after the other stuff has been done…
This overall feels like a weirdly big gap in p5.js. I feel like the community should be able to say “here’s a best practice for generating, iterating, adding to and removing from a collection of items” - either as arrays or js/s hashmap-ish use of objects with keys…
Yeah, I forgot about that… so unusual that it’s like a “live” connection, for people who spent their programming lives safely using println debugging.
Anyway, I had two thoughts about paradigms to use, either with object-with-keys collections or with good old arrays: https://editor.p5js.org/kirkjerk/sketches/_kxt_7wsg has the code live, but basically it might be
I know I’m at risking of mixing up old school js with new ECMAscript hotness that will have some browser issues, but still… I really have trouble reading the pop / tail thing… like it’s sneaking in a concatanation, right? kind of like a splice?
https://editor.p5js.org/kirkjerk/sketches/pin9ZbkXe - there’s my example, roughly equivalent to the functionality in your ball program I think, but I think the filter() is easier to understand than backwards iteration with pop and tail…