Skip to content

Commit 30efc59

Browse files
committed
bug fixes
1 parent 1c295c4 commit 30efc59

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

platformerTest.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ let playerHeight = 50;
1212
// Player physics
1313
let playerVelY = 0;
1414
let playerVelX = 0;
15-
let onGround = true;
1615
let gravity = 0.4;
1716
let groundHeight = 400;
1817
let moveLeft = false;
@@ -60,22 +59,13 @@ window.onload = function () { //when game starts
6059
break;
6160
case " ":
6261
e.preventDefault()
63-
if (onGround) {
64-
playerVelY = -10;
65-
onGround = false;
66-
}
62+
playerVelY = -10;
6763
break;
6864
case "w":
69-
if (onGround) {
70-
playerVelY = -10;
71-
onGround = false;
72-
}
65+
playerVelY = -10;
7366
break;
7467
case "ArrowUp":
75-
if (onGround) {
76-
playerVelY = -10;
77-
onGround = false;
78-
}
68+
playerVelY = -10;
7969
break;
8070

8171
}
@@ -123,7 +113,6 @@ function update() {
123113
player.x += playerVelX;
124114

125115
// Collision detection with platforms
126-
onGround = false;
127116
for (let i = 0; i < platformArray.length; i++) {
128117
let platform = platformArray[i];
129118
if (player.x + player.width > platform.x && player.x < platform.x + platform.width) {
@@ -142,14 +131,8 @@ function update() {
142131
}
143132
}
144133

145-
// Collision detection with ground
146-
if (player.y + player.height > groundHeight) {
147-
player.y = groundHeight - player.height;
148-
onGround = true;
149-
playerVelY = 0;
150-
}
151-
152-
player.y += playerVelY;
134+
//move player
135+
player.y = Math.min(playerVelY + player.y, groundHeight);
153136

154137
// Drawing platforms
155138
for (let i = 0; i < platformArray.length; i++) {

0 commit comments

Comments
 (0)