mirror of
https://github.com/nature-of-code/noc-book-2
synced 2024-11-17 07:49:05 +01:00
Notion - Update docs
This commit is contained in:
parent
4fdb113125
commit
7d06514903
1 changed files with 7 additions and 6 deletions
|
@ -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. 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.
|
||||
|
@ -671,7 +671,8 @@ function draw() {
|
|||
} else if (this.position.y < 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>
|
||||
|
|
Loading…
Reference in a new issue