@@ -8,7 +8,8 @@ cc.Class({
8
8
gravity : - 1000 ,
9
9
drag : 1000 ,
10
10
direction : 0 ,
11
- jumpSpeed : 300
11
+ jumpSpeed : 300 ,
12
+ _lastSpeedY : 0
12
13
} ,
13
14
14
15
// use this for initialization
@@ -19,6 +20,7 @@ cc.Class({
19
20
20
21
this . collisionX = 0 ;
21
22
this . collisionY = 0 ;
23
+ this . jumping = true ;
22
24
23
25
this . prePosition = cc . v2 ( ) ;
24
26
this . preStep = cc . v2 ( ) ;
@@ -51,7 +53,8 @@ cc.Class({
51
53
case cc . macro . KEY . up :
52
54
if ( ! this . jumping ) {
53
55
this . jumping = true ;
54
- this . speed . y = this . jumpSpeed ;
56
+ this . speed . y = this . jumpSpeed > this . maxSpeed . y ? this . maxSpeed . y : this . jumpSpeed ;
57
+ this . _lastSpeedY = this . jumpSpeed > this . maxSpeed . y ? this . maxSpeed . y : this . jumpSpeed ;
55
58
}
56
59
break ;
57
60
}
@@ -119,6 +122,7 @@ cc.Class({
119
122
}
120
123
121
124
this . speed . y = 0 ;
125
+ this . _lastSpeedY = 0 ;
122
126
other . touchingY = true ;
123
127
}
124
128
@@ -161,9 +165,9 @@ cc.Class({
161
165
this . jumping = true ;
162
166
}
163
167
} ,
164
-
168
+
165
169
update : function ( dt ) {
166
- if ( this . collisionY === 0 ) {
170
+ if ( this . jumping ) {
167
171
this . speed . y += this . gravity * dt ;
168
172
if ( Math . abs ( this . speed . y ) > this . maxSpeed . y ) {
169
173
this . speed . y = this . speed . y > 0 ? this . maxSpeed . y : - this . maxSpeed . y ;
@@ -198,6 +202,13 @@ cc.Class({
198
202
this . preStep . y = this . speed . y * dt ;
199
203
200
204
this . node . x += this . speed . x * dt ;
201
- this . node . y += this . speed . y * dt ;
205
+ if ( this . _lastSpeedY === 0 || this . speed . y === 0 || this . _lastSpeedY / Math . abs ( this . _lastSpeedY ) === this . speed . y / Math . abs ( this . speed . y ) ) {
206
+ this . node . y += ( this . _lastSpeedY + this . speed . y ) * dt / 2 ;
207
+ }
208
+ else {
209
+ this . node . y += - this . _lastSpeedY / this . gravity / 2 * this . _lastSpeedY + this . speed . y / this . gravity / 2 * this . speed . y ;
210
+ }
211
+
212
+ this . _lastSpeedY = this . speed . y ;
202
213
} ,
203
214
} ) ;
0 commit comments