Skip to content

Commit a098ad4

Browse files
committed
dgudhgug
1 parent 169c9a1 commit a098ad4

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

platformerTest.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,22 @@ window.onload = function () { //when game starts
6060
break;
6161
case " ":
6262
e.preventDefault()
63-
playerVelY = -10;
63+
if (onGround) {
64+
playerVelY = -10;
65+
onGround = false;
66+
}
6467
break;
6568
case "w":
66-
playerVelY = -10;
69+
if (onGround) {
70+
playerVelY = -10;
71+
onGround = false;
72+
}
6773
break;
6874
case "ArrowUp":
69-
playerVelY = -10;
75+
if (onGround) {
76+
playerVelY = -10;
77+
onGround = false;
78+
}
7079
break;
7180

7281
}
@@ -108,19 +117,23 @@ function update() {
108117
playerVelX = 0
109118
}
110119
// Calculating gravity
111-
playerVelY += gravity;
120+
if (!onGround) {
121+
playerVelY += gravity;
122+
}
112123

113124
// Player x movement
114125
player.x += playerVelX;
115126

116127
// Collision detection with platforms
128+
onGround = false;
117129
for (let i = 0; i < platformArray.length; i++) {
118130
let platform = platformArray[i];
119131
if (player.x + player.width > platform.x && player.x < platform.x + platform.width) {
120-
if (player.y + player.height > platform.y && player.y < platform.y + platform.height) {
132+
if (player.y + player.height > platform.y && player.y + player.height < platform.y + platform.height) {
121133
// If player is moving downwards and touches the platform
122134
if (playerVelY > 0) {
123135
player.y = platform.y - player.height;
136+
onGround = true;
124137
playerVelY = 0;
125138
}
126139
// If player is moving upwards and touches the platform
@@ -135,6 +148,7 @@ function update() {
135148
// Collision detection with ground
136149
if (player.y + player.height > groundHeight) {
137150
player.y = groundHeight - player.height;
151+
onGround = true;
138152
playerVelY = 0;
139153
}
140154

0 commit comments

Comments
 (0)