Skip to content

Commit e581271

Browse files
committed
Added Spring class. Fixed reason why World wasn't pre and post updating (Stage didn't have an exists property).
1 parent e922bbd commit e581271

10 files changed

Lines changed: 95 additions & 2525 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ module.exports = function (grunt) {
136136
'src/physics/World.js',
137137
'src/physics/PointProxy.js',
138138
'src/physics/Body.js',
139+
'src/physics/Spring.js',
139140

140141
'src/particles/Particles.js',
141142
'src/particles/arcade/ArcadeParticles.js',

build/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<script src="$path/src/physics/World.js"></script>
183183
<script src="$path/src/physics/PointProxy.js"></script>
184184
<script src="$path/src/physics/Body.js"></script>
185+
<script src="$path/src/physics/Spring.js"></script>
185186
186187
<script src="$path/src/particles/Particles.js"></script>
187188
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script>

examples/wip/tilesprite fixed to cam.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function create() {
2121
sprite = game.add.tileSprite(100, 100, 200, 200, 'starfield');
2222
sprite.autoScroll(0, 100);
2323
sprite.fixedToCamera = true;
24+
console.log(sprite.parent);
2425

2526
mushroom = game.add.sprite(400, 400, 'mushroom');
2627

src/core/Group.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,9 @@ Phaser.Group.prototype.preUpdate = function () {
734734
return false;
735735
}
736736

737-
for (var i = this.children.length - 1; i >= 0; i--)
737+
var i = this.children.length;
738+
739+
while (i--)
738740
{
739741
this.children[i].preUpdate();
740742
}
@@ -750,7 +752,9 @@ Phaser.Group.prototype.preUpdate = function () {
750752
*/
751753
Phaser.Group.prototype.update = function () {
752754

753-
for (var i = this.children.length - 1; i >= 0; i--)
755+
var i = this.children.length;
756+
757+
while (i--)
754758
{
755759
this.children[i].update();
756760
}
@@ -771,7 +775,9 @@ Phaser.Group.prototype.postUpdate = function () {
771775
this.y = this.game.camera.view.y + this.cameraOffset.y;
772776
}
773777

774-
for (var i = this.children.length - 1; i >= 0; i--)
778+
var i = this.children.length;
779+
780+
while (i--)
775781
{
776782
this.children[i].postUpdate();
777783
}

src/core/Stage.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ Phaser.Stage = function (game, width, height) {
4949
*/
5050
this.checkOffsetInterval = 2500;
5151

52+
/**
53+
* @property {boolean} exists - If exists is true the Stage and all children are updated, otherwise it is skipped.
54+
* @default
55+
*/
56+
this.exists = true;
57+
5258
/**
5359
* @property {number} _nextOffsetCheck - The time to run the next offset check.
5460
* @private

src/core/World.js

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -95,81 +95,6 @@ Phaser.World.prototype.setBounds = function (x, y, width, height) {
9595

9696
}
9797

98-
/**
99-
* This is called automatically after the plugins preUpdate and before the State.update.
100-
* Most objects have preUpdate methods and it's where initial movement and positioning is done.
101-
*
102-
* @method Phaser.World#preUpdate
103-
*/
104-
Phaser.World.prototype.preUpdate = function () {
105-
106-
this.currentRenderOrderID = 0;
107-
108-
var i = this.children.length;
109-
110-
while (i--)
111-
{
112-
this.children[i].preUpdate();
113-
}
114-
115-
}
116-
117-
/**
118-
* This is called automatically after the State.update, but before particles or plugins update.
119-
*
120-
* @method Phaser.World#update
121-
*/
122-
Phaser.World.prototype.update = function () {
123-
124-
var i = this.children.length;
125-
126-
while (i--)
127-
{
128-
this.children[i].update();
129-
}
130-
131-
}
132-
133-
/**
134-
* This is called automatically before the renderer runs and after the plugins have updated.
135-
* In postUpdate this is where all the final physics calculatations and object positioning happens.
136-
* The objects are processed in the order of the display list.
137-
* The only exception to this is if the camera is following an object, in which case that is updated first.
138-
*
139-
* @method Phaser.World#postUpdate
140-
*/
141-
Phaser.World.prototype.postUpdate = function () {
142-
143-
if (this.game.world.camera.target)
144-
{
145-
this.game.world.camera.target.postUpdate();
146-
147-
this.game.world.camera.update();
148-
149-
var i = this.children.length;
150-
151-
while (i--)
152-
{
153-
if (this.children[i] !== this.game.world.camera.target)
154-
{
155-
this.children[i].postUpdate();
156-
}
157-
}
158-
}
159-
else
160-
{
161-
this.game.world.camera.update();
162-
163-
var i = this.children.length;
164-
165-
while (i--)
166-
{
167-
this.children[i].postUpdate();
168-
}
169-
}
170-
171-
}
172-
17398
/**
17499
* Destroyer of worlds.
175100
* @method Phaser.World#destroy

src/physics/Spring.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Creates a spring, connecting two bodies.
9+
*
10+
* @class Phaser.Physics.Spring
11+
* @classdesc Physics Spring Constructor
12+
* @constructor
13+
* @param {Phaser.Game} game - A reference to the current game.
14+
* @param {p2.Body} bodyA - First connected body.
15+
* @param {p2.Body} bodyB - Second connected body.
16+
* @param {number} [restLength=1] - Rest length of the spring. A number > 0.
17+
* @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0.
18+
* @param {number} [damping=1] - Damping of the spring. A number >= 0.
19+
* @param {Array} [worldA] - Where to hook the spring to body A, in world coordinates, i.e. [32, 32].
20+
* @param {Array} [worldB] - Where to hook the spring to body B, in world coordinates, i.e. [32, 32].
21+
* @param {Array} [localA] - Where to hook the spring to body A, in local body coordinates.
22+
* @param {Array} [localB] - Where to hook the spring to body B, in local body coordinates.
23+
*/
24+
Phaser.Physics.Spring = function (game, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB) {
25+
26+
/**
27+
* @property {Phaser.Game} game - Local reference to game.
28+
*/
29+
this.game = game;
30+
31+
if (typeof restLength === 'undefined') { restLength = 1; }
32+
if (typeof stiffness === 'undefined') { stiffness = 100; }
33+
if (typeof damping === 'undefined') { damping = 1; }
34+
35+
var options = {
36+
restLength: restLength,
37+
stiffness: stiffness,
38+
damping: damping
39+
};
40+
41+
if (typeof worldA !== 'undefined')
42+
{
43+
options.worldAnchorA = [ game.math.px2p(worldA[0]), game.math.px2p(worldA[1]) ];
44+
}
45+
46+
if (typeof worldB !== 'undefined')
47+
{
48+
options.worldAnchorB = [ game.math.px2p(worldB[0]), game.math.px2p(worldB[1]) ];
49+
}
50+
51+
if (typeof localA !== 'undefined')
52+
{
53+
options.localAnchorA = [ game.math.px2p(localA[0]), game.math.px2p(localA[1]) ];
54+
}
55+
56+
if (typeof localB !== 'undefined')
57+
{
58+
options.localAnchorB = [ game.math.px2p(localB[0]), game.math.px2p(localB[1]) ];
59+
}
60+
61+
p2.Spring.call(this, bodyA, bodyB, options);
62+
63+
}
64+
65+
Phaser.Physics.Spring.prototype = Object.create(p2.Spring.prototype);
66+
Phaser.Physics.Spring.prototype.constructor = Phaser.Physics.Spring;

src/physics/World.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,14 @@ Phaser.Physics.World.prototype.update = function () {
221221
this.step(1 / 60);
222222

223223
};
224+
225+
/**
226+
* @method Phaser.Physics.World.prototype.destroy
227+
*/
228+
Phaser.Physics.World.prototype.destroy = function () {
229+
230+
this.clear();
231+
232+
this.game = null;
233+
234+
};

0 commit comments

Comments
 (0)