Jump to content

Collision detection in group


zelcard
 Share

Recommended Posts

create: function()
{
 platforms = this.game.add.physicsGroup();

 for (var i = 0; i < 5; i++)
 {
  platform = platforms.create(this.game.world.centerX, this.game.world.centerY + 300, 'platform')
  platform.anchor.setTo(0.5, 1);
  this.game.physics.enable(platform, Phaser.Physics.ARCADE);
  platform.body.immovable = true;
  platform.body.checkCollision.up    = true;
  platform.body.checkCollision.down  = false;
  platform.body.stopVelocityOnCollide = false;
 }

 player = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY + 260, 'player');
 player.anchor.setTo(0.5, 1);
 this.game.physics.enable(player, Phaser.Physics.ARCADE);
 player.body.gravity.y = 150;
 player.body.collideWorldBounds=true;
 player.body.checkCollision.up    = false;
 player.body.checkCollision.left  = false;
 player.body.checkCollision.right = false;
 player.body.checkCollision.down  = true;
},

update: function()
{
 platforms.forEach(this.checkPosition, this);   
 this.game.physics.arcade.collide(player, platforms, this.addScore, null, this);
},

checkPosition: function(platform)
{
  if(platform.body.touching.up == true){platform.body.velocity.y = 100;}  
},

I'm trying to call body.touching.up for every platform that is in-game but it doesn't seem to work. Am I doing something wrong or is this even possible to do using platforms.forEach function? Please note that this is just a part of the code but the basic idea is that I launch player sprite and when it lands on platform, the platform starts to move. I understand that I can do this by checking if player is touching the platform, but I want platforms that are not colliding with player do something as well. Thanks.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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