Setup velcocity after collision in p2 psychics


#1

Hello,

i’m making a geometry football game, where players and ball are circles.

When player hits the ball with pressed key, i would like to ball throw with 3 times more velocity.
To detect event collision between sprites i used to .onBeginContact.add, but when i try to change ball speed after that like this:: (sprite.body.velocity.x*=3) im getting an error: Cannot read property ‘body’ of null.

how to change speed of movement sprite in p2 psychics, during move?


#2

Hi,
Am not sure that you can play with the body property of a GameObject Geometry, can you post a bit of code please on how you declare your sprites ?


#3

sure,

addNewPlayer: function (id, x, y, color, thisPlayerReference) {
    let newplayer = game.add.graphics(405, 505);
    game.physics.p2.enable(newplayer, true);

    newplayer.lineStyle(4, 0xfd02eb, 1);
    newplayer.beginFill(0xFF0000, 1);
    newplayer.drawCircle(0, 0, 40);
    newplayer.anchor.setTo(0.5, 0.5);
    newplayer.body.collideWorldBounds = true;
    newplayer.name = socket.name;
    newplayer.id = socket.id;

    newplayer.body.setCircle(22);
    newplayer.body.onBeginContact.add(Game.contact, this);

}

contact: function (bodyA, bodyB, shapeA, shapeB, equation){
    bodyA.body.velocity.x *=3;
},

#4

Sorry my guess was wrong, it seems to affect it a body