Skip to content

Commit 60f0e89

Browse files
committed
Added Phaser.Graphics and fixed the Out of World Bounds call.
1 parent 30fc409 commit 60f0e89

8 files changed

Lines changed: 669 additions & 123 deletions

File tree

Phaser.sublime-workspace

Lines changed: 397 additions & 114 deletions
Large diffs are not rendered by default.

examples/graphics.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, render: render });
16+
17+
function preload() {
18+
19+
game.load.image('atari1', 'assets/sprites/atari130xe.png');
20+
21+
}
22+
23+
var graphics;
24+
25+
function create() {
26+
27+
graphics = game.add.graphics(0, 0);
28+
29+
// set a fill and line style
30+
graphics.beginFill(0xFF3300);
31+
graphics.lineStyle(10, 0xffd900, 1);
32+
33+
// draw a shape
34+
graphics.moveTo(50,50);
35+
graphics.lineTo(250, 50);
36+
graphics.lineTo(100, 100);
37+
graphics.lineTo(250, 220);
38+
graphics.lineTo(50, 220);
39+
graphics.lineTo(50, 50);
40+
graphics.endFill();
41+
42+
// set a fill and line style again
43+
graphics.lineStyle(10, 0xFF0000, 0.8);
44+
graphics.beginFill(0xFF700B, 1);
45+
46+
// draw a second shape
47+
graphics.moveTo(210,300);
48+
graphics.lineTo(450,320);
49+
graphics.lineTo(570,350);
50+
graphics.lineTo(580,20);
51+
graphics.lineTo(330,120);
52+
graphics.lineTo(410,200);
53+
graphics.lineTo(210,300);
54+
graphics.endFill();
55+
56+
// draw a rectangel
57+
graphics.lineStyle(2, 0x0000FF, 1);
58+
graphics.drawRect(50, 250, 100, 100);
59+
60+
// draw a circle
61+
graphics.lineStyle(0);
62+
graphics.beginFill(0xFFFF0B, 0.5);
63+
graphics.drawCircle(470, 200,100);
64+
65+
graphics.lineStyle(20, 0x33FF00);
66+
graphics.moveTo(30,30);
67+
graphics.lineTo(600, 300);
68+
69+
game.add.tween(graphics).to({ x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
70+
71+
}
72+
73+
function render() {
74+
}
75+
76+
})();
77+
</script>
78+
79+
</body>
80+
</html>

examples/js.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<script src="../src/gameobjects/TileSprite.js"></script>
6565
<script src="../src/gameobjects/Text.js"></script>
6666
<script src="../src/gameobjects/Button.js"></script>
67+
<script src="../src/gameobjects/Graphics.js"></script>
6768
<script src="../src/system/Canvas.js"></script>
6869
<script src="../src/system/StageScaleMode.js"></script>
6970
<script src="../src/system/Device.js"></script>

examples/outofbounds.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
(function () {
1414

15-
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
1616

1717
function preload() {
1818
game.load.image('alien', 'assets/sprites/space-baddie.png');
@@ -33,18 +33,21 @@ function create() {
3333
{
3434
for (var x = 0; x < 10; x++)
3535
{
36-
var alien = aliens.create(x * 48, y * 50, 'alien');
36+
var alien = aliens.create(200 + x * 48, y * 50, 'alien');
3737
alien.name = 'alien' + x.toString() + y.toString();
3838
alien.events.onOutOfBounds.add(alienOut, this);
39-
alien.body.velocity.y = 50 + Math.random() * 150;
39+
alien.body.velocity.y = 50 + Math.random() * 200;
4040
}
4141
}
4242

4343
}
4444

4545
function alienOut(alien) {
4646

47+
// Move the alien to the top of the screen again
4748
alien.reset(alien.x, -32);
49+
// And give it a new random velocity
50+
alien.body.velocity.y = 50 + Math.random() * 200;
4851

4952
}
5053

src/gameobjects/GameObjectFactory.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ Phaser.GameObjectFactory.prototype = {
8585

8686
},
8787

88+
graphics: function (x, y) {
89+
90+
return this.world.add(new Phaser.Graphics(this.game, x, y));
91+
92+
},
93+
8894
};

src/gameobjects/Graphics.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
Phaser.Graphics = function (game, x, y) {
2+
3+
// If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all
4+
this.exists = true;
5+
6+
// This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering
7+
this.alive = true;
8+
9+
this.group = null;
10+
11+
this.name = '';
12+
13+
this.game = game;
14+
15+
PIXI.DisplayObjectContainer.call(this);
16+
17+
this.position.x = x;
18+
this.position.y = y;
19+
20+
// Replaces the PIXI.Point with a slightly more flexible one
21+
this.scale = new Phaser.Point(1, 1);
22+
23+
// Influence of camera movement upon the position
24+
this.scrollFactor = new Phaser.Point(1, 1);
25+
26+
// A mini cache for storing all of the calculated values
27+
this._cache = {
28+
29+
dirty: false,
30+
31+
// Transform cache
32+
a00: 1, a01: 0, a02: x, a10: 0, a11: 1, a12: y, id: 1,
33+
34+
// The previous calculated position inc. camera x/y and scrollFactor
35+
x: -1, y: -1,
36+
37+
// The actual scale values based on the worldTransform
38+
scaleX: 1, scaleY: 1
39+
40+
};
41+
42+
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
43+
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
44+
45+
this.renderable = true;
46+
47+
/**
48+
* The alpha of the fill of this graphics object
49+
*
50+
* @property fillAlpha
51+
* @type Number
52+
*/
53+
this.fillAlpha = 1;
54+
55+
/**
56+
* The width of any lines drawn
57+
*
58+
* @property lineWidth
59+
* @type Number
60+
*/
61+
this.lineWidth = 0;
62+
63+
/**
64+
* The color of any lines drawn
65+
*
66+
* @property lineColor
67+
* @type String
68+
*/
69+
this.lineColor = "black";
70+
71+
/**
72+
* Graphics data
73+
*
74+
* @property graphicsData
75+
* @type Array
76+
* @private
77+
*/
78+
this.graphicsData = [];
79+
80+
/**
81+
* Current path
82+
*
83+
* @property currentPath
84+
* @type Object
85+
* @private
86+
*/
87+
this.currentPath = {points:[]};
88+
89+
};
90+
91+
Phaser.Graphics.prototype = Phaser.Utils.extend(true, PIXI.Graphics.prototype, PIXI.DisplayObjectContainer.prototype, Phaser.Sprite.prototype);
92+
Phaser.Graphics.prototype.constructor = Phaser.Graphics;
93+
94+
// Add our own custom methods
95+
96+
/**
97+
* Automatically called by World.update
98+
*/
99+
Phaser.Graphics.prototype.update = function() {
100+
101+
if (!this.exists)
102+
{
103+
return;
104+
}
105+
106+
this._cache.dirty = false;
107+
108+
this._cache.x = this.x - (this.game.world.camera.x * this.scrollFactor.x);
109+
this._cache.y = this.y - (this.game.world.camera.y * this.scrollFactor.y);
110+
111+
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
112+
{
113+
this.position.x = this._cache.x;
114+
this.position.y = this._cache.y;
115+
this._cache.dirty = true;
116+
}
117+
118+
}
119+
120+
Object.defineProperty(Phaser.Graphics.prototype, 'angle', {
121+
122+
get: function() {
123+
return Phaser.Math.radToDeg(this.rotation);
124+
},
125+
126+
set: function(value) {
127+
this.rotation = Phaser.Math.degToRad(value);
128+
}
129+
130+
});
131+
132+
Object.defineProperty(Phaser.Graphics.prototype, 'x', {
133+
134+
get: function() {
135+
return this.position.x;
136+
},
137+
138+
set: function(value) {
139+
this.position.x = value;
140+
}
141+
142+
});
143+
144+
Object.defineProperty(Phaser.Graphics.prototype, 'y', {
145+
146+
get: function() {
147+
return this.position.y;
148+
},
149+
150+
set: function(value) {
151+
this.position.y = value;
152+
}
153+
154+
});

src/gameobjects/Sprite.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
142142
// Set-up the physics body
143143
this.body = new Phaser.Physics.Arcade.Body(this);
144144

145+
// World bounds check
146+
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds);
147+
this.inWorldThreshold = 0;
145148
this._outOfBoundsFired = false;
146149

147150
};
@@ -280,7 +283,7 @@ Phaser.Sprite.prototype.reset = function(x, y) {
280283
this.exists = true;
281284
this.visible = true;
282285
this._outOfBoundsFired = false;
283-
286+
284287
}
285288

286289
Phaser.Sprite.prototype.updateBounds = function() {
@@ -306,11 +309,27 @@ Phaser.Sprite.prototype.updateBounds = function() {
306309
this._cache.boundsX = this._cache.x;
307310
this._cache.boundsY = this._cache.y;
308311

309-
// Check world bounds
310-
if (this._outOfBoundsFired == false && Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, 100) == false)
312+
if (this.inWorld == false)
311313
{
312-
this.events.onOutOfBounds.dispatch(this);
313-
this._outOfBoundsFired = true;
314+
// Sprite WAS out of the screen, is it still?
315+
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
316+
317+
if (this.inWorld)
318+
{
319+
// It's back again, reset the OOB check
320+
this._outOfBoundsFired = false;
321+
}
322+
}
323+
else
324+
{
325+
// Sprite WAS in the screen, has it now left?
326+
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold);
327+
328+
if (this.inWorld == false)
329+
{
330+
this.events.onOutOfBounds.dispatch(this);
331+
this._outOfBoundsFired = true;
332+
}
314333
}
315334

316335
}

src/tween/TweenManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Phaser.TweenManager.prototype = {
6060
*/
6161
create: function (object, localReference) {
6262

63-
if (typeof localReference === "undefined") { localReference = false; }
63+
localReference = localReference || false;
6464

6565
if (localReference)
6666
{

0 commit comments

Comments
 (0)