Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] - onBodyEnter Method #836

Open
ghost opened this issue Nov 17, 2016 · 3 comments
Open

[Suggestion] - onBodyEnter Method #836

ghost opened this issue Nov 17, 2016 · 3 comments
Projects

Comments

@ghost
Copy link

@ghost ghost commented Nov 17, 2016

I see we have a onCollision method here.

It would be nice I think to also have a onBodyEnter too. I know this isn't a huge suggestion and might not be needed, but helps a lot with creating doors and whatnot!

@parasyte
Copy link
Member

@parasyte parasyte commented Nov 17, 2016

FWIW, the shapes example uses the low-level collision checks to determine whether the pointer is intersecting with body shapes:

if (shape.containsPoint(x, y)) {

It's a little more work than a specialized event, but it's something you can easily use today.

@ghost
Copy link
Author

@ghost ghost commented Nov 17, 2016

Hm, Thanks so far -- I was thinking for the collision / physics system too. Something like this:

game.Player = me.Entity.extend({
  init : function (x, y, data) {
    //var image = me.loader.getImage("player");
        //console.log(data);
       this.data = data;
       this.onBodyEnter = false;
       this._super(me.Entity, "init", [x, y, {
            image: "player",
            width: 72,
            height: 144
        }]);
  },
  update : function (dt) {
    this.body.update(dt);
    me.collision.check(this);
    return this._super(me.Entity, 'update', [dt]);
    },
    onBodyEnter: function(response, other){
        console.log("Body Entered");
    },
    onCollision : function (response, other) {
        //Handle Map Entity Collision
        if(response.b.name == "WarpEntity"){
            this.onBodyEnter(response, other);
                // We need to get this to only run one time on body that entered

        }

      // Make the object solid
      return true;
    }


});
@ghost
Copy link
Author

@ghost ghost commented Nov 17, 2016

Wow. This is awesome. Okay, so I found that me.collision.check(this) returns if the body is colliding or not.

So, take a look at this:

game.Player = me.Entity.extend({
  init : function (x, y, data) {
       this.data = data;
       this.onBodyEntered = false;
       this._super(me.Entity, "init", [x, y, {
            image: "player",
            width: 72,
            height: 144
        }]);
        this.velx = 450;
        this.vely = 450;
        this.alwaysUpdate = true;
        this.chooseShipImage();
        this.body.setVelocity(0, 0);
        //Collision Shape
        this.body.shapes[0] = new me.Ellipse(this.width/2, this.height/2, this.width/1.3, this.height/1.3);

  },
  update : function (dt) {
    this.body.update(dt);
    if(!me.collision.check(this)){ // They are not colliding
        this.onBodyEntered = false
    }
    return this._super(me.Entity, 'update', [dt]);
    },
    onBodyEnter: function(response, other){
        console.log("Body Entered");
    },
    onCollision : function (response, other) {
        if(response.b.name == "WarpEntity" && this.onBodyEntered == false){
            this.onBodyEnter(response, other);
            this.onBodyEntered = true;
        }
    }


});

It's working fine right now, but do you think I will run into any problems? It basically sets onBodyEntered to false if it's not colliding.... I think I got it? And the onBodyEnter method gets only called once (which is normal)

@obiot obiot added this to To Do in Roadmap Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Roadmap
  
New Features (Core)
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.