In c10 you kept many things in an array, and in c13 you chased apples that were small bundles { x, y }. Now imagine a paddock with twelve horses. Each horse needs its own position, its own speed, its own coat colour, and the same behaviour: trot along, turn at the fence, get drawn. Writing that out twelve times would be a nightmare.
The fix is a class: a blueprint that says what every horse has (properties) and what every horse can do (methods). Then you stamp out as many horses as you like with new.
x, y, speed, shadeupdate(), display()Five horses are trotting. Tap or click inside the paddock to add a new horse at that spot (up to 15). Each one gets its own random speed and coat.
Here is the code, slightly simplified from the sketch above. Read the class first, then the part that uses it.
Before classes, you would have used one array per property, called parallel arrays. It works, but look how much you have to keep in step:
Forget one of those four splices and horse 2's speed suddenly belongs to horse 3. With a class, one horse is one object, so herd.splice(2, 1) removes everything about it at once. Compare also the words: h.update() reads like "horse, update yourself".
new Horse(...).this: "this very horse". this.x is the x of the horse being worked on, not of any other.update() and display().Two classic class bugs. Decide what is wrong before you open each one.
update() { this.x += speed;}speed instead of this.speed. Inside a method, plain speed looks for an ordinary variable called speed, and there is none. A property always needs this. in front. Fix: this.x += this.speed;herd.push(Horse(100, 100, 2, '#fff'));new. Calling Horse(...) like a normal function does not build an object. You must write new Horse(100, 100, 2, '#fff') so that the constructor runs and hands you a fresh horse.Using the Horse class above (the fence turns only happen outside 20 to width minus 20, so ignore them here):
Answer: 54 47. Horse a moved twice by 2 (50 + 2 + 2 = 54). Horse b moved once by -3 (50 - 3 = 47). Updating one horse never touches the other, because each owns its own x. (Careful: width is the canvas width, so this snippet works once your sketch has called createCanvas.)
size to the constructor (this.size = size;) and use it in display() instead of the fixed 30. Pass a different size when you create each horse, for example random(20, 40). Foals are small, stallions are big.this.speed *= -1; to this.speed = 0;, what will every horse do at the fence? Write your guess in one sentence, then run it.this. from the this.speed in update() to recreate Case 1. Read the red message in the console, find the line number it names, then fix it.gallop() that makes the horse's speed twice as big, for example this.speed *= 2;. Call it on the first horse only, with herd[0].gallop();, when the mouse is pressed. Can you make it stop at a top speed with constrain?Firefly class with x, y, a small size, and methods update() (move a random step each frame with random(-2, 2)) and display() (a glowing yellow circle). Make 20 of them in setup() on a dark background. Everything you used for horses carries over.