1
\$\begingroup\$

I am using crafty.js to make a platformer. I assigned the main player entity the default jumping controls and movement with .twoway() and used .checkHits() against the obstacles ("obs") the player will jump on. The problem is that when the player jumps, if the top of the player entity hits the bottom of the obstacle, the player entity "snaps" to the top. How do I make it so that this movement does not happen? The intended effect is that the player entity will only land on the obstacle if the player entity lands on the top.

Here is the code:

window.onload = function() {
  
  Crafty.init(600, 400);
  Crafty.background('#07A6FF');
  
  // obstacle/platform entities
  // 'obs' means obstacle 
  var obs = Crafty.e('obs, 2D, DOM, Collision, Color')
  .attr({ x: 100, y: 250, w: 400, h: 45 })
  .color('#E88D0C');
  
  var obs2 = Crafty.e('obs, 2D, DOM, Collision, Color')
  .attr({ x: 200, y: 150, w: 45, h: 45 })
  .color('#E88D0C');
  
  // player entity
  var player = Crafty.e('player, 2D, DOM, Color, Collision, Twoway, Gravity, Jumper')
  .attr({x: 50, y: 50, w: 60, h: 40 })
  .color('#0035AB')
  .twoway(200, 150)
  .checkHits('obs')
  .gravityConst(200)
  .gravity('obs');
}
<script src="https://github.com/craftyjs/Crafty/releases/download/0.7.1/crafty.js"></script>

\$\endgroup\$
0
\$\begingroup\$

I found a solution on the crafty.js forums. This post explains this code (posted by mucaho):

.bind("CheckLanding", function(ground) {
  if (this._y + this._h > ground._y + ground._h) // disallow landing of player, if player's feet are not above ground
      this.canLand = false;
})
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.