In CraftyJS, how do i stop my player entity from clipping into other entities?
This is my object:
Crafty.c("Mushroom", {
init: function() {
this.addComponent("collision");
this.collision(new Crafty.polygon([[8,8],[24,8],[24,24],[8,24]]));
}
});
var mushroom = Crafty.e("2D, canvas, mushroomRed, Mushroom")
.attr({x: 200, y: 150, z:1, w: 32, h: 32});
and this is my player onHit:
.onhit("mushroomRed", function() {
this.x += this._speed;
this.stop();
}
It works only when i approach it from certain angle, otherwise, it goes haywire.
Advice?
It looks like you are using
to move the player away from the mushroom after they have collided. But as you are only moving it in the x direction it wont work if you collide from top or bottom. Is that your problem?
If you use the Multiway or Fourway components you could do this instead:
Edit: complete example
Running at http://jsfiddle.net/PzKVh/
This is using the latest version of Crafty 0.4.5. There has been a few breaking changes and a lot of improvements, so i would suggest you use this version.
Also, feel free to ask in the forums at https://groups.google.com/forum/#!forum/craftyjs I think you are much more likely to find help there 🙂