Embarissed Posted February 9, 2017 Share Posted February 9, 2017 Hello, been stuck on this for a few days now and figure I'd try asking. In my platformer, my character can double jump. The problem is when I flip this.body.gravity, the character can double jump forever. if (me.input.isKeyPressed("jump")) { this.renderable.setCurrentAnimation("jump"); if (this.multipleJump <= 2 && this.body.gravity > 0) { // easy "math" for double jump this.body.vel.y -= (this.body.maxVel.y * this.multipleJump++) * me.timer.tick; this.body.jumping = true; me.audio.play("jump", false); }else if(this.multipleJump <= 2 && this.body.gravity < 0) { this.body.vel.y = (this.body.maxVel.y * this.multipleJump++) * me.timer.tick; this.body.jumping = true; me.audio.play("jump", false); } } if (this.body.jumping || this.body.falling){ this.renderable.setCurrentAnimation("jump"); } else if (!this.body.falling && !this.body.jumping) { // reset the multipleJump flag if on the ground this.multipleJump = 1; } else if (this.body.falling && this.multipleJump < 2) { // reset the multipleJump flag if falling this.multipleJump = 2; } And here is the code for the gravity flip, case me.collision.types.ACTION_OBJECT: if(other.type === "flip") { this.body.gravity = -this.body.gravity; this.renderable.flipY(this.body.gravity < 0); } break; I know the problem is with the second else if. While in midair the flag is being reset and it's probably because falling upwards doesn't count as falling. I have tried using body.vel.y <= 0 instead of falling and that did not work. I've checked the Body documentation and didn't see anything that could help me. Here again is the link to the github if you need to see more, https://github.com/embarissed/godawggaming/blob/master/js/entities/entities.js Thanks again! Quote Link to comment Share on other sites More sharing options...
ldd Posted February 10, 2017 Share Posted February 10, 2017 I think you made a 'typo' you probably meant this.body.vel.y += (this.body.maxVel.y * this.multipleJump++) * me.timer.tick; instead of this.body.vel.y = (this.body.maxVel.y * this.multipleJump++) * me.timer.tick; Moreover, rewriting your code yields, for the conditional statement after me.input.isKeyPressed, var isMoving = this.body.jumping || this.body.falling; if (isMoving){ this.renderable.setCurrentAnimation('jump'); } else if (!isMoving){ // reset the multipleJump flag if on the ground this.multipleJump = 1; } else if (this.body.falling && this.multipleJump < 2) { // reset the multipleJump flag if falling this.multipleJump = 2; } so obviously the last else if is never called. You probably should avoid using complicated if/else statements until you actually need them (and even then, be careful ) More info: https://en.wikipedia.org/wiki/De_Morgan's_laws Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 10, 2017 Author Share Posted February 10, 2017 You would think it would be " +=" but it's not. To get a full jump, no matter where you are falling it has to just be " = " or else it just does a sort of short hop. Not sure what you mean about the last "else if". It does get called when I run off a platform then do my double jump. Edit: Cancel that second statement, after further testing, it is definitely not working as intended, just a short hop. Edit 2: Changed statements to If, works as intended. Thanks Idd. Reverse Double jump still infinite. Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 11, 2017 Share Posted February 11, 2017 We do double-jumping in the Platformer example with a flag (more accurately a counter) to gate the number of jumps until the player touches ground: https://github.com/melonjs/melonJS/blob/bca659885eb2782d3730193843b94da2b58d6e88/examples/platformer/js/entities/player.js#L77-L93 Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 11, 2017 Author Share Posted February 11, 2017 4 hours ago, Parasyte said: We do double-jumping in the Platformer example with a flag (more accurately a counter) to gate the number of jumps until the player touches ground: https://github.com/melonjs/melonJS/blob/bca659885eb2782d3730193843b94da2b58d6e88/examples/platformer/js/entities/player.js#L77-L93 Well yeah. That's where I got that code from originally. That flag however else if (!this.body.falling && !this.body.jumping) { // reset the multipleJump flag if on the ground this.multipleJump = 1; is repeatedly triggered while gravity is negative during mid-jump (checked with breakpoints). My double jump works perfectly while the gravity is > 0. The only thing I can think of is that while gravity is flipped the engine believes falling upward does not count as this.body.falling. Just wondering if there is a logical way to get around this or some property that I don't know of that could help in this situation. Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 11, 2017 Share Posted February 11, 2017 Gotcha. That's because this.body.falling is computed by melonJS for real-world gravity. Since you're reversing gravity, you'll have to do your own flag corrections. (This is technically a bug.) this.body.update(dt); // Add the following two lines after the body update this.body.falling = (this.body.vel.y * Math.sign(this.body.gravity)) > 0; this.body.jumping = (this.body.falling ? false : this.body.jumping); You'll make the corrections after the body update, which also needs to be after your double-jump implementation. Also if you need to run your game on IE, you'll need a polyfill for Math.sign(). You can find one here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign#Polyfill There might be some additional requirements that still need to be satisfied, but this will be the basis of a good solution! Embarissed 1 Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 13, 2017 Share Posted February 13, 2017 This bug is being tracked here: https://github.com/melonjs/melonJS/issues/871 obiot and Embarissed 2 Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 13, 2017 Author Share Posted February 13, 2017 It is still not working, there was a very small change I noticed however. With gravity reversed, if I quickly tap jump twice and hit the now ceiling (originally the ground), it will disable jump til I land. It's kind of hard to explain, but while in mid air, ill jump, then hit jump 2 more times before hitting the ceiling, then fall to the ground, assumingly hitting the 3 this.multiplejump. Something I noticed also was that while holding left or right, the animation for walking was used instead of my jumping. But only for about for a half second after using a multi jump, then it resumed with the jumping animation. Again, normal gravity still works perfectly, even with the added lines. Feel free to try it out, http://godawg.xyz/game/index.html At the start, jump up and to the left to be taken to an area with the flip. Quote Link to comment Share on other sites More sharing options...
obiot Posted February 13, 2017 Share Posted February 13, 2017 FYI, there is a WWW remake in melonJS : http://alexdantas.net/games/www/ source code of the main player here : https://github.com/alexdantas/www/blob/master/js/entities/player.js Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 13, 2017 Author Share Posted February 13, 2017 9 hours ago, obiot said: FYI, there is a WWW remake in melonJS : http://alexdantas.net/games/www/ source code of the main player here : https://github.com/alexdantas/www/blob/master/js/entities/player.js Haha, if I didn't have a double jump I would be in the clear. The code I used is very similar to the what he did for his game, I just have it so that touching an item causes the flip instead. The collision.y < 0 looked very promising but anytime I try to use updateMovement() in any way, I get the error that updateMovement() is not a function. Quote Link to comment Share on other sites More sharing options...
Embarissed Posted February 14, 2017 Author Share Posted February 14, 2017 Finally got it! Had to make a slight change to your code @Parasyte. this.body.update(dt); this.body.falling = (this.body.vel.y) != 0; this.body.jumping = (this.body.falling ? false : this.body.jumping); Thanks for the help! Quote Link to comment Share on other sites More sharing options...
obiot Posted February 15, 2017 Share Posted February 15, 2017 but in this case, it does not take in account the current gravity, which means that falling can be true if either you are falling (vel.y > 0), or jumping (vel.y <0) ? Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 15, 2017 Share Posted February 15, 2017 Yeah ... It should respect the direction of gravity. The change you made will prevent the jumping flag from ever being true. Anyway, I'm glad you found something that seems to work for you. To fix the ceiling/floor resets, there is a second set of things to edit in the collision handler. This one is buried in the melonJS source code, though. It's mentioned in the ticket. And the animation weirdness is caused by the conditions you chose for swapping to your jump animation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.