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

Physics Group createCallback not working as expected, removes hit detection #4657

Closed
kendistiller opened this issue Jul 17, 2019 · 5 comments

Comments

@kendistiller
Copy link

kendistiller commented Jul 17, 2019

Version

  • Phaser Version: 3.18.1
  • Operating system: Mac OS

Description

I'm having an issue related to #4420, but with createCallback on Physics group. createCallback is never called within a Physics Group config. If you manually assign the createCallback property after the group has been created, then the physics body becomes invalidated and collision does not work. You have to manually enableBody on the created child to get it to work again.

Example Test Code

CreateCallback is never called within a Physics Group config:

const bullets = this.physics.add.group({
    createCallback: () => console.log('create'); // DOES NOT GET CALLED
});

The callback will get called if adding it subsequently as a property:

bullets.createCallback = () => console.log('create');` // GETS CALLED

However, once you add the above callback property, collision detection stops working completely:

this.physics.add.overlap(this.player, bullets, () => console.log('overlap')); // DOES NOT GET CALLED

Additional Information

This hack (re-enabling child body in callback) works, but feels like working against what it's supposed to do. We're trying to say "all objects in this group have a physics body" so having to enable physics again isn't great. Perhaps I'm doing something wrong from the get-go:

bullets.createCallback = (child) => { if (!child.body) { this.physics.world.enableBody(child); } };
@photonstorm
Copy link
Collaborator

The Physics Group uses this callback itself for its own internal requirements, so when you replace it (as per the second example) you override what it needs to do, hence no physics bodies are created. It should be marked as private, but JSDoc is a bit crappy when it comes to overriding docs inherited from parent classes.

@kendistiller
Copy link
Author

@photonstorm That makes a lot of sense, and like I mentioned it felt very hacky. Is there any way we can get a hook into this for the physics groups, while still allowing it to do its own work internally?

@photonstorm
Copy link
Collaborator

Override it, like you're doing, but make sure to call Group.createCallbackHandler(child) at the start of your own handler so it's still given all the right properties by the Group, then you can do whatever you need extra after that.

@kendistiller
Copy link
Author

Yep, that will work thanks. Is there any reason not to implement this directly into the physics group itself? (calling createCallbackHandler + custom hook)

photonstorm added a commit that referenced this issue Jan 14, 2020
…lRemoveCallback` to handle its body creation and destruction, allowing you to use your own `createCallback` and `removeCallback` as defined in the Group config. Fix #4420 #4657 #4822
@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants