Skip to content

Commit 3cac9fb

Browse files
committed
Added Game Object Creator.
1 parent 28db9a2 commit 3cac9fb

4 files changed

Lines changed: 53 additions & 24 deletions

File tree

v3/src/gameobjects/FactoryContainer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ var FactoryContainer = function ()
4343
return factories[key];
4444
};
4545

46-
this.load = function (dest)
46+
this.load = function (dest, isFactory)
4747
{
4848
for (var factory in factories)
4949
{
50-
console.log('Testing load of:', factory);
51-
5250
if (factories.hasOwnProperty(factory))
5351
{
54-
console.log('Loading', factory);
55-
dest[factory] = factories[factory].add;
52+
// console.log('Loading', factory);
53+
54+
dest[factory] = (isFactory) ? factories[factory].add : factories[factory].make;
5655
}
5756
}
5857

v3/src/gameobjects/image/ImageFactory.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var Image = require('./Image');
78
var FactoryContainer = require('../../gameobjects/FactoryContainer');
89

910
var ImageFactory = {
@@ -26,31 +27,20 @@ var ImageFactory = {
2627
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
2728
* @return {Phaser.Image} The newly created Image object.
2829
*/
29-
add: function (x, y, key, frame, group, name)
30+
add: function (x, y, key, frame, group)
3031
{
31-
console.log('ImageFactory.add', key, x, y, frame, group, name);
32-
console.log('into State', this.state);
32+
if (group === undefined) { group = this.state; }
3333

34-
// if (group === undefined) { group = this.state; }
34+
console.log('ImageFactory.add', key, x, y, frame, group);
35+
console.log('into State', this.state);
3536

36-
// return group.children.add(new Image(this.state, x, y, key, frame, name));
37+
return group.children.add(new Image(this.state, x, y, key, frame));
3738
},
3839

39-
/**
40-
* Create a new Image object.
41-
*
42-
* An Image is a light-weight object you can use to display anything that doesn't need physics or animation.
43-
* It can still rotate, scale, crop and receive input events. This makes it perfect for logos, backgrounds, simple buttons and other non-Sprite graphics.
44-
*
45-
* @method Phaser.GameObjectCreator#image
46-
* @param {number} x - X position of the image.
47-
* @param {number} y - Y position of the image.
48-
* @param {string|Phaser.RenderTexture|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
49-
* @param {string|number} [frame] - If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
50-
* @return {Phaser.Image} the newly created sprite object.
51-
*/
5240
make: function (x, y, key, frame)
5341
{
42+
console.log('ImageFactory.make', key, x, y, frame);
43+
5444
return new Image(this.state, x, y, key, frame);
5545
}
5646

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2016 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var FactoryContainer = require('../../gameobjects/FactoryContainer');
8+
9+
/**
10+
* The GameObject Factory is a quick way to create many common game objects. The Factory is owned by the State.
11+
*
12+
* @class Phaser.GameObject.Factory
13+
* @constructor
14+
* @param {Phaser.Game} game - A reference to the currently running game.
15+
*/
16+
17+
var GameObjectCreator = {
18+
19+
/**
20+
* @property {Phaser.State} state - The State that owns this Factory
21+
* @protected
22+
*/
23+
state: null
24+
25+
};
26+
27+
function init (state)
28+
{
29+
console.log('Creating GameObjectCreator instance for State', state);
30+
31+
GameObjectCreator.state = state;
32+
33+
// Load the factories into this Object
34+
35+
FactoryContainer.load(GameObjectCreator, false);
36+
37+
return GameObjectCreator;
38+
}
39+
40+
module.exports = init;

v3/src/state/systems/GameObjectFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function init (state)
3232

3333
// Load the factories into this Object
3434

35-
FactoryContainer.load(GameObjectFactory);
35+
FactoryContainer.load(GameObjectFactory, true);
3636

3737
return GameObjectFactory;
3838
}

0 commit comments

Comments
 (0)