Giter Club home page Giter Club logo

breakout's People

Contributors

nyapal avatar

Watchers

 avatar  avatar

breakout's Issues

Your work is looking pretty good so far

This looks pretty good so far.

I see you're having trouble with the keydown and mouse handlers. Try switching to ES6 arrow functions. Like this:

class Game {
   constructor() {
    this.canvas = document.getElementById('myCanvas');
    this.ctx = this.canvas.getContext('2d');
    this.ball = new Ball(this.canvas.width / 2, this.canvas.height - 30);
    this.paddle = new Paddle(this.canvas.width / 2, this.canvas.height - 10);
    this.bricks = new Bricks();
    this.lives = new Life(this.canvas.width - 75);
    this.score = new Score();

    // let rightPressed = false;
    // let leftPressed = false;

    console.log(this.keyDownHandler)

    document.addEventListener('keydown', (e) => {  // Use Arraow function here
      console.log(this)
      this.keyDownHandler(e) 
    }, false);

    document.addEventListener('keyup', (e) => { // Use Arraow function here
      this.keyUpHandler(e) 
    }, false);

    document.addEventListener('mousemove', (e) => { // Use Arraow function here
      this.mouseMoveHandler(e) 
    }, false);
  }
...

With this change, the key down and mouse move handlers are working. The paddle is still not moving. You seem to have lost this couple lines of code that move the paddle.

 if(rightPressed && paddleX < canvas.width-paddleWidth) {
    paddleX += 7;
  }
  else if(leftPressed && paddleX > 0) {
    paddleX -= 7;
  }

This from the original tutorial. For what you have this would go into draw() function of the Game class and you'd have a this in a few places since Game owns the rightPressed and leftPressed properties. The paddle owns it's x and game owns the paddle so this would be something like: this.paddle.x

I notice that you have some of this code in the handlers() method at the very bottom. Read it's closely it's missing this.rightPressed and this.leftPressed. You need to call this in draw() or won't be used by the game.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.