Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:17:04+00:00 2026-05-29T09:17:04+00:00

In CraftyJS, how do i stop my player entity from clipping into other entities?

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:17:05+00:00Added an answer on May 29, 2026 at 9:17 am

    It looks like you are using

    this.x += this._speed;
    

    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:

    .bind('Moved', function(from) {
        if(this.hit('mushroomRed')){
            this.attr({x: from.x, y:from.y});
        }
    }).
    

    Edit: complete example

    // Init Crafty:
    Crafty.init();
    Crafty.canvas.init();
    
    var player = Crafty.e("2D, Canvas, Color, player, Multiway, Collision")
       .attr({x: 0, y: 0, w: 50, h: 50})
       .color("rgb(0,255,0)")
       .multiway(3, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180})
       .bind('Moved', function(from) {
           if(this.hit('mushroomRed')){
               this.attr({x: from.x, y:from.y});
            }
        });
    
    var mushroom = Crafty.e("2D, Canvas, mushroomRed, Color")
        .attr({x: 200, y: 150, z:1, w: 32, h: 32})
        .color("red");
    

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to CraftyJS and was wondering if anybody has had this problem. I
I am implementing sound effects in HTML5 audio but after a while, it just
I'm creating a web-based online game and am trying to find the best fit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.