mirror of
https://github.com/wassname/phaser.git
synced 2026-08-09 12:20:34 +08:00
Phaser.Game constructor can now be passed a single object containing game settings + Stage settings, useful for advanced configurations.
This commit is contained in:
+157
-22
@@ -24,55 +24,61 @@
|
||||
*/
|
||||
Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias) {
|
||||
|
||||
width = width || 800;
|
||||
height = height || 600;
|
||||
renderer = renderer || Phaser.AUTO;
|
||||
parent = parent || '';
|
||||
state = state || null;
|
||||
|
||||
if (typeof transparent === 'undefined') { transparent = false; }
|
||||
if (typeof antialias === 'undefined') { antialias = true; }
|
||||
|
||||
/**
|
||||
* @property {number} id - Phaser Game ID (for when Pixi supports multiple instances).
|
||||
*/
|
||||
this.id = Phaser.GAMES.push(this) - 1;
|
||||
|
||||
/**
|
||||
* @property {HTMLElement} parent - The Games DOM parent.
|
||||
* @property {object} config - The Phaser.Game configuration object.
|
||||
*/
|
||||
this.parent = parent;
|
||||
this.config = null;
|
||||
|
||||
/**
|
||||
* @property {HTMLElement} parent - The Games DOM parent.
|
||||
* @default
|
||||
*/
|
||||
this.parent = '';
|
||||
|
||||
/**
|
||||
* @property {number} width - The Game width (in pixels).
|
||||
* @default
|
||||
*/
|
||||
this.width = width;
|
||||
this.width = 800;
|
||||
|
||||
/**
|
||||
* @property {number} height - The Game height (in pixels).
|
||||
* @default
|
||||
*/
|
||||
this.height = height;
|
||||
this.height = 600;
|
||||
|
||||
/**
|
||||
* @property {boolean} transparent - Use a transparent canvas background or not.
|
||||
* @default
|
||||
*/
|
||||
this.transparent = transparent;
|
||||
this.transparent = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} antialias - Anti-alias graphics (in WebGL this helps with edges, in Canvas2D it retains pixel-art quality).
|
||||
* @default
|
||||
*/
|
||||
this.antialias = antialias;
|
||||
this.antialias = true;
|
||||
|
||||
/**
|
||||
* @property {number} renderer - The Pixi Renderer
|
||||
* @default
|
||||
*/
|
||||
this.renderer = null;
|
||||
this.renderer = Phaser.AUTO;
|
||||
|
||||
/**
|
||||
* @property {number} renderType - The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL.
|
||||
*/
|
||||
this.renderType = Phaser.AUTO;
|
||||
|
||||
/**
|
||||
* @property {number} state - The StateManager.
|
||||
*/
|
||||
this.state = new Phaser.StateManager(this, state);
|
||||
this.state = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} _paused - Is game paused?
|
||||
@@ -81,11 +87,6 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
|
||||
*/
|
||||
this._paused = false;
|
||||
|
||||
/**
|
||||
* @property {number} renderType - The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL.
|
||||
*/
|
||||
this.renderType = renderer;
|
||||
|
||||
/**
|
||||
* @property {boolean} _loadComplete - Whether load complete loading or not.
|
||||
* @private
|
||||
@@ -225,6 +226,47 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
|
||||
*/
|
||||
this.particles = null;
|
||||
|
||||
// Parse the configuration object (if any)
|
||||
if (arguments.length === 1 && typeof arguments[0] === 'object')
|
||||
{
|
||||
this.parseConfig(arguments[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (typeof width !== 'undefined')
|
||||
{
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
if (typeof height !== 'undefined')
|
||||
{
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
if (typeof renderer !== 'undefined')
|
||||
{
|
||||
this.renderer = renderer;
|
||||
this.renderType = renderer;
|
||||
}
|
||||
|
||||
if (typeof parent !== 'undefined')
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
if (typeof transparent !== 'undefined')
|
||||
{
|
||||
this.transparent = transparent;
|
||||
}
|
||||
|
||||
if (typeof antialias !== 'undefined')
|
||||
{
|
||||
this.antialias = antialias;
|
||||
}
|
||||
|
||||
this.state = new Phaser.StateManager(this, state);
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._onBoot = function () {
|
||||
@@ -247,6 +289,99 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
|
||||
|
||||
Phaser.Game.prototype = {
|
||||
|
||||
/**
|
||||
* Parses a Game configuration object.
|
||||
*
|
||||
* @method Phaser.Game#parseConfig
|
||||
* @protected
|
||||
*/
|
||||
parseConfig: function (config) {
|
||||
|
||||
this.config = config;
|
||||
|
||||
if (config['width'])
|
||||
{
|
||||
this.width = this.parseDimension(config['width'], 0);
|
||||
}
|
||||
|
||||
if (config['height'])
|
||||
{
|
||||
this.height = this.parseDimension(config['height'], 1);
|
||||
}
|
||||
|
||||
if (config['renderer'])
|
||||
{
|
||||
this.renderer = config['renderer'];
|
||||
this.renderType = config['renderer'];
|
||||
}
|
||||
|
||||
if (config['parent'])
|
||||
{
|
||||
this.parent = config['parent'];
|
||||
}
|
||||
|
||||
if (config['transparent'])
|
||||
{
|
||||
this.transparent = config['transparent'];
|
||||
}
|
||||
|
||||
if (config['antialias'])
|
||||
{
|
||||
this.antialias = config['antialias'];
|
||||
}
|
||||
|
||||
var state = null;
|
||||
|
||||
if (config['state'])
|
||||
{
|
||||
state = config['state'];
|
||||
}
|
||||
|
||||
this.state = new Phaser.StateManager(this, state);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get dimension.
|
||||
*
|
||||
* @method Phaser.Game#parseDimension
|
||||
* @protected
|
||||
*/
|
||||
parseDimension: function (size, dimension) {
|
||||
|
||||
var f = 0;
|
||||
var px = 0;
|
||||
|
||||
if (typeof size === 'string')
|
||||
{
|
||||
// %?
|
||||
if (size.substr(-1) === '%')
|
||||
{
|
||||
f = parseInt(size, 10) / 100;
|
||||
|
||||
if (dimension === 0)
|
||||
{
|
||||
px = window.innerWidth * f;
|
||||
}
|
||||
else
|
||||
{
|
||||
px = window.innerHeight * f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
px = parseInt(size, 10);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
px = size;
|
||||
}
|
||||
|
||||
return px;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize engine sub modules and start the game.
|
||||
*
|
||||
|
||||
+65
-4
@@ -24,7 +24,6 @@ Phaser.Stage = function (game, width, height) {
|
||||
/**
|
||||
* @property {string} game - Background color of the stage (defaults to black). Set via the public backgroundColor property.
|
||||
* @private
|
||||
* @default 'rgb(0,0,0)'
|
||||
*/
|
||||
this._backgroundColor = 'rgb(0,0,0)';
|
||||
|
||||
@@ -34,10 +33,9 @@ Phaser.Stage = function (game, width, height) {
|
||||
this.offset = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {HTMLCanvasElement} canvas - Reference to the newly created <canvas> element.
|
||||
* @property {HTMLCanvasElement} canvas - Reference to the newly created `canvas` element.
|
||||
*/
|
||||
this.canvas = Phaser.Canvas.create(width, height);
|
||||
this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
|
||||
this.canvas = null;
|
||||
|
||||
/**
|
||||
* @property {PIXI.Stage} _stage - The Pixi Stage which is hooked to the renderer.
|
||||
@@ -90,10 +88,73 @@ Phaser.Stage = function (game, width, height) {
|
||||
*/
|
||||
this.checkOffsetInterval = 2500;
|
||||
|
||||
if (game.config)
|
||||
{
|
||||
this.parseConfig(game.config);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.canvas = Phaser.Canvas.create(width, height);
|
||||
this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Phaser.Stage.prototype = {
|
||||
|
||||
/**
|
||||
* Parses a Game configuration object.
|
||||
*
|
||||
* @method Phaser.Stage#parseConfig
|
||||
* @protected
|
||||
*/
|
||||
parseConfig: function (config) {
|
||||
|
||||
if (config['canvasID'])
|
||||
{
|
||||
this.canvas = Phaser.Canvas.create(this.game.width, this.game.height, config['canvasID']);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.canvas = Phaser.Canvas.create(this.game.width, this.game.height);
|
||||
}
|
||||
|
||||
if (config['canvasStyle'])
|
||||
{
|
||||
this.canvas.stlye = config['canvasStyle'];
|
||||
}
|
||||
else
|
||||
{
|
||||
this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
|
||||
}
|
||||
|
||||
if (config['checkOffsetInterval'])
|
||||
{
|
||||
this.checkOffsetInterval = config['checkOffsetInterval'];
|
||||
}
|
||||
|
||||
if (config['disableVisibilityChange'])
|
||||
{
|
||||
this.disableVisibilityChange = config['disableVisibilityChange'];
|
||||
}
|
||||
|
||||
if (config['fullScreenScaleMode'])
|
||||
{
|
||||
this.fullScreenScaleMode = config['fullScreenScaleMode'];
|
||||
}
|
||||
|
||||
if (config['scaleMode'])
|
||||
{
|
||||
this.scaleMode = config['scaleMode'];
|
||||
}
|
||||
|
||||
if (config['backgroundColor'])
|
||||
{
|
||||
this.backgroundColor = config['backgroundColor'];
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialises the stage and adds the event listeners.
|
||||
* @method Phaser.Stage#boot
|
||||
|
||||
@@ -32,7 +32,7 @@ Phaser.StateManager = function (game, pendingState) {
|
||||
*/
|
||||
this._pendingState = null;
|
||||
|
||||
if (pendingState !== null)
|
||||
if (typeof pendingState !== 'undefined' && pendingState !== null)
|
||||
{
|
||||
this._pendingState = pendingState;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user