Jump to content

[Arcade Physics] method Collide not working


dcooke616
 Share

Recommended Posts

I'm relatively new to Phaser, but up until now I have found everything to work as expected. 

I'm trying to develop a Worms clone (just for fun) and i'm basing most of my code on the following tutorial :http://phaser.io/tutorials/coding-tips-001

The problem is, I can't get my worms (group) to collide with the land (bitmapdata converted into a sprite)

Here is my create()

create: function() {

        	
//create background first
        	
this.background = this.game.add.sprite(0,0,'background');

            
//start the physics system
			
this.physics.startSystem(Phaser.Physics.ARCADE);
        	
            
            
//create land
            
this.createLand();

            
//create worms
          	
this.createWorms();



          
         	
}

and createLand()

createLand: function(){
            
//create the land bitmap data
            
this.landBmp = this.add.bitmapData(1500,1000);
            
this.landBmp.draw('land1');
            
this.landBmp.update();
            
            
            
//convert to sprite landBmp -> land
            
this.land = this.game.add.sprite(0,0,this.landBmp);

           
//enable physics on land sprite
           
this.physics.arcade.enable(this.land);
            
            
this.land.body.allowGravity = true;
            
this.land.body.immovable = true;
          

        
}

createWorms()

createWorms: function(){


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

            this.physics.enable(this.worms,Phaser.Physics.ARCADE);
        	for (var i = 0; i < 10; i++)
        	{
                //calculate team and select name
        		var team = i % 2;
        		var name = names[Math.floor((Math.random() * names.length -1))];
        		
                //create a worm
        		var worm = this.worms.create(land1SpawnPoints[i].x,land1SpawnPoints[i].y,'sprites');
                
                worm.team = team;
                worm.name = name;
                worm.health = 100;

                //enable physics for the worm
                this.physics.arcade.enable(worm);
                worm.body.gravity.y = 200;
                worm.body.bounce = 0.3;
                worm.body.velocity.x = 1;

                //animations
                worm.animations.add('idle' ,
                    Phaser.Animation.generateFrameNames('worm_idle',1,1),5,true);
                worm.animations.add('move' ,
                    Phaser.Animation.generateFrameNames('worm_walk',1,3),5,true);


                //add the worm to worms group
                this.worms.add(worm);
  
        	}
        },

and in my update function i'm calling


this.physics.arcade.collide(this.worms,this.land);
            
        
//NOTE: this function works , and by works, I mean - 
//
//The function this.test will be called when a worm overlaps with the land sprite

//this.physics.arcade.overlap(this.worms, this.land, this.test, null, this);

When I run my code with physics.arcade.collide it doesn't trigger any callback function and the worms just fall through the ground.

However if I use the overlap function , a callback function is triggered.

If anyone could point me in the right direction as to what is going wrong I would be very grateful.

 

Edit: Even if you could tell me how I can  programmatically stop my worms from falling through the ground 

For example, the following function would be called when a worm overlaps with the ground.

 test : function(worm, land) {
            
            //code goes here to prevent worm falling further
        },

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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