The Wayback Machine - https://web.archive.org/web/20230323212112/https://github.com/photonstorm/phaser/issues/6406
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

onBounce event emitted from a Physics.Arcade.Body #6406

Open
ricardofiorani opened this issue Feb 27, 2023 · 2 comments
Open

onBounce event emitted from a Physics.Arcade.Body #6406

ricardofiorani opened this issue Feb 27, 2023 · 2 comments

Comments

@ricardofiorani
Copy link
Sponsor

Hello there,
I tried recently to achieve a way of getting an Arcade Physics Body to emit an event without changing Phaser's source code, however, I found myself unable to do so.

I would kindly drop a pull request adding said functionality, however, I would like to ask here first if is there a way already to do it.

Please let me know if really there is no way to do so at the moment, so I can start and draft a pull request for it.

Thanks in advance.

@photonstorm
Copy link
Owner

Hi - Thanks for the offer of a PR. First, could you explain why you'd need an onBounce event when you've already got onCollide? (as a bounce cannot happen without a collision)

@ricardofiorani
Copy link
Sponsor Author

ricardofiorani commented Feb 27, 2023

Hello @photonstorm , thank you for the quick response.

In my idea, the reason for having a universal event onBounce is that it would work with both World Bounds and other bodies. It would simplify and prevent the need of keeping track of everything that is collidable and not having to add an add.onCollide() to each possible collidable body, plus, listening for the worldbounds event.

In the following example, you can see that the collide event is not emitted when it happens to a world bound:

var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    parent: 'phaser-example',
    physics: {
        default: 'arcade',
        arcade: {
            gravity: {
                y: 200
            }
        }
    },
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('bg', 'assets/skies/space2.png');
    this.load.spritesheet('ball', 'assets/sprites/balls.png', { frameWidth: 17, frameHeight: 17 });
}

function create ()
{
    this.add.image(400, 300, 'bg');

    var group = this.physics.add.group({
        key: 'ball',
        frameQuantity: 2,
        bounceX: 1,
        scale: 9,
        bounceY: 1,
        collideWorldBounds: true,
        velocityX: 180,
        velocityY: 120,
    });

    Phaser.Actions.RandomRectangle(group.getChildren(), this.cameras.main);

    Phaser.Actions.Call(group.getChildren(), function (ball) {
        ball.body.onWorldBounds = true;
        ball.body.onCollide = true;
    });

    this.physics.world.on('worldbounds', () => console.log('worldbounds event'));
    this.physics.world.on('collide', () => console.log('collide event'));
}

EDIT:

PS: Also it would only be emitted by bodies that have a bounce value > 0.

EDIT2:

I tested it here -> https://labs.phaser.io/edit.html?src=src/physics/arcade/world%20bounds%20event.js&v=3.55.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment