File tree Expand file tree Collapse file tree 1 file changed +5
-22
lines changed
Expand file tree Collapse file tree 1 file changed +5
-22
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,6 @@ let playerHeight = 50;
1212// Player physics
1313let playerVelY = 0 ;
1414let playerVelX = 0 ;
15- let onGround = true ;
1615let gravity = 0.4 ;
1716let groundHeight = 400 ;
1817let 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 ++ ) {
You can’t perform that action at this time.
0 commit comments