Skip to content

Commit 6466361

Browse files
committed
Great new thrust ship example added for ScrollZones. Also added rotationOffset value to GameObject base class.
1 parent 00fb20f commit 6466361

10 files changed

Lines changed: 129 additions & 24 deletions

File tree

Phaser/Motion.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ module Phaser {
7979
*/
8080
public velocityFromAngle(angle: number, speed: number): Point {
8181

82+
if (isNaN(speed))
83+
{
84+
speed = 0;
85+
}
86+
8287
var a: number = this._game.math.degreesToRadians(angle);
8388

8489
return new Point((Math.cos(a) * speed), (Math.sin(a) * speed));

Phaser/gameobjects/GameObject.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ module Phaser {
7676
public origin: MicroPoint;
7777
public z: number = 0;
7878

79+
// This value is added to the angle of the GameObject.
80+
// For example if you had a sprite drawn facing straight up then you could set
81+
// rotationOffset to 90 and it would correspond correctly with Phasers rotation system
82+
public rotationOffset: number = 0;
83+
7984
// Physics properties
8085
public immovable: bool;
8186

Phaser/gameobjects/Sprite.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ module Phaser {
224224
}
225225

226226
// Rotation - needs to work from origin point really, but for now from center
227-
if (this.angle !== 0 || this.flipped == true)
227+
if (this.angle !== 0 || this.rotationOffset !== 0 || this.flipped == true)
228228
{
229229
this._game.stage.context.save();
230230
this._game.stage.context.translate(this._dx + (this._dw / 2), this._dy + (this._dh / 2));
231231

232-
if (this.angle !== 0)
232+
if (this.angle !== 0 || this.rotationOffset !== 0)
233233
{
234-
this._game.stage.context.rotate(this.angle * (Math.PI / 180));
234+
this._game.stage.context.rotate((this.rotationOffset + this.angle) * (Math.PI / 180));
235235
}
236236

237237
this._dx = -(this._dw / 2);

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Phaser
33

44
Version 0.9.3
55

6-
21st April 2013
6+
23rd April 2013
77

88
By Richard Davey, [Photon Storm](http://www.photonstorm.com)
99

@@ -25,9 +25,9 @@ V0.9.3
2525
* Re-built Tilemap handling from scratch to allow for proper layered maps (as exported from Tiled / Mappy)
2626
* Tilemap no longer requires a buffer per Camera (in prep for WebGL support)
2727
* Added shiftSinTable and shiftCosTable to the GameMath class to allow for quick iteration through the data tables.
28-
* Added the new ScrollZone game object. Endlessly useful but especially for scrolling backdrops. Created several example tests.
28+
* Added the new ScrollZone game object. Endlessly useful but especially for scrolling backdrops. Created 6 example tests.
2929
* Removed the need for DynamicTextures to require a key property and updated test cases.
30-
30+
* Add the rotationOffset value to GameObject (and thus Sprite). Useful if your graphics need to rotate but don't weren't drawn facing zero degrees (to the right).
3131

3232
V0.9.2
3333

Tests/phaser.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ var Phaser;
467467
_super.call(this, game);
468468
this._angle = 0;
469469
this.z = 0;
470+
// This value is added to the angle of the GameObject.
471+
// For example if you had a sprite drawn facing straight up then you could set
472+
// rotationOffset to 90 and it would correspond correctly with Phasers rotation system
473+
this.rotationOffset = 0;
470474
this.moves = true;
471475
// Input
472476
this.inputEnabled = false;
@@ -1541,11 +1545,11 @@ var Phaser;
15411545
this._dy -= (camera.worldView.y * this.scrollFactor.y);
15421546
}
15431547
// Rotation - needs to work from origin point really, but for now from center
1544-
if(this.angle !== 0 || this.flipped == true) {
1548+
if(this.angle !== 0 || this.rotationOffset !== 0 || this.flipped == true) {
15451549
this._game.stage.context.save();
15461550
this._game.stage.context.translate(this._dx + (this._dw / 2), this._dy + (this._dh / 2));
1547-
if(this.angle !== 0) {
1548-
this._game.stage.context.rotate(this.angle * (Math.PI / 180));
1551+
if(this.angle !== 0 || this.rotationOffset !== 0) {
1552+
this._game.stage.context.rotate((this.rotationOffset + this.angle) * (Math.PI / 180));
15491553
}
15501554
this._dx = -(this._dw / 2);
15511555
this._dy = -(this._dh / 2);
@@ -6983,6 +6987,9 @@ var Phaser;
69836987
* @return A Point where Point.x contains the velocity x value and Point.y contains the velocity y value
69846988
*/
69856989
function (angle, speed) {
6990+
if(isNaN(speed)) {
6991+
speed = 0;
6992+
}
69866993
var a = this._game.math.degreesToRadians(angle);
69876994
return new Phaser.Point((Math.cos(a) * speed), (Math.sin(a) * speed));
69886995
};

Tests/scrollzones/parallax.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
}
99
function create() {
1010
var zone = myGame.createScrollZone('starray');
11+
// Hide the default region (the full image)
1112
zone.currentRegion.visible = false;
1213
var y = 0;
1314
var speed = 16;

Tests/scrollzones/parallax.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
var zone: Phaser.ScrollZone = myGame.createScrollZone('starray');
1919

20+
// Hide the default region (the full image)
2021
zone.currentRegion.visible = false;
2122

2223
var y:number = 0;

Tests/scrollzones/texture repeat.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,49 @@
33
(function () {
44
var myGame = new Phaser.Game(this, 'game', 800, 600, init, create, update);
55
function init() {
6+
myGame.loader.addImageFile('nashwan', 'assets/sprites/xenon2_ship.png');
67
myGame.loader.addImageFile('starfield', 'assets/misc/starfield.jpg');
8+
myGame.loader.addImageFile('jet', 'assets/sprites/jets.png');
79
myGame.loader.load();
810
}
911
var scroller;
12+
var emitter;
1013
var ship;
14+
var speed = 0;
1115
function create() {
12-
// 512 x 512
13-
scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 512);
14-
// Some sin/cos data for the movement
15-
myGame.math.sinCosGenerator(256, 4, 4, 2);
16+
scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 1024);
17+
emitter = myGame.createEmitter(myGame.stage.centerX + 16, myGame.stage.centerY + 12);
18+
emitter.makeParticles('jet', 250, 0, false, 0);
19+
//emitter.lifespan
20+
ship = myGame.createSprite(myGame.stage.centerX, myGame.stage.centerY, 'nashwan');
21+
// We do this because the ship was drawn facing up, but 0 degrees is pointing to the right
22+
ship.rotationOffset = 90;
1623
}
1724
function update() {
18-
scroller.currentRegion.scrollSpeed.x = myGame.math.shiftSinTable();
19-
scroller.currentRegion.scrollSpeed.y = myGame.math.shiftCosTable();
25+
ship.angularVelocity = 0;
26+
if(myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
27+
ship.angularVelocity = -200;
28+
} else if(myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
29+
ship.angularVelocity = 200;
30+
}
31+
if(myGame.input.keyboard.isDown(Phaser.Keyboard.UP)) {
32+
speed += 0.1;
33+
if(speed > 10) {
34+
speed = 10;
35+
}
36+
} else {
37+
speed -= 0.1;
38+
if(speed < 0) {
39+
speed = 0;
40+
}
41+
}
42+
var motion = myGame.motion.velocityFromAngle(ship.angle, speed);
43+
scroller.setSpeed(motion.x, motion.y);
44+
// emit particles
45+
if(speed > 2) {
46+
emitter.setXSpeed(-(motion.x * 20), -(motion.x * 30));
47+
emitter.setYSpeed(-(motion.y * 20), -(motion.y * 30));
48+
emitter.emitParticle();
49+
}
2050
}
2151
})();

Tests/scrollzones/texture repeat.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,78 @@
77

88
function init() {
99

10+
myGame.loader.addImageFile('nashwan', 'assets/sprites/xenon2_ship.png');
1011
myGame.loader.addImageFile('starfield', 'assets/misc/starfield.jpg');
12+
myGame.loader.addImageFile('jet', 'assets/sprites/jets.png');
1113

1214
myGame.loader.load();
1315

1416
}
1517

1618
var scroller: Phaser.ScrollZone;
19+
var emitter: Phaser.Emitter;
1720
var ship: Phaser.Sprite;
1821

22+
var speed: number = 0;
23+
1924
function create() {
2025

21-
// 512 x 512
22-
scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 512);
26+
scroller = myGame.createScrollZone('starfield', 0, 0, 1024, 1024);
27+
28+
emitter = myGame.createEmitter(myGame.stage.centerX + 16, myGame.stage.centerY + 12);
29+
emitter.makeParticles('jet', 250, 0, false, 0);
30+
//emitter.lifespan
31+
2332

24-
// Some sin/cos data for the movement
25-
myGame.math.sinCosGenerator(256, 4, 4, 2);
33+
ship = myGame.createSprite(myGame.stage.centerX, myGame.stage.centerY, 'nashwan');
34+
35+
// We do this because the ship was drawn facing up, but 0 degrees is pointing to the right
36+
ship.rotationOffset = 90;
2637

2738
}
2839

2940
function update() {
3041

31-
scroller.currentRegion.scrollSpeed.x = myGame.math.shiftSinTable();
32-
scroller.currentRegion.scrollSpeed.y = myGame.math.shiftCosTable();
42+
ship.angularVelocity = 0;
43+
44+
if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
45+
{
46+
ship.angularVelocity = -200;
47+
}
48+
else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
49+
{
50+
ship.angularVelocity = 200;
51+
}
52+
53+
if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
54+
{
55+
speed += 0.1;
56+
57+
if (speed > 10)
58+
{
59+
speed = 10;
60+
}
61+
}
62+
else
63+
{
64+
speed -= 0.1;
65+
66+
if (speed < 0) {
67+
speed = 0;
68+
}
69+
}
70+
71+
var motion:Phaser.Point = myGame.motion.velocityFromAngle(ship.angle, speed);
72+
73+
scroller.setSpeed(motion.x, motion.y);
74+
75+
// emit particles
76+
if (speed > 2)
77+
{
78+
emitter.setXSpeed(-(motion.x * 20), -(motion.x * 30));
79+
emitter.setYSpeed(-(motion.y * 20), -(motion.y * 30));
80+
emitter.emitParticle();
81+
}
3382

3483
}
3584

build/phaser.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ var Phaser;
467467
_super.call(this, game);
468468
this._angle = 0;
469469
this.z = 0;
470+
// This value is added to the angle of the GameObject.
471+
// For example if you had a sprite drawn facing straight up then you could set
472+
// rotationOffset to 90 and it would correspond correctly with Phasers rotation system
473+
this.rotationOffset = 0;
470474
this.moves = true;
471475
// Input
472476
this.inputEnabled = false;
@@ -1541,11 +1545,11 @@ var Phaser;
15411545
this._dy -= (camera.worldView.y * this.scrollFactor.y);
15421546
}
15431547
// Rotation - needs to work from origin point really, but for now from center
1544-
if(this.angle !== 0 || this.flipped == true) {
1548+
if(this.angle !== 0 || this.rotationOffset !== 0 || this.flipped == true) {
15451549
this._game.stage.context.save();
15461550
this._game.stage.context.translate(this._dx + (this._dw / 2), this._dy + (this._dh / 2));
1547-
if(this.angle !== 0) {
1548-
this._game.stage.context.rotate(this.angle * (Math.PI / 180));
1551+
if(this.angle !== 0 || this.rotationOffset !== 0) {
1552+
this._game.stage.context.rotate((this.rotationOffset + this.angle) * (Math.PI / 180));
15491553
}
15501554
this._dx = -(this._dw / 2);
15511555
this._dy = -(this._dh / 2);
@@ -6983,6 +6987,9 @@ var Phaser;
69836987
* @return A Point where Point.x contains the velocity x value and Point.y contains the velocity y value
69846988
*/
69856989
function (angle, speed) {
6990+
if(isNaN(speed)) {
6991+
speed = 0;
6992+
}
69866993
var a = this._game.math.degreesToRadians(angle);
69876994
return new Phaser.Point((Math.cos(a) * speed), (Math.sin(a) * speed));
69886995
};

0 commit comments

Comments
 (0)