Jump to content

Phaser using overlap() with two sprites


kcurtin
 Share

Recommended Posts

Hi All - I have two Sprites, a player and a shape. When the player is overlapping the shape, I want it to accerate the player sprite object (like a conveyor belt).. I'm having some issues getting this to work with the following code:

 

var Player = function(game, x, y, frame) {
  Phaser.Sprite.call(this, game, x, y, 'playerMovements', 0);
  this.game.physics.arcade.enableBody(this);
  this.body.collideWorldBounds = true
  this.anchor.setTo(0.5, 0.5);
};
 
var Belt = function(game, player, x, y) {
  var beltBMD = game.add.bitmapData(50, 400);
  beltBMD.ctx.fillStyle = 'red';
  beltBMD.ctx.fillRect(0, 0, 50, 400);
  Phaser.Sprite.call(this, game, x, y, beltBMD);
  this.game.physics.arcade.enable(this);
 
  // Code that isn't working as intended
  this.game.physics.arcade.overlap(this, player, this.applyEffect, null, this);
 
  this.game.time.events.add(2000, this.destroy, this);
  this.game.add.existing(this);
  player.bringToTop()
};
 
Belt.prototype.applyEffect = function(drop, player) {
  console.log(drop.body.overlapX); // 0
  console.log(drop.body.overlapY); // 0
  console.log(player.body.overlapX); // 0
  console.log(player.body.overlapY); // 0
  player.body.velocity.y += 400;
};
 
The issue is that my applyEffect callback gets triggered, but there isn't an overlap property set on either of the two objects so I can't accelerate until the overlap properties are back to 0 because they never seem to be set in the first place. If the callback gets triggered, shouldn't an overlap be set? Open to other suggestions about how to do this, I went with the bitmap data approach because I need to be able to draw shapes w/ physics bodies which the Graphics class doesn't support.
 
Thanks!
 
 
Link to comment
Share on other sites

Overlap needs to be called in a repeating function, usually update or a function called from update, as it is a discreet check. Take it out of your belt constructor and place it in your main game's update loop, and have it check player against all belts (add the belts to a group or an array to do this) which will be much faster than having every belt check its own collision against the player. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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