Keep track of score and reset game after score
Client: only set direction on change
This commit is contained in:
@ -86,10 +86,6 @@ function movePaddle(direction) {
|
||||
});
|
||||
}
|
||||
|
||||
function moveUp() { return movePaddle(-1); }
|
||||
function stopPaddle() { return movePaddle(0); }
|
||||
function moveDown() { return movePaddle(1); }
|
||||
|
||||
// Create the application helper and add its render target to the page
|
||||
let app = new PIXI.Application({ width: 1000, height: 500 });
|
||||
getElement('canvas-container').appendChild(app.view);
|
||||
@ -124,18 +120,25 @@ connection.on("ReceiveGameState", function (state) {
|
||||
const keyEvent = (function () {
|
||||
var upPressed = false;
|
||||
var downPressed = false;
|
||||
var direction = 0;
|
||||
|
||||
function setDirection(dir) {
|
||||
if (direction != dir) {
|
||||
direction = dir;
|
||||
movePaddle(direction);
|
||||
}
|
||||
}
|
||||
|
||||
function moveUpdated() {
|
||||
if (upPressed == downPressed) stopPaddle();
|
||||
else if (upPressed) moveUp();
|
||||
else if (downPressed) moveDown();
|
||||
if (upPressed == downPressed) setDirection(0);
|
||||
else if (upPressed) setDirection(-1);
|
||||
else if (downPressed) setDirection(1);
|
||||
else console.error("unknown move!");
|
||||
}
|
||||
|
||||
function handler(event) {
|
||||
if (event.repeat) return;
|
||||
if (event.path.indexOf(document.body) != 0) return; // only use key if it was pressed on empty space
|
||||
var pressed = event.type == "keyup";
|
||||
var pressed = event.type == "keydown";
|
||||
|
||||
// W Key is 87, Up arrow is 87
|
||||
// S Key is 83, Down arrow is 40
|
||||
switch (event.keyCode) {
|
||||
@ -150,6 +153,9 @@ const keyEvent = (function () {
|
||||
default: return;
|
||||
}
|
||||
|
||||
if (event.repeat) return;
|
||||
if (event.path.indexOf(document.body) != 0) return; // only use key if it was pressed on empty space
|
||||
|
||||
event.preventDefault();
|
||||
moveUpdated();
|
||||
}
|
||||
|
Reference in New Issue
Block a user