Lakshya dubey
Lakshya dubey

Reputation: 311

How do I make a Sprite revolve around a point in Phaser 3?

In my Phaser 3 game I have an object which I want to revolve around a point (x,y) in a circle of a specific radius say 5 units. All the tutorials are in Phaser 2, so please help me out.

Upvotes: 0

Views: 3164

Answers (2)

Fabadiculous
Fabadiculous

Reputation: 574

You can use Phaser.Actions.RotateAroundDistance. There is an example here

In case the link breaks in the future, the parameters are

RotateAroundDistance(point, x, y, angle, distance):

And an exmple use on a group of objects is below:

Phaser.Actions.RotateAroundDistance(group.getChildren(), { x: 400, y: 300 }, 0.02, 200)

Upvotes: 3

BdR
BdR

Reputation: 3058

My guess would be to set the anchor of the sprite outside the actual sprite. The anchor is the pivot point or "handle" on the sprite, effectively an offset for the x,y position and angle etc. The further away you set the anchor, the bigger the radius will be.

And then add a tween to rotate the angle. So something like:

// setAnchor, x offset = 10 to the right of the sprite, y offset = center of sprite
mysprite.setAnchor(10.0, 0.5);

var tween = game.tweens.add({
  targets: mysprite,
  angle: 360.0,
  duration: 1500,
  repeat: 0
});

Upvotes: 1

Related Questions