nyapal / breakout Goto Github PK
View Code? Open in Web Editor NEWFEW 1.1 Tutorial
FEW 1.1 Tutorial
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.