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 sprite roatation format to ES6 #359

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
155 changes: 80 additions & 75 deletions public/src/game objects/sprites/sprite rotation.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,99 @@
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-example',
scene: {
preload: preload,
create: create,
update: update
class Example extends Phaser.Scene
{
constructor ()
{
super();
this.text = undefined;
this.arrow = undefined;
}
};

var arrow;
preload ()
{
this.load.image('arrow', 'assets/sprites/longarrow-white.png');
}

var game = new Phaser.Game(config);
create ()
{
// The code isn't too important for this example
// What we're showing here is the angles, in degrees and radians,
// that a Phaser sprite uses when rotating.
//
// You can rotate a sprite by setting either property.
//
// `angle` is in degrees, from -180 to 180.
// `rotation` is in radians, from -PI to PI
//
// For example:
//
// sprite.angle = 45
//
// is the same as:
//
// sprite.rotation = 0.785

function preload ()
{
this.load.image('arrow', 'assets/sprites/longarrow-white.png');
}
let labelStyle = { font: "16px courier", fill: "#00ff00", align: "center" };

function create ()
{
// The code isn't too important for this example
// What we're showing here is the angles, in degrees and radians,
// that a Phaser sprite uses when rotating.
//
// You can rotate a sprite by setting either property.
//
// `angle` is in degrees, from -180 to 180.
// `rotation` is in radians, from -PI to PI
//
// For example:
//
// sprite.angle = 45
//
// is the same as:
//
// sprite.rotation = 0.785

var labelStyle = { font: "16px courier", fill: "#00ff00", align: "center" };

// Create a large circle, then draw the angles on it

var circle = new Phaser.Geom.Circle(400, 300, 225);
var labelCircle = new Phaser.Geom.Circle(400, 300, 265);

var graphics = this.add.graphics();

graphics.lineStyle(2, 0x00bb00, 1);

graphics.strokeCircleShape(circle);

graphics.beginPath();

for (var a = 0; a < 360; a += 22.5)
{
graphics.moveTo(400, 300);
// Create a large circle, then draw the angles on it

let circle = new Phaser.Geom.Circle(400, 300, 225);
let labelCircle = new Phaser.Geom.Circle(400, 300, 265);

var p = Phaser.Geom.Circle.CircumferencePoint(circle, Phaser.Math.DegToRad(a));
let graphics = this.add.graphics();

graphics.lineTo(p.x, p.y);
graphics.lineStyle(2, 0x00bb00, 1);

var lp = Phaser.Geom.Circle.CircumferencePoint(labelCircle, Phaser.Math.DegToRad(a));
graphics.strokeCircleShape(circle);

var na = a;
graphics.beginPath();

if (a > 180)
for (let a = 0; a < 360; a += 22.5)
{
na -= 360;
graphics.moveTo(400, 300);

let p = Phaser.Geom.Circle.CircumferencePoint(circle, Phaser.Math.DegToRad(a));

graphics.lineTo(p.x, p.y);

let lp = Phaser.Geom.Circle.CircumferencePoint(labelCircle, Phaser.Math.DegToRad(a));

let na = a;

if (a > 180)
{
na -= 360;
}

let rads = String(Phaser.Math.DegToRad(na)).substr(0, 5);
let info = na + "°\n" + rads;
let label = this.add.text(lp.x, lp.y, info, labelStyle).setOrigin(0.5);
}

var rads = String(Phaser.Math.DegToRad(na)).substr(0, 5);
var info = na + "°\n" + rads;
var label = this.add.text(lp.x, lp.y, info, labelStyle).setOrigin(0.5);
graphics.strokePath();

this.arrow = this.add.sprite(400, 300, 'arrow').setOrigin(0, 0.5);

this.text = this.add.text(10, 10, '', { font: '16px Courier', fill: '#ffffff' });
}

graphics.strokePath();

arrow = this.add.sprite(400, 300, 'arrow').setOrigin(0, 0.5);
update ()
{
this.arrow.angle += 0.2;

this.text.setText([
'Sprite Rotation',
'Angle: ' + this.arrow.angle.toFixed(2),
'Rotation: ' + this.arrow.rotation.toFixed(2)
]);
}

text = this.add.text(10, 10, '', { font: '16px Courier', fill: '#ffffff' });
}

function update ()
{
arrow.angle += 0.2;
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-example',
scene: [ Example ]
};

text.setText([
'Sprite Rotation',
'Angle: ' + arrow.angle.toFixed(2),
'Rotation: ' + arrow.rotation.toFixed(2)
]);
}
const game = new Phaser.Game(config);