Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to using ES6 classes! #8

Closed
shiffman opened this issue Oct 2, 2017 · 1 comment
Closed

Change to using ES6 classes! #8

shiffman opened this issue Oct 2, 2017 · 1 comment

Comments

@shiffman
Copy link
Owner

shiffman commented Oct 2, 2017

Here's a basic model of how I'm doing this simply:

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 8-5: Rewrite the gravity example from Chapter 5 using objects with a Ball class. 
// Include two instances of a Ball object.

// Ball constuctor
class Ball {
  constructor(x, y, w) {
    this.x = x; // x location of square 
    this.y = y; // y location of square 
    this.w = w; // speed of square 
    this.speed = 0; // size
  }

  display() {
    // Display the square 
    fill(175);
    stroke(0);
    ellipse(this.x, this.y, this.w, this.w);
  }

  update() {
    // Add speed to location
    this.y = this.y + this.speed;

    // Add gravity to speed
    this.speed = this.speed + gravity;

    // If square reaches the bottom 
    // Reverse speed 
    if (this.y > height) {
      this.speed = this.speed * -0.95;
    }
  }
}
@MikeCroall
Copy link
Contributor

MikeCroall commented Oct 6, 2017

I can do this. Will pull request when ready.

@shiffman is it all exercises that use a class-like function set up that you want changed over?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants