Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

P2 collision callbacks not called


Retaliator
 Share

Recommended Posts

Hey,

I'm developing a Phaser game and been stuck for way too long in this error in my code. Basically I create the player object and everything else from classes to groups and add them to collision groups. Finally I make them collide with body.collides where

the callback should happen. I've tried firing the callbacks with just the function name, this.function, function(this.meteors) but nothing seems to work so that the callback gets fired only on contact. Either it fires with every create or never.

Here's the main game.js: 

BasicGame.Game = function (game) {

    //  When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:
    var playerSpawnX = 300;
    var playerSpawnY = 300;
    this.game = game;      //  a reference to the currently running game (Phaser.Game)
    
    this.player = new BasicGame.Player(this.game,playerSpawnX,playerSpawnY);    
    this.gem = new BasicGame.Gem(this.game);
    this.meteor = new BasicGame.Meteor(this.game);
    this.hud = new BasicGame.Hud(this.game,this.player);
    

};

BasicGame.Game.prototype = {

    create: function () {    
            this.timer;
            this.timer2;
            this.total = 0;
            //this.score = 0;
            var highscore = localStorage.getItem("highscore");
          
          
            this.game.physics.startSystem(Phaser.Physics.P2JS);
            this.game.physics.p2.setImpactEvents(true);
        
        
            this.ships = this.add.group();   
            this.ships.enableBody = true;        
            this.ships.physicsBodyType = Phaser.Physics.P2JS;
           
            
            this.ground = this.game.add.tileSprite(0, 2205, 600, 220, 'layers', 'maa');
        
        
            this.ship = this.ships.create(this.player.create());
            
            this.game.physics.p2.enable(this.ship, false);
 
        
            this.game.world.setBounds(0, 0, 600, 2400);
            //this.physics.startSystem(Phaser.Physics.P2JS);
    
            //////////////////////////////GLOBAL PHYSICS//////////////////////////////////
            this.game.physics.p2.setBoundsToWorld(true, false, false, true, false);   
        
            //GLOBAL PHYSICS GRAVITY
            this.physics.p2.gravity.y = 250;     
            
            //  Create our collision groups. 
            this.shipCollisionGroup = this.game.physics.p2.createCollisionGroup();
            this.meteorsCollisionGroup = this.game.physics.p2.createCollisionGroup();
            this.gemsCollisionGroup = this.game.physics.p2.createCollisionGroup();
        
            
            this.ship.body.setCollisionGroup(this.shipCollisionGroup);
            
        
            
            //  This part is vital if you want the objects with their own collision groups to still collide with the world bounds
            //  (which we do) - what this does is adjust the bounds to use its own collision group.
            this.shipCollisionGroup.collideWorldBounds = true;
            this.meteorsCollisionGroup.collideWorldBounds = false;
            //this.gemsCollisionGroup.collideWorldBounds = false;
        
           
        
            //to make ship hit the sides of screen
            this.game.physics.p2.updateBoundsCollisionGroup();
      
           
             //gems group        
            this.gems = this.game.add.group();
            this.gems.enableBody = true; 
            this.gems.physicsBodyType = Phaser.Physics.P2JS;
         
            
            //meteors group
            this.meteors = this.game.add.group();
            this.meteors.enableBody = true; 
            this.meteors.physicsBodyType = Phaser.Physics.P2JS;
            this.meteors.gravity = 0;
    
            this.timer = this.game.time.create(false);

            //  Set a TimerEvent to occur for meteors and gems
            this.timer.loop(3000, updateScoreCounter, this);   
            this.timer.loop(1000, spawnGems, this);
            this.timer.loop(7000, spawnaaMeteoriitteja, this);                
            this.timer.loop(5000, spawnaaPaikallisiaMeteoriitteja, this);        
            
            this.timer.start();      
        
           
            function updateScoreCounter()
            {

                this.total++;
                this.player.score = this.player.score + this.total * 10;
                console.log(this.player.score);
         
            }
        
          
            
           
            function spawnaaMeteoriitteja()
            {
                for (var i = 0; i < 1; i++)
                {
                   
                    var meteor = this.meteors.create(this.meteor.create());
                    this.game.physics.p2.enable(meteor);
                    meteor.body.setCollisionGroup(this.meteorsCollisionGroup);
                    meteor.body.collides([this.meteorsCollisionGroup,this.gemsCollisionGroup, this.shipCollisionGroup]);
                    meteor.body.collideWorldBounds=false;
                 

                }              
                
                
            }
            
            function spawnaaPaikallisiaMeteoriitteja()
            {
                for (var i = 0; i < 1; i++)
                {
            
                    var static = "true"; 
                    var meteor = this.meteors.create(this.meteor.create(this.game.world.randomX, this.game.world.randomY, static));
                    this.game.physics.p2.enable(meteor);
                    meteor.body.setCollisionGroup(this.meteorsCollisionGroup);
                    meteor.body.collides([this.meteorsCollisionGroup, this.gemsCollisionGroup, this.shipCollisionGroup]);
                    meteor.body.collideWorldBounds=false;   			

                }   
            } 
        
            function spawnGems()
            {
             
                for (var i = 0; i < 2; i++)
                {
           
                    var static = "false";               
                    var gem = this.gems.create(this.gem.create(this.game.world.randomX, this.game.world.randomY, static));
                    this.game.physics.p2.enable(gem);                    
                    gem.body.setCollisionGroup(this.gemsCollisionGroup);
                    gem.body.collides([this.gemsCollisionGroup, this.shipCollisionGroup, this.meteorsCollisionGroup ]);
                    gem.body.collideWorldBounds=false;  
           
                }      
                
               
            }
            
            this.hud.create();
           
            this.ship.body.collides([this.shipCollisionGroup,this.meteorsCollisionGroup], hitMeteor, this);   
            this.ship.body.collides([this.shipCollisionGroup,this.gemsCollisionGroup], hitGem, this);  

      
         function hitMeteor(body1, body2) {
            //console.log(body1);
            console.log("hit meteor");  
        }
        
            function hitGem(body1, body2) {
            console.log("hit gem");
        }
    
       
},
    
    

    update: function () {
   
         this.player.update(); 
        
       
    },
    
    render: function() {
        
   
        this.hud.render();
        this.player.render();
    
            
    },    
  

    quitGame: function (pointer) {

    
    }

};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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