Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated rotate around a sprite format to ES6 #357

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions public/src/game objects/sprites/rotate around a sprite.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scene: {
preload: preload,
create: create,
update: update
class Example extends Phaser.Scene
{
constructor ()
{
super();
this.ball1 = undefined;
this.ball2 = undefined;
this.ball3 = undefined;
this.angle1 = 0;
this.distance1 = 200;
this.angle2 = 0;
this.distance2 = 80;
}
preload ()
{
this.load.image('ballRed', 'assets/demoscene/ball.png');
this.load.image('ballBlue', 'assets/demoscene/blue_ball.png');
this.load.image('ballSmall', 'assets/demoscene/ball-tlb.png');
}
};

var ball1;
var ball2;
var ball3;

var angle1 = 0;
var distance1 = 200;

var angle2 = 0;
var distance2 = 80;
create ()
{
this.ball1 = this.add.sprite(400, 300, 'ballRed');
this.ball2 = this.add.sprite(400, 300, 'ballBlue');
this.ball3 = this.add.sprite(400, 300, 'ballSmall');
}

var game = new Phaser.Game(config);
update()
{
this.ball2.setPosition(400, 300);
this.ball3.setPosition(400, 300);

function preload ()
{
this.load.image('ballRed', 'assets/demoscene/ball.png');
this.load.image('ballBlue', 'assets/demoscene/blue_ball.png');
this.load.image('ballSmall', 'assets/demoscene/ball-tlb.png');
}
Phaser.Math.RotateAroundDistance(this.ball2, this.ball1.x, this.ball1.y, this.angle1, this.distance1);
Phaser.Math.RotateAroundDistance(this.ball3, this.ball2.x, this.ball2.y, this.angle2, this.distance2);

function create ()
{
ball1 = this.add.sprite(400, 300, 'ballRed');
ball2 = this.add.sprite(400, 300, 'ballBlue');
ball3 = this.add.sprite(400, 300, 'ballSmall');
this.angle1 = Phaser.Math.Angle.Wrap(this.angle1 + 0.02);
this.angle2 = Phaser.Math.Angle.Wrap(this.angle2 + 0.03);
}
}

function update ()
{
// Reset the position so the rotation gets the correct _new_ position
ball2.setPosition(400, 300);
ball3.setPosition(400, 300);

Phaser.Math.RotateAroundDistance(ball2, ball1.x, ball1.y, angle1, distance1);
Phaser.Math.RotateAroundDistance(ball3, ball2.x, ball2.y, angle2, distance2);
const config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scene: [ Example]
};

angle1 = Phaser.Math.Angle.Wrap(angle1 + 0.02);
angle2 = Phaser.Math.Angle.Wrap(angle2 + 0.03);
}
const game = new Phaser.Game(config);