State handling done. Refactored the Game constructor a LOT. Now works from within closures, outside of them, with Phaser.State objects or normal Objects with the right functions inside. Also fixed some small bugs in PluginManager and various scope issues with RAF.

This commit is contained in:
Richard Davey
2013-08-29 07:06:16 +01:00
parent 3c8cd20b70
commit 4bbcc380c5
12 changed files with 558 additions and 475 deletions
+88 -385
View File
@@ -3,6 +3,34 @@
{
"selected_items":
[
[
"remem",
"rememberMe"
],
[
"start",
"startState"
],
[
"swit",
"switchState"
],
[
"onpause",
"onPausedCallback"
],
[
"onload",
"onLoadUpdateCallback"
],
[
"onp",
"onPreloadCallback (variable)"
],
[
"upda",
"updateSetTimeout"
],
[
"inter",
"intersectsRaw"
@@ -195,10 +223,6 @@
"jukeb",
"jukeboxPP"
],
[
"start",
"startTheEngines"
],
[
"star",
"startPause"
@@ -490,30 +514,6 @@
[
"die",
"diveSpeed"
],
[
"wave",
"waveY"
],
[
"dive",
"diveCanvas"
],
[
"wa",
"waveY"
],
[
"di",
"diveContext"
],
[
"divec",
"diveContext"
],
[
"wavet",
"waveTween1"
]
]
},
@@ -523,26 +523,9 @@
"file": "src/Game.js",
"settings":
{
"buffer_size": 5482,
"buffer_size": 15461,
"line_ending": "Windows"
}
},
{
"file": "todo/Tween.js",
"settings":
{
"buffer_size": 12434,
"line_ending": "Windows"
}
},
{
"file": "src/tween/TweenManager.js",
"settings":
{
"buffer_size": 3317,
"line_ending": "Windows",
"name": "Phaser.TweenManager = function () {"
}
}
],
"build_system": "",
@@ -560,10 +543,7 @@
},
"console":
{
"height": 146.0,
"history":
[
]
"height": 146.0
},
"distraction_free":
{
@@ -576,6 +556,38 @@
},
"file_history":
[
"/D/wamp/www/phaser/examples/flow.php",
"/D/wamp/www/phaser/examples/flow3.php",
"/D/wamp/www/phaser/src/core/State.js",
"/D/wamp/www/phaser/examples/flow2.php",
"/D/wamp/www/phaser/TS Tests/tilemaps/csv tilemap.js",
"/D/wamp/www/phaser/src/system/RequestAnimationFrame.js",
"/D/wamp/www/phaser/TS Source/system/RequestAnimationFrame.js",
"/D/wamp/www/phaser/TS Source/Game.js",
"/D/wamp/www/phaser/TS Source/State.js",
"/D/wamp/www/phaser/examples/js.php",
"/D/wamp/www/phaser/src/Phaser.js",
"/D/wamp/www/phaser/TS Tests/audio/audio sprites 1.ts",
"/D/wamp/www/phaser/examples/loader 2.html",
"/D/wamp/www/phaser/src/tween/TweenManager.js",
"/D/wamp/www/phaser/src/core/Plugin.js",
"/D/wamp/www/phaser/src/core/PluginManager.js",
"/D/wamp/www/index.php",
"/D/wamp/www/phaser/src/loader/Loader.js",
"/D/wamp/www/phaser/TS Source/core/Plugin.js",
"/D/wamp/www/phaser/TS Source/core/PluginManager.js",
"/D/wamp/www/phaser/TS Source/Game.ts",
"/D/wamp/www/phaser/examples/pixi 1.html",
"/D/wamp/www/pixi.js/examples/example 1 - Basics/index.html",
"/D/wamp/www/phaser/src/pixi/textures/Texture.js",
"/D/wamp/www/phaser/src/pixi/textures/BaseTexture.js",
"/D/wamp/www/phaser/examples/loader.html",
"/D/wamp/www/phaser/examples/math sincos.html",
"/D/wamp/www/phaser/examples/raf.html",
"/D/wamp/www/phaser/src/time/Time.js",
"/D/wamp/www/phaser/src/pixi/utils/Utils.js",
"/D/wamp/www/phaser/TS Source/system/RequestAnimationFrame.ts",
"/D/wamp/www/phaser/todo/Tween.js",
"/D/wamp/www/phaser/TS Source/tweens/TweenManager.js",
"/D/wamp/www/phaser/src/geom/Rectangle.js",
"/D/wamp/www/phaser/TS Source/utils/RectangleUtils.js",
@@ -590,17 +602,14 @@
"/D/wamp/www/phaser/TS Source/time/TimeManager.js",
"/D/wamp/www/phaser/TS Source/time/TimeManager.ts",
"/D/wamp/www/phaser/TS Source/math/GameMath.js",
"/D/wamp/www/phaser/examples/math sincos.html",
"/D/wamp/www/phaser/examples/circle.html",
"/D/wamp/www/phaser/TS Source/utils/CircleUtils.js",
"/D/wamp/www/phaser/TS Source/geom/Circle.js",
"/D/wamp/www/phaser/src/time/Time.js",
"/D/wamp/www/phaser/src/net/Net.js",
"/D/wamp/www/phaser/examples/net.html",
"/D/wamp/www/phaser/TS Source/net/Net.js",
"/D/wamp/www/phaser/src/animation/Parser.js",
"/D/wamp/www/phaser/src/loader/Cache.js",
"/D/wamp/www/phaser/src/loader/Loader.js",
"/D/wamp/www/phaser/examples/loader audio.html",
"/D/wamp/www/phaser/examples/loader spritesheet.html",
"/D/wamp/www/phaser/examples/loader atlas xml.html",
@@ -616,14 +625,10 @@
"/D/wamp/www/phaser/TS Source/animation/FrameData.js",
"/D/wamp/www/phaser/TS Source/animation/Frame.ts",
"/D/wamp/www/phaser/TS Source/animation/Frame.js",
"/D/wamp/www/phaser/examples/loader 2.html",
"/D/wamp/www/bbc-wolfblood/game.js",
"/D/wamp/www/phaser/examples/loader.html",
"/D/wamp/www/phaser/TS Source/loader/Loader.js",
"/D/wamp/www/phaser/TS Source/Game.js",
"/D/wamp/www/phaser/TS Source/loader/Cache.js",
"/D/wamp/www/phaser/TS Source/loader/Cache.ts",
"/D/wamp/www/phaser/src/Phaser.js",
"/D/wamp/www/phaser/TS Source/loader/Loader.ts",
"/D/wamp/www/phaser/src/math/RandomDataGenerator.js",
"/D/wamp/www/phaser/src/core/SignalBinding.js",
@@ -633,7 +638,6 @@
"/D/wamp/www/phaser/TS Source/core/Signal.js",
"/D/wamp/www/js-signals/src/Signal.js",
"/D/wamp/www/js-signals/src/wrapper.js",
"/D/wamp/www/pixi.js/examples/example 1 - Basics/index.html",
"/D/wamp/www/awasenghi/lib/Core.js",
"/D/wamp/www/phaser/TS Source/math/RandomDataGenerator.js",
"/D/wamp/www/Nonsense/Nonsense.js",
@@ -679,31 +683,7 @@
"/D/wamp/www/Proton/src/zone/Zone.js",
"/D/wamp/www/Proton/src/zone/PointZone.js",
"/D/wamp/www/Proton/src/plus/ease.js",
"/D/wamp/www/Proton/src/behaviour/RandomDrift.js",
"/D/wamp/www/Proton/src/math/ColorSpan.js",
"/D/wamp/www/Proton/src/math/NumericalIntegration.js",
"/D/wamp/www/Proton/src/math/MathUtils.js",
"/D/wamp/www/Proton/src/behaviour/Behaviour.js",
"/D/wamp/www/Proton/src/initialize/Velocity.js",
"/D/wamp/www/Proton/src/math/Polar2D.js",
"/D/wamp/www/Proton/src/behaviour/Scale.js",
"/D/wamp/www/Proton/src/behaviour/Repulsion.js",
"/D/wamp/www/Proton/src/initialize/Rate.js",
"/D/wamp/www/Proton/src/core/Proton.js",
"/D/wamp/www/Proton/src/core/Particle.js",
"/D/wamp/www/Proton/src/core/ParticlePool.js",
"/D/wamp/www/Proton/src/initialize/Life.js",
"/D/wamp/www/Proton/src/initialize/Radius.js",
"/D/wamp/www/Proton/src/utils/Util.js",
"/D/wamp/www/Proton/src/math/Span.js",
"/D/wamp/www/Proton/src/initialize/Initialize.js",
"/D/wamp/www/Proton/src/initialize/Mass.js",
"/D/wamp/www/bbc-jigsaw/sarahandduck/frame.html",
"/D/wamp/www/phaser/Tests/phaser.css",
"/D/wamp/www/phaser/Docs/tags.txt",
"/D/wamp/www/phaser/Docs/docs_server.bat",
"/D/wamp/www/phaser/Docs/docs_build.bat",
"/D/wamp/www/phaser/Phaser/Phaser.csproj"
"/D/wamp/www/Proton/src/behaviour/RandomDrift.js"
],
"find":
{
@@ -711,7 +691,7 @@
},
"find_in_files":
{
"height": 90.0,
"height": 0.0,
"where_history":
[
"D:\\wamp\\www\\phaser\\src\\pixi",
@@ -736,6 +716,22 @@
"case_sensitive": false,
"find_history":
[
"game",
"State.prototype.",
"preload",
"Callback",
"Plugin.prototype.",
"this._parent",
"PluginManager.prototype.",
"public ",
"startSt",
"_this",
"Phaser.Math",
"_raf",
"raf",
"delta",
"_raf",
"RequestAnimationFrame.prototype.",
"TweenManager.prototype.",
"Phaser.Rectangle.",
"a.",
@@ -847,23 +843,7 @@
"solveVelocity",
"velocitySolver",
"initSolve",
"step",
"contactSolverArr",
"add",
"findContact",
"contactSolvers",
"add",
"norma",
"saveSceneToServer",
"console",
"saveSceneToServer",
"drawBox",
"drawBodyShape",
"scale",
"damping",
"gravity",
"timeDelta",
" for (var i = 0; i <= 10; i++) {\n var body = new Body(Body.DYNAMIC, new vec2(-6 + i * 1.2, 8));\n var shape = new ShapeCircle(0, 0, 0.4);\n shape.e = i / 10;\n shape.u = 1.0;\n shape.density = 1;\n body.addShape(shape);\n body.resetMassData();\n space.addBody(body);\n }\n"
"step"
],
"highlight": true,
"in_selection": false,
@@ -871,6 +851,7 @@
"regex": false,
"replace_history":
[
"elapsed",
"queueSize",
"{boolean}",
"queueSize",
@@ -911,24 +892,23 @@
"groups":
[
{
"selected": 2,
"selected": 0,
"sheets":
[
{
"buffer": 0,
"file": "src/Game.js",
"semi_transient": false,
"settings":
{
"buffer_size": 5482,
"buffer_size": 15461,
"regions":
{
},
"selection":
[
[
1281,
1281
12999,
12999
]
],
"settings":
@@ -1040,269 +1020,7 @@
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 1,
"file": "todo/Tween.js",
"semi_transient": false,
"settings":
{
"buffer_size": 12434,
"regions":
{
},
"selection":
[
[
726,
1538
]
],
"settings":
{
"codeintel": true,
"codeintel_config":
{
"JavaScript":
{
"codeintel_max_recursive_dir_depth": 2,
"codeintel_scan_files_in_project": false,
"javascriptExtraPaths":
[
]
},
"PHP":
{
"codeintel_max_recursive_dir_depth": 5,
"codeintel_scan_files_in_project": false,
"phpExtraPaths":
[
]
},
"Python":
{
"env":
{
}
}
},
"codeintel_enabled_languages":
[
"JavaScript",
"Mason",
"XBL",
"XUL",
"RHTML",
"SCSS",
"Python",
"HTML",
"Ruby",
"Python3",
"XML",
"Sass",
"XSLT",
"Django",
"HTML5",
"Perl",
"CSS",
"Twig",
"Less",
"Smarty",
"Node.js",
"Tcl",
"TemplateToolkit",
"PHP"
],
"codeintel_live": true,
"codeintel_live_enabled_languages":
[
"JavaScript",
"Mason",
"XBL",
"XUL",
"RHTML",
"SCSS",
"Python",
"HTML",
"Ruby",
"Python3",
"XML",
"Sass",
"XSLT",
"Django",
"HTML5",
"Perl",
"CSS",
"Twig",
"Less",
"Smarty",
"Node.js",
"Tcl",
"TemplateToolkit",
"PHP"
],
"codeintel_max_recursive_dir_depth": 10,
"codeintel_scan_exclude_dir":
{
"JavaScript":
[
"/build/",
"/min/"
]
},
"codeintel_scan_files_in_project": true,
"codeintel_selected_catalogs":
[
"PyWin32",
"jQuery",
"Rails"
],
"codeintel_snippets": true,
"codeintel_syntax_map":
{
"Python Django": "Python"
},
"sublime_auto_complete": true,
"syntax": "Packages/JavaScript/JavaScript.tmLanguage",
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 798.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 2,
"file": "src/tween/TweenManager.js",
"semi_transient": false,
"settings":
{
"buffer_size": 3317,
"regions":
{
},
"selection":
[
[
720,
720
]
],
"settings":
{
"auto_name": "Phaser.TweenManager = function () {",
"codeintel": true,
"codeintel_config":
{
"JavaScript":
{
"codeintel_max_recursive_dir_depth": 2,
"codeintel_scan_files_in_project": false,
"javascriptExtraPaths":
[
]
},
"PHP":
{
"codeintel_max_recursive_dir_depth": 5,
"codeintel_scan_files_in_project": false,
"phpExtraPaths":
[
]
},
"Python":
{
"env":
{
}
}
},
"codeintel_enabled_languages":
[
"JavaScript",
"Mason",
"XBL",
"XUL",
"RHTML",
"SCSS",
"Python",
"HTML",
"Ruby",
"Python3",
"XML",
"Sass",
"XSLT",
"Django",
"HTML5",
"Perl",
"CSS",
"Twig",
"Less",
"Smarty",
"Node.js",
"Tcl",
"TemplateToolkit",
"PHP"
],
"codeintel_live": true,
"codeintel_live_enabled_languages":
[
"JavaScript",
"Mason",
"XBL",
"XUL",
"RHTML",
"SCSS",
"Python",
"HTML",
"Ruby",
"Python3",
"XML",
"Sass",
"XSLT",
"Django",
"HTML5",
"Perl",
"CSS",
"Twig",
"Less",
"Smarty",
"Node.js",
"Tcl",
"TemplateToolkit",
"PHP"
],
"codeintel_max_recursive_dir_depth": 10,
"codeintel_scan_exclude_dir":
{
"JavaScript":
[
"/build/",
"/min/"
]
},
"codeintel_scan_files_in_project": true,
"codeintel_selected_catalogs":
[
"PyWin32",
"jQuery",
"Rails"
],
"codeintel_snippets": true,
"codeintel_syntax_map":
{
"Python Django": "Python"
},
"sublime_auto_complete": true,
"syntax": "Packages/JavaScript/JavaScript.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"translation.y": 8910.0,
"zoom_level": 1.0
},
"type": "text"
@@ -1341,7 +1059,6 @@
]
},
"menu_visible": true,
"project": "Phaser.sublime-project",
"replace":
{
"height": 62.0
@@ -1363,24 +1080,10 @@
],
"width": 0.0
},
"select_symbol":
{
"height": 0.0,
"selected_items":
[
],
"width": 0.0
},
"settings":
{
},
"show_minimap": true,
"show_open_files": false,
"show_tabs": true,
"side_bar_visible": true,
"side_bar_width": 284.0,
"status_bar_visible": true,
"template_settings":
{
}
"status_bar_visible": true
}
+53
View File
@@ -0,0 +1,53 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
// Your game will work either from within a closure or not, it doesn't matter
(function () {
// These are all optional parameters now
var game = new Phaser.Game();
// In this approach we're simply using functions in the current scope to do what we need
game.launch(this, preload, create);
function preload() {
console.log('*** preload called');
game.load.image('cockpit', 'assets/pics/cockpit.png');
game.load.image('rememberMe', 'assets/pics/remember-me.jpg');
game.load.image('overdose', 'assets/pics/lance-overdose-loader_eye.png');
game.load.text('copyright', 'assets/warning - copyright.txt');
}
function create() {
console.log('*** create called');
// Let's try adding an image to the DOM
document.body.appendChild(game.cache.getImage('cockpit'));
document.body.appendChild(game.cache.getImage('overdose'));
// And some text
var para = document.createElement('pre');
para.innerHTML = game.cache.getText('copyright');
document.body.appendChild(para);
}
/})();
</script>
</body>
</html>
+55
View File
@@ -0,0 +1,55 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var state = new Phaser.State();
state.preload = function() {
console.log('state preload called');
// When an instance of Phaser.State is added to Phaser.Game it becomes bound to that game
// which opens up lots of handy short-cuts to helpers and libs, such as this.math, this.load, this.cache, etc
this.load.image('cockpit', 'assets/pics/cockpit.png');
this.load.image('rememberMe', 'assets/pics/remember-me.jpg');
this.load.image('overdose', 'assets/pics/lance-overdose-loader_eye.png');
this.load.text('copyright', 'assets/warning - copyright.txt');
}
state.create = function() {
console.log('state create called');
// Let's try adding an image to the DOM
document.body.appendChild(this.cache.getImage('cockpit'));
// And some text
var para = document.createElement('pre');
para.innerHTML = this.cache.getText('copyright');
document.body.appendChild(para);
}
var megaBlasterGame = new Phaser.Game();
// In this approach we're using a Phaser.State object and switching to it
// This gives us the advantage that from within state functions you can access 'this' and most the properties of Phaser.Game
megaBlasterGame.switchState(state);
})();
</script>
</body>
</html>
+50
View File
@@ -0,0 +1,50 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var state = {
preload: function() {
console.log('state preload called');
game.load.image('rememberMe', 'assets/pics/remember-me.jpg');
game.load.text('copyright', 'assets/warning - copyright.txt');
},
create: function() {
console.log('state create called');
// Let's try adding an image to the DOM
document.body.appendChild(game.cache.getImage('rememberMe'));
// And some text
var para = document.createElement('pre');
para.innerHTML = game.cache.getText('copyright');
document.body.appendChild(para);
}
}
var game = new Phaser.Game();
// This method now works either from within a closure or not
game.switchState(state);
})();
</script>
</body>
</html>
+67
View File
@@ -0,0 +1,67 @@
<?php
// All JS files in build order.
// Much easier for debugging
?>
<script src="../src/Phaser.js"></script>
<script src="../src/pixi/Pixi.js"></script>
<script src="../src/pixi/InteractionManager.js"></script>
<script src="../src/pixi/core/Circle.js"></script>
<script src="../src/pixi/core/Ellipse.js"></script>
<script src="../src/pixi/core/Matrix.js"></script>
<script src="../src/pixi/core/Point.js"></script>
<script src="../src/pixi/core/Polygon.js"></script>
<script src="../src/pixi/core/Rectangle.js"></script>
<script src="../src/pixi/display/DisplayObject.js"></script>
<script src="../src/pixi/display/DisplayObjectContainer.js"></script>
<script src="../src/pixi/display/Sprite.js"></script>
<script src="../src/pixi/display/MovieClip.js"></script>
<script src="../src/pixi/display/Stage.js"></script>
<script src="../src/pixi/extras/CustomRenderable.js"></script>
<script src="../src/pixi/extras/Strip.js"></script>
<script src="../src/pixi/extras/Rope.js"></script>
<script src="../src/pixi/extras/Spine.js"></script>
<script src="../src/pixi/extras/TilingSprite.js"></script>
<script src="../src/pixi/filters/FilterBlock.js"></script>
<script src="../src/pixi/filters/MaskFilter.js"></script>
<script src="../src/pixi/primitives/Graphics.js"></script>
<script src="../src/pixi/renderers/canvas/CanvasGraphics.js"></script>
<script src="../src/pixi/renderers/canvas/CanvasRenderer.js"></script>
<script src="../src/pixi/renderers/webgl/WebGLBatch.js"></script>
<script src="../src/pixi/renderers/webgl/WebGLGraphics.js"></script>
<script src="../src/pixi/renderers/webgl/WebGLRenderer.js"></script>
<script src="../src/pixi/renderers/webgl/WebGLRenderGroup.js"></script>
<script src="../src/pixi/renderers/webgl/WebGLShaders.js"></script>
<script src="../src/pixi/text/BitmapText.js"></script>
<script src="../src/pixi/text/Text.js"></script>
<script src="../src/pixi/textures/BaseTexture.js"></script>
<script src="../src/pixi/textures/Texture.js"></script>
<script src="../src/pixi/textures/RenderTexture.js"></script>
<script src="../src/pixi/utils/Detector.js"></script>
<script src="../src/pixi/utils/EventTarget.js"></script>
<script src="../src/pixi/utils/Polyk.js"></script>
<script src="../src/pixi/utils/Utils.js"></script>
<script src="../src/core/State.js"></script>
<script src="../src/system/RequestAnimationFrame.js"></script>
<script src="../src/system/Device.js"></script>
<script src="../src/core/SignalBinding.js"></script>
<script src="../src/core/Signal.js"></script>
<script src="../src/core/PluginManager.js"></script>
<script src="../src/core/Plugin.js"></script>
<script src="../src/math/RandomDataGenerator.js"></script>
<script src="../src/math/Math.js"></script>
<script src="../src/geom/Point.js"></script>
<script src="../src/geom/Circle.js"></script>
<script src="../src/net/Net.js"></script>
<script src="../src/tween/TweenManager.js"></script>
<script src="../src/tween/Tween.js"></script>
<script src="../src/tween/Easing.js"></script>
<script src="../src/time/Time.js"></script>
<script src="../src/animation/Animation.js"></script>
<script src="../src/animation/Frame.js"></script>
<script src="../src/animation/FrameData.js"></script>
<script src="../src/animation/Parser.js"></script>
<script src="../src/loader/Cache.js"></script>
<script src="../src/loader/Loader.js"></script>
<script src="../src/Game.js"></script>
+133 -77
View File
@@ -13,25 +13,28 @@
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
*/
Phaser.Game = function (callbackContext, parent, width, height, preloadCallback, createCallback, updateCallback, renderCallback, destroyCallback) {
/**
* Game constructor
*
* Instantiate a new <code>Phaser.Game</code> object.
*
* @constructor
* @param width {number} The width of your game in game pixels.
* @param height {number} The height of your game in game pixels.
* @param renderer {number} Which renderer to use (canvas or webgl)
* @param parent {string} ID of its parent DOM element.
*/
Phaser.Game = function (width, height, renderer, parent) {
if (typeof parent === "undefined") { parent = ''; }
if (typeof width === "undefined") { width = 800; }
if (typeof height === "undefined") { height = 600; }
if (typeof preloadCallback === "undefined") { preloadCallback = null; }
if (typeof createCallback === "undefined") { createCallback = null; }
if (typeof updateCallback === "undefined") { updateCallback = null; }
if (typeof renderCallback === "undefined") { renderCallback = null; }
if (typeof destroyCallback === "undefined") { destroyCallback = null; }
if (typeof renderer === "undefined") { renderer = Phaser.RENDERER_AUTO; }
if (typeof parent === "undefined") { parent = ''; }
this.id = Phaser.GAMES.push(this) - 1;
this.renderer = renderer;
this.callbackContext = callbackContext;
this.onPreloadCallback = preloadCallback;
this.onCreateCallback = createCallback;
this.onUpdateCallback = updateCallback;
this.onRenderCallback = renderCallback;
this.onDestroyCallback = destroyCallback;
//var _this = this;
if (document.readyState === 'complete' || document.readyState === 'interactive')
{
@@ -43,6 +46,8 @@ Phaser.Game = function (callbackContext, parent, width, height, preloadCallback,
window.addEventListener('load', Phaser.GAMES[this.id].boot(parent, width, height), false);
}
return this;
};
Phaser.Game.prototype = {
@@ -53,6 +58,12 @@ Phaser.Game.prototype = {
*/
id: 0,
/**
* The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL
* @type {number}
*/
renderer: 0,
/**
* Whether load complete loading or not.
* @type {bool}
@@ -244,13 +255,18 @@ Phaser.Game.prototype = {
return;
}
console.log('Phaser.Game.Boot');
console.log(this);
var _this = this;
if (!document.body) {
setTimeout(Phaser.GAMES[this.id].boot(parent, width, height), 13);
setTimeout(Phaser.GAMES[_this.id].boot(parent, width, height), 13);
}
else
{
document.removeEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot);
window.removeEventListener('load', Phaser.GAMES[this.id].boot);
document.removeEventListener('DOMContentLoaded', Phaser.GAMES[_this.id].boot);
window.removeEventListener('load', Phaser.GAMES[_this.id].boot);
this.onPause = new Phaser.Signal();
this.onResume = new Phaser.Signal();
@@ -270,49 +286,99 @@ Phaser.Game.prototype = {
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
// this.physics = new Phaser.Physics.PhysicsManager(this);
this.plugins = new Phaser.PluginManager(this, this);
this.load.onLoadComplete.add(this.loadComplete, this);
// this.setRenderer(Phaser.Types.RENDERER_CANVAS);
// this.world.boot();
// this.stage.boot();
// this.input.boot();
console.log('Phaser', Phaser.VERSION, 'initialized');
this.isBooted = true;
this.isRunning = true;
this._loadComplete = false;
if (this.onPreloadCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null) {
console.warn("Phaser update loop cannot start: No preload, create, update or render functions given and no pending State found");
}
else
{
console.log('Phaser', Phaser.VERSION, 'alive');
this.isRunning = true;
this._loadComplete = false;
this.raf = new Phaser.RequestAnimationFrame(this);
this.raf.start();
if (this._pendingState)
{
this.switchState(this._pendingState, false, false);
}
else
{
this.startState();
}
}
this.raf = new Phaser.RequestAnimationFrame(this);
this.raf.start();
if (this.onPreloadCallback || this.onCreateCallback || this.onUpdateCallback || this.onRenderCallback || this._pendingState)
{
if (this._pendingState)
{
console.log('boot has found a pending state');
this.switchState(this._pendingState, false, false);
}
else
{
console.log('boot has found enough callbacks to start the state');
this.startState();
}
}
}
},
/**
* Launch the game
* @param callbackContext Which context will the callbacks be called with.
* @param preloadCallback {function} Preload callback invoked when init default screen.
* @param createCallback {function} Create callback invoked when create default screen.
* @param updateCallback {function} Update callback invoked when update default screen.
* @param renderCallback {function} Render callback invoked when render default screen.
*/
launch: function (context, preload, create, update, render) {
this.callbackContext = context;
this.onPreloadCallback = preload || null;
this.onCreateCallback = create || null;
this.onUpdateCallback = update || null;
this.onRenderCallback = render || null;
if (this.onPreloadCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null)
{
console.warn("Phaser cannot start: No preload, create, update or render functions given and no pending State found");
}
else
{
if (this.isBooted)
{
console.log('launch has set the callbacks and dom is booted so lets rock');
this.startState();
// if (this._pendingState)
// {
// this.switchState(this._pendingState, false, false);
// }
// else
// {
// this.startState();
// }
}
else
{
console.log('launch has set the callbacks but cant start because the DOM isnt booted yet');
}
}
},
/**
* Called when the load has finished after preload was run.
*/
loadComplete: function () {
console.log('loadComplete', this);
this._loadComplete = true;
this.onCreateCallback.call(this.callbackContext);
if (this.onCreateCallback) {
this.onCreateCallback.call(this.callbackContext);
// this.onCreateCallback.call(this);
}
},
@@ -321,10 +387,15 @@ Phaser.Game.prototype = {
*/
startState: function () {
console.log('startState');
// console.log(this);
// console.log(this.callbackContext);
if (this.onPreloadCallback !== null)
{
this.load.reset();
this.onPreloadCallback.call(this.callbackContext);
// this.onPreloadCallback.call(this.onPreloadCallback);
// Is the loader empty?
if (this.load.queueSize == 0)
@@ -332,10 +403,10 @@ Phaser.Game.prototype = {
if (this.onCreateCallback !== null)
{
this.onCreateCallback.call(this.callbackContext);
// this.onCreateCallback.call(this.onCreateCallback);
}
this._loadComplete = true;
}
else
{
@@ -349,36 +420,12 @@ Phaser.Game.prototype = {
// No init? Then there was nothing to load either
if (this.onCreateCallback !== null) {
this.onCreateCallback.call(this.callbackContext);
// this.onCreateCallback.call(this.onCreateCallback);
}
this._loadComplete = true;
}
},
/**
* Set the most common state callbacks (init, create, update, render).
* @param preloadCallback {function} Init callback invoked when init state.
* @param createCallback {function} Create callback invoked when create state.
* @param updateCallback {function} Update callback invoked when update state.
* @param renderCallback {function} Render callback invoked when render state.
* @param destroyCallback {function} Destroy callback invoked when state is destroyed.
*/
setCallbacks: function (preloadCallback, createCallback, updateCallback, renderCallback, destroyCallback) {
if (typeof preloadCallback === "undefined") { preloadCallback = null; }
if (typeof createCallback === "undefined") { createCallback = null; }
if (typeof updateCallback === "undefined") { updateCallback = null; }
if (typeof renderCallback === "undefined") { renderCallback = null; }
if (typeof destroyCallback === "undefined") { destroyCallback = null; }
this.onPreloadCallback = preloadCallback;
this.onCreateCallback = createCallback;
this.onUpdateCallback = updateCallback;
this.onRenderCallback = renderCallback;
this.onDestroyCallback = destroyCallback;
},
update: function (time) {
@@ -388,11 +435,11 @@ Phaser.Game.prototype = {
this.plugins.preUpdate();
this.tweens.update();
this.input.update();
this.stage.update();
this.sound.update();
this.physics.update();
this.world.update();
// this.input.update();
// this.stage.update();
// this.sound.update();
// this.physics.update();
// this.world.update();
this.plugins.update();
@@ -403,7 +450,7 @@ Phaser.Game.prototype = {
this.onUpdateCallback.call(this.callbackContext);
}
this.world.postUpdate();
// this.world.postUpdate();
this.plugins.postUpdate();
this.plugins.preRender();
@@ -412,7 +459,7 @@ Phaser.Game.prototype = {
this.onPreRenderCallback.call(this.callbackContext);
}
this.renderer.render();
// this.renderer.render();
this.plugins.render();
if (this.onRenderCallback)
@@ -430,10 +477,10 @@ Phaser.Game.prototype = {
this.onLoadUpdateCallback.call(this.callbackContext);
}
this.world.postUpdate();
// this.world.postUpdate();
this.plugins.postUpdate();
this.plugins.preRender();
this.renderer.render();
// this.renderer.render();
this.plugins.render();
if (this.onLoadRenderCallback)
@@ -454,9 +501,17 @@ Phaser.Game.prototype = {
*/
switchState: function (state, clearWorld, clearCache) {
// console.log('switchState', state, this.isBooted);
// console.log(typeof state);
// console.log(state instanceof Phaser.State);
if (typeof clearWorld === "undefined") { clearWorld = true; }
if (typeof clearCache === "undefined") { clearCache = false; }
if (state instanceof Phaser.State) {
state.link(this);
}
if (this.isBooted == false) {
this._pendingState = state;
return;
@@ -467,7 +522,9 @@ Phaser.Game.prototype = {
this.onDestroyCallback.call(this.callbackContext);
}
this.input.reset(true);
if (this.input) {
this.input.reset(true);
}
// Prototype?
if (typeof state === 'function')
@@ -507,7 +564,6 @@ Phaser.Game.prototype = {
this._loadComplete = false;
this.startState();
}
else
{
+9 -1
View File
@@ -23,4 +23,12 @@
/**
* @module Phaser
*/
var Phaser = Phaser || { VERSION: '1.0.0', GAMES: [] };
var Phaser = Phaser || {
VERSION: '1.0.0',
GAMES: [],
RENDERER_AUTO: 0,
RENDERER_CANVAS: 1,
RENDERER_WEBGL: 2
};
+1 -1
View File
@@ -20,7 +20,7 @@ Phaser.Plugin = function (game, parent) {
};
Phaser.Plugin.prototype = {,
Phaser.Plugin.prototype = {
/**
* Pre-update is called at the start of the update cycle, before any other updates have taken place.
+1 -1
View File
@@ -13,7 +13,7 @@ Phaser.PluginManager = function(game, parent) {
};
Phaser.PluginManager.prototype = {,
Phaser.PluginManager.prototype = {
/**
* Add a new Plugin to the PluginManager.
+73
View File
@@ -0,0 +1,73 @@
/**
* State
*
* This is a base State class which can be extended if you are creating your game with TypeScript.
* It provides quick access to common functions such as the camera, cache, input, match, sound and more.
*
* @package Phaser.State
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
*/
Phaser.State = function () {
};
Phaser.State.prototype = {
link: function (game) {
this.game = game;
// this.add = game.add;
// this.camera = game.camera;
this.cache = game.cache;
// this.input = game.input;
this.load = game.load;
this.math = game.math;
// this.sound = game.sound;
// this.stage = game.stage;
this.time = game.time;
this.tweens = game.tweens;
// this.world = game.world;
},
/**
* Override this method to add some load operations.
* If you need to use the loader, you may need to use them here.
*/
preload: function () {
},
/**
* This method is called after the game engine successfully switches states.
* Feel free to add any setup code here.(Do not load anything here, override init() instead)
*/
create: function () {
},
/**
* Put update logic here.
*/
update: function () {
},
/**
* Put render operations here.
*/
render: function () {
},
/**
* This method will be called when game paused.
*/
paused: function () {
},
/**
* This method will be called when the state is destroyed
*/
destroy: function () {
}
};
+22 -4
View File
@@ -26,6 +26,12 @@ Phaser.RequestAnimationFrame = function(game) {
Phaser.RequestAnimationFrame.prototype = {
/**
* The function called by the update
* @private
**/
_onLoop: null,
/**
* Starts the requestAnimatioFrame running or setTimeout if unavailable in browser
* @method start
@@ -34,15 +40,27 @@ Phaser.RequestAnimationFrame.prototype = {
this.isRunning = true;
var _this = this;
if (!window.requestAnimationFrame)
{
this._isSetTimeOut = true;
this._timeOutID = window.setTimeout(Phaser.GAMES[this.game.id].raf.updateSetTimeout, 0);
this._onLoop = function () {
return _this.updateSetTimeout();
};
this._timeOutID = window.setTimeout(this._onLoop, 0);
}
else
{
this._isSetTimeOut = false;
window.requestAnimationFrame(Phaser.GAMES[this.game.id].raf.updateRAF);
this._onLoop = function () {
return _this.updateRAF();
};
window.requestAnimationFrame(this._onLoop);
}
},
@@ -55,7 +73,7 @@ Phaser.RequestAnimationFrame.prototype = {
this.game.update(time);
window.requestAnimationFrame(Phaser.GAMES[this.game.id].raf.updateRAF);
window.requestAnimationFrame(this._onLoop);
},
@@ -67,7 +85,7 @@ Phaser.RequestAnimationFrame.prototype = {
this.game.update(Date.now());
this._timeOutID = window.setTimeout(Phaser.GAMES[this.game.id].raf.updateSetTimeout, this.game.time.timeToCall);
this._timeOutID = window.setTimeout(this._onLoop, this.game.time.timeToCall);
},
+6 -6
View File
@@ -98,21 +98,21 @@ Phaser.TweenManager.prototype = {
*/
update: function () {
if ( _tweens.length === 0 ) return false;
if ( this._tweens.length === 0 ) return false;
var i = 0, numTweens = _tweens.length;
var i = 0, numTweens = this._tweens.length;
while ( i < numTweens ) {
if ( _tweens[ i ].update( this.game.time.now ) ) {
if ( this._tweens[ i ].update( this.game.time.now ) ) {
i ++;
i++;
} else {
_tweens.splice( i, 1 );
this._tweens.splice( i, 1 );
numTweens --;
numTweens--;
}