Skip to content

Commit

Permalink
Notion - Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shiffman committed Sep 11, 2023
1 parent 4fdb113 commit 7d06514
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions content/01_vectors.html
Original file line number Diff line number Diff line change
Expand Up @@ -646,17 +646,17 @@ <h2 id="motion-with-vectors">Motion with Vectors</h2>
this.velocity = createVector(random(-2,2), random(-2, 2));
}</pre>
<p>The functionality follows suit. The <code>Mover</code> needs to move (by applying its velocity to its position) and it needs to be visible. I’ll implement these needs as functions named <code>update()</code> and <code>show()</code>. I’ll put all of the motion logic code in <code>update()</code> and draw the object in <code>show()</code>.</p>
<pre class="codesplit" data-code-language="javascript"> update() {
//{!1} The Mover moves.
this.position.add(this.velocity);
<pre class="codesplit" data-code-language="javascript"> update() {
//{!1} The Mover moves.
this.position.add(this.velocity);
}

show() {
show() {
stroke(0);
fill(175);
//{!1} The Mover is drawn as a circle.
circle(this.position.x, this.position.y, 48);
}</pre>
}</pre>
<p>The <code>Mover</code> class also needs a function that determines what the object should do when it reaches the edge of the canvas. For now, I’ll do something simple, and have it wrap around the edges.</p>
<pre class="codesplit" data-code-language="javascript"> checkEdges() {
//{!11} When it reaches one edge, set position to the other.
Expand All @@ -671,7 +671,8 @@ <h2 id="motion-with-vectors">Motion with Vectors</h2>
} else if (this.position.y &#x3C; 0) {
this.position.y = height;
}
}</pre>
}
}</pre>
<p>Now the <code>Mover</code> class is finished, but the class itself isn’t an object; it’s a template for creating an instance of an object. To actually create a <code>Mover</code> object, I first need to declare a variable to hold it:</p>
<pre class="codesplit" data-code-language="javascript">let mover;</pre>
<p>Then, inside the <code>setup()</code> function, I create the object by invoking the class name along with the <code>new</code> keyword. This triggers the class’s constructor to make an instance of the object.</p>
Expand Down

0 comments on commit 7d06514

Please sign in to comment.