Jump to content

unable to enable p2

Abdullah Al Rifat

Recommended Posts

in this game i need physics for multiple objects for a group its fine to enable p2 bt whenever i tried to enable p2 for player with this line

  game.physics.p2.enable(player, false);

my screen goes black 

why is this happening ???



    var game = new Phaser.Game(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio, Phaser.AUTO, '', { preload: preload, create: create, update: update});
    var player;
    var world;
    var blink;
    var rocketangel;
    var rocketdirection;
    var rocketSpeedDelta;
    var rocketradius;
    var platforms;
    var scoreText;
    var playerangle;
    var scaleRatio;
    var score;
    var asteroid;
    scaleRatio = window.devicePixelRatio / 2;

    //load images or files before game starts
    function preload()
        game.load.image('background', 'assets/game/background.png');
        game.load.image('world', 'assets/game/world.png');
        game.load.image('player', 'assets/game/player.png');
        game.load.image('blink', 'assets/game/blink.png');
        game.load.image('asteroid', 'assets/game/asteroid.png');


    //create all the images ans things on the screen
    function create()
        rocketangel = 0;
        rocketdirection = 1;
        rocketSpeedDelta = 0.002;
        rocketradius = 170;

        //  A simple background for our game
        game.add.sprite(0, 0, 'background');

        //  We're going to be using physics, so enable the Arcade Physics system

//  Turn on impact events for the world, without this we get no collision callbacks

        game.physics.p2.restitution = 0.8;

        //  Create our collision groups. One for the player, one for the pandas
        var playerCollisionGroup = game.physics.p2.createCollisionGroup();
        var asteroidCollisionGroup = game.physics.p2.createCollisionGroup();

        world=game.add.sprite(game.world.centerX-10, game.world.centerY-20, 'world');
        world.anchor.setTo(0.5, 0.5);
        world.scale.setTo(scaleRatio, scaleRatio);

        platforms = game.add.group();
        platforms .enableBody = true;
        platforms.physicsBodyType = Phaser.Physics.P2JS;
        for (var i = 0; i < 10; i++) {
             asteroid = platforms.create(game.world.height , game.world.width , 'asteroid');
            asteroid.anchor.setTo(0.5, 0.5);
            asteroid.scale.setTo(scaleRatio, scaleRatio);
            game.physics.p2.enable(asteroid, false);
            asteroid.body.setRectangle(40, 40);

            //  Tell the panda to use the pandaCollisionGroup

            //  Pandas will collide against themselves and the player
            //  If you don't set this they'll not collide with anything.
            //  The first parameter is either an array or a single collision group.
            asteroid.body.collides([asteroidCollisionGroup, playerCollisionGroup]);

        //adding player
        player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');
        player.anchor.setTo(0.5, 0.5);
        player.scale.setTo(scaleRatio, scaleRatio);
        //game.physics.p2.enable(player, false);

        //  Set the ships collision group


        //  The ship will collide with the pandas, and when it strikes one the hitPanda callback will fire, causing it to alpha out a bit
        //  When pandas collide with each other, nothing happens to them.

        //player.body.collides( asteroidCollisionGroup, hitPanda, this);

        //  The platforms group contains the ground and the 2 ledges we can jump on

        //asteroid.body.collideWorldBounds = true;
        //  The score
        scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#FFF' });
        scoreText.scale.setTo(scaleRatio, scaleRatio);
        cursors = game.input.keyboard.createCursorKeys();

    function hitPanda(body1, body2) {

        //  body1 is the space ship (as it's the body that owns the callback)
        //  body2 is the body it impacted with, in this case our panda
        //  As body2 is a Phaser.Physics.P2.Body object, you access its own (the sprite) via the sprite property:
        body2.sprite.alpha -= 0.1;

    function update()

        var playerangelchange;

        platforms.forEach(function(asteroid) {
            game.physics.arcade.collide(player,asteroid,deathHandler, null, this);
        }, this);

        if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
            //it will speed up the rocket

        else if (game.input.keyboard.isDown(Phaser.Keyboard.ENTER))
            //it will cause the blink
            blink= game.add.sprite(player.x, player.y, 'blink');
            blink.anchor.setTo(0.5, 0.5);
            blink.scale.setTo(scaleRatio, scaleRatio);
            game.add.tween(blink).to( { alpha: 0 }, 20, Phaser.Easing.Linear.None, true);
        else if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
            rocketdirection=-1; //change the direction
        else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
            rocketdirection=1; //change the direction

        //speed of current rocket movement
        if (rocketdirection == 1)
            rocketSpeedDelta = 1.14;
        else if (rocketdirection == -1)
            rocketSpeedDelta = -1.14;

        //for moving on a circular path
        scoreText.text = 'Score: ' + score;
        rocketangel += this.time.physicsElapsed * rocketSpeedDelta ;
        player.x = game.world.centerX+ Math.cos(rocketangel) * rocketradius ;
        player.y = game.world.centerY+Math.sin(rocketangel) * rocketradius ;

    function moveBullets (asteroid) {
        setTimeout(accelerateToObject(asteroid,world,30),50000);  //start accelerateToObject on every bullet
    function accelerateToObject(obj1, obj2, speed) {
        if (typeof speed === 'undefined') { speed = 60; }
        var angle = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
        //obj1.body.rotation = angle + game.math.degToRad(90);  // correct angle of angry bullets (depends on the sprite used)
        obj1.body.force.x = Math.cos(angle) * speed;    // accelerateToObject
        obj1.body.force.y = Math.sin(angle) * speed;
   function deathHandler() {


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...