1

So I've been watching the coding train to learn to code in javascript and I tried to copy his code but it doesn't work. I'm using brackets. Here is the episode:

https://www.youtube.com/watch?v=fBqaA7zRO58&t=0s&list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA&index=26

let bubbles = []

function setup() {
    createCanvas(600,400) 
    for (let i = 0; i < 3; i++) {
        let x = 10 + 30 * i
        bubbles[i] = new Bubble(x, 200, 40);
    }
}

function draw() {
    background(0);
    for(let i = 0; i < bubbles.lenght; i++) {
        bubbles[i].move;
        bubbles[i].show;
    }
}

class Bubble {
    constructor(x,y,r) {
        this.x=x;
        this.y=y;
        this.r=r;
    }
    move() {
        this.x = this.x + random(-5,5);
        this.y = this.y + random(-5,5);
    }
    show() {
        stroke(255);
        strokeWeight(4);
        noFill();
        ellipse(this.x, this.y, this.r * 2);
    }
}        
Brian Flanagan
  • 4,612
  • 5
  • 29
  • 38
  • `length` not `lenght`. Also you have to actually *call* the functions; `bubbles[i].move()` etc. – Pointy Jul 26 '18 at 21:47
  • Make sure to hit F12 to open your developer tools and have your console up while you are developing. It will often tell you about errors it encounters as a heads up to what you are doing incorrectly. – Taplar Jul 26 '18 at 21:50

1 Answers1

0

Please get into the habit of checking your developer tools and debugging your code.

For example, I would start by printing out the values of the variables you're using. Like this:

console.log(bubbles.lenght);
for(let i = 0; i < bubbles.lenght; i++) {

You'll see that this prints out undefined, which will give you a hint: you've spelled length wrong!

After you fix that problem, you have other issues. Take these lines:

bubbles[i].move;
bubbles[i].show;

This is not how you call functions. You need () parentheses after the function name:

bubbles[i].move();
bubbles[i].show();

Taking a step back, you should really not be trying to copy code like this. Instead, try to start with a simple example and then work forward in small steps. Then if you encounter an error, you'll know exactly which part of the code is the problem.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107