Jump to content

Rotate Game Objects Around a Moving Object?


marcaaron
 Share

Recommended Posts

I am attempting to create a series of protective bombs that orbit the player object. So far I have a group of bombs set up and have them orbiting around the player, but would like them to evenly distribute around the player.

// within create function

  this.bombs = this.physics.add.group({
    key:'bomb',
    frameQuantity: 10
  });

// within update function

  Phaser.Actions.RotateAroundDistance(this.bombs.getChildren(), {x:this.player.x, y:this.player.y}, 0.02, 150);

This works. When the player moves the bomb follows in orbit. But all the bombs are stacked on top of each other. What's the correct way to have them evenly distribute in a circle around the player object? I tried creating a new Phaser.Geom.Circle and then Phaser.Actions.PlaceOnCircle but it seems the bombs cannot be placed on the circle and also rotated?

 

I am trying to create something similar to this...

http://labs.phaser.io/edit.html?src=src/actions/rotate around xy.js

but have the group items not eventually collapse into the same location but remain evenly distributed.

Link to comment
Share on other sites

Ah! Ok! I completely missed those optional arguments. Thanks! I also managed to get this working by setting the velocity x and y of each bomb object to match the player velocity everytime the player moves. But seems like not the best solution since it requires mapping over all the bombs each time the player moves in any direction.

 

 

Link to comment
Share on other sites

Here's the solution I came up with using your suggestion :)

 

// Create

  this.bombs = this.physics.add.group({
    key:'bomb',
    frameQuantity: 10
  });

  this.circle = new Phaser.Geom.Circle(this.player.x, this.player.y, 150);

  this.startAngle = this.tweens.addCounter({
    from: 0,
    to: 6.28,
    duration: 6000,
    repeat: -1
  })

  this.endAngle = this.tweens.addCounter({
    from: 6.28,
    to: 12.56,
    duration: 6000,
    repeat: -1
  })

// Update

Phaser.Actions.SetXY([this.circle], this.player.x, this.player.y);

Phaser.Actions.PlaceOnCircle(
  this.bombs.getChildren(), 
  this.circle, 
  this.startAngle.getValue(), 
  this.endAngle.getValue()
);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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