gkhnavarro
gkhnavarro

Reputation: 466

Phaser.js Game objects collide as intended, but collision callback isn't

I am making a simple game where a ball object hits with a bullet object. The objects collide properly, but the callback function(addscore()), included in the same collides() function for the objects, only gets called once(most probably during the objects' first creation).

Here is the code snippet for the create() function. The collision parts are at the bottom:

create: function() { 

        this.cursors = this.game.input.keyboard.createCursorKeys();

        //background
        this.cloud = game.add.sprite(50, 100, 'cloud');
        this.cloud.scale.setTo(.4,.4);
        this.cloud1 = game.add.sprite(250, 200, 'cloud');
        this.cloud1.scale.setTo(.5,.5);
        this.grass = game.add.sprite(0, 470, 'grass');
        game.stage.backgroundColor = '#71c5cf';

        //Ball and cannon
        game.physics.startSystem(Phaser.Physics.P2JS);
        game.physics.p2.restitution = .9;
        this.ball = game.add.sprite(200, 245, 'ball');
        this.cannon = game.add.sprite(200, 490, 'cannon');
        this.ball.anchor.setTo(0.4, 0.4);
        this.cannon.anchor.setTo(0.5, 0.5);
        this.cannon.scale.setTo(.4,.4);
        this.ball.scale.setTo(.4,.4);
        game.physics.p2.enable(this.ball);
        this.ball.body.setCircle(29);
        this.game.debug.body(this.ball)
        //gravity and bounce, collision
        this.game.physics.p2.gravity.y = 1500; 

        this.ballCollisionGroup = this.game.physics.p2.createCollisionGroup();
        this.bulletCollisionGroup = this.game.physics.p2.createCollisionGroup();
        this.game.physics.p2.updateBoundsCollisionGroup();
        this.ball.body.setCollisionGroup(this.ballCollisionGroup);
        this.ball.body.collides([this.bulletCollisionGroup], this.addscore);

        this.bullet = game.add.group();

        this.bullet.createMultiple(20, 'bullet');

        this.bullet.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', this.resetbullet);
        this.bullet.callAll('anchor.setTo', 'anchor', 0.1, 0.1);
        this.bullet.callAll('scale.setTo', 'scale', .1, .1);
        this.bullet.setAll('checkWorldBounds', true);
        this.bullet.enableBody = true;
        this.bullet.physicsBodyType = Phaser.Physics.P2JS;
        game.physics.p2.enable(this.bullet);


        this.bullet.forEach(function(child){
        child.body.setCircle(7);
        child.body.setCollisionGroup(this.bulletCollisionGroup);
        child.body.collides([this.ballCollisionGroup]);
        child.body.collideWorldBounds=false;
    }, this);
    },

you can view the game here: http://gabrnavarro.github.io/Volleyball.js . view the source to see the whole code. Thanks for your help. :)

Upvotes: 1

Views: 500

Answers (2)

Fabadiculous
Fabadiculous

Reputation: 574

I have not had any experience with the P2 physics, so this may be completely wrong. When using arcade physics, the groups are initialised in the create function, but the collision is checked in the update function.

So you would have to do is move:

    this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);

To your update function (Along with any other code that checks for collision).

Again, I have not had any experience with the p2 physics system, so this may be incorrect.

Sorry if this did not help.

Upvotes: 1

Konrad 'Zegis'
Konrad 'Zegis'

Reputation: 16531

Looking at code snipped you posted here everything looks good for first glance. However when I looked at your code on gh I see that you don't pass addScore as callback but instead just call it.

Changing main.js line 67 from

this.ball.body.collides([this.bulletCollisionGroup], this.addscore(), this);

to

this.ball.body.collides([this.bulletCollisionGroup], this.addscore, this);

Should do the trick.

Upvotes: 3

Related Questions