Notion - Update docs

This commit is contained in:
shiffman 2023-09-11 17:42:50 +00:00 committed by GitHub
parent 4fdb113125
commit 7d06514903

View file

@ -646,17 +646,17 @@ function draw() {
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. Ill implement these needs as functions named <code>update()</code> and <code>show()</code>. Ill 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, Ill 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.
@ -671,7 +671,8 @@ function draw() {
} 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 isnt an object; its 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 classs constructor to make an instance of the object.</p>