Skip to content

Commit c7a9ce5

Browse files
committed
fix wrong jump height in case platform
1 parent 8579456 commit c7a9ce5

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

assets/cases/collider/Platform/HeroControl.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ cc.Class({
88
gravity: -1000,
99
drag: 1000,
1010
direction: 0,
11-
jumpSpeed: 300
11+
jumpSpeed: 300,
12+
_lastSpeedY: 0
1213
},
1314

1415
// use this for initialization
@@ -19,6 +20,7 @@ cc.Class({
1920

2021
this.collisionX = 0;
2122
this.collisionY = 0;
23+
this.jumping = true;
2224

2325
this.prePosition = cc.v2();
2426
this.preStep = cc.v2();
@@ -51,7 +53,8 @@ cc.Class({
5153
case cc.macro.KEY.up:
5254
if (!this.jumping) {
5355
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;
5558
}
5659
break;
5760
}
@@ -119,6 +122,7 @@ cc.Class({
119122
}
120123

121124
this.speed.y = 0;
125+
this._lastSpeedY = 0;
122126
other.touchingY = true;
123127
}
124128

@@ -161,9 +165,9 @@ cc.Class({
161165
this.jumping = true;
162166
}
163167
},
164-
168+
165169
update: function (dt) {
166-
if (this.collisionY === 0) {
170+
if (this.jumping) {
167171
this.speed.y += this.gravity * dt;
168172
if (Math.abs(this.speed.y) > this.maxSpeed.y) {
169173
this.speed.y = this.speed.y > 0 ? this.maxSpeed.y : -this.maxSpeed.y;
@@ -198,6 +202,13 @@ cc.Class({
198202
this.preStep.y = this.speed.y * dt;
199203

200204
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;
202213
},
203214
});

0 commit comments

Comments
 (0)