From 7c6e6df91aa58c93d37d62ac6b45b33cbbf89de6 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 13 Sep 2013 05:44:04 +0100 Subject: [PATCH] State swap example done and working. --- examples/state/Game.js | 40 +++++++++++++ examples/state/MainMenu.js | 30 ++++++++++ examples/state/Preloader.js | 28 +++++++++ examples/state/index.php | 30 ++++++++++ examples/state/js.php | 109 ++++++++++++++++++++++++++++++++++++ examples/text1.php | 2 +- src/core/Group.js | 10 +++- src/core/LinkedList.js | 7 ++- src/core/State.js | 6 +- src/core/StateManager.js | 6 +- src/core/World.js | 16 +++++- src/gameobjects/Text.js | 7 ++- src/input/Input.js | 2 +- 13 files changed, 282 insertions(+), 11 deletions(-) create mode 100644 examples/state/Game.js create mode 100644 examples/state/MainMenu.js create mode 100644 examples/state/Preloader.js create mode 100644 examples/state/index.php create mode 100644 examples/state/js.php diff --git a/examples/state/Game.js b/examples/state/Game.js new file mode 100644 index 00000000..0863e177 --- /dev/null +++ b/examples/state/Game.js @@ -0,0 +1,40 @@ +TestGame.Game = function (game) { + + this.game = game; + +}; + +TestGame.Game.prototype = { + + preload: function () { + + this.game.load.spritesheet('balls', '../assets/sprites/balls.png', 17, 17); + + }, + + create: function () { + + this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'cougar').anchor.setTo(0.5, 0.5); + + p = this.game.add.emitter(100, 100, 250); + p.makeParticles('balls', [0,1,2,3,4,5]); + p.minParticleSpeed.setTo(-100, -100); + p.maxParticleSpeed.setTo(100, -200); + p.gravity = 10; + p.start(false, 3000, 10); + + this.game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Quadratic.InOut, true, 0, 1000, true); + + this.game.input.onDown.add(this.quitToMenu, this); + + }, + + quitToMenu: function () { + + console.log('lets quit! back to the main menu'); + + this.game.state.start('mainmenu'); + + } + +} diff --git a/examples/state/MainMenu.js b/examples/state/MainMenu.js new file mode 100644 index 00000000..ea15dcf0 --- /dev/null +++ b/examples/state/MainMenu.js @@ -0,0 +1,30 @@ +TestGame.MainMenu = function (game) { + + // Our main menu + this.game = game; + +}; + +TestGame.MainMenu.prototype = { + + create: function () { + + var bg = this.game.add.sprite(0, 100, 'nocooper'); + bg.scale.setTo(2.5, 2.5); + + var t = this.game.add.sprite(100, 600, 'touhou'); + t.anchor.setTo(0, 1); + + button = this.game.add.button(this.game.world.centerX, 400, 'button', this.startGame, this, 2, 1, 0); + button.anchor.setTo(0.5, 0.5); + + }, + + startGame: function () { + + console.log('lets play'); + this.game.state.start('game'); + + } + +} diff --git a/examples/state/Preloader.js b/examples/state/Preloader.js new file mode 100644 index 00000000..d5c3d659 --- /dev/null +++ b/examples/state/Preloader.js @@ -0,0 +1,28 @@ +var TestGame = {}; + +TestGame.Preloader = function (game) { + + this.game = game; + +}; + +TestGame.Preloader.prototype = { + + preload: function () { + + this.game.load.image('nocooper', '../assets/pics/1984-nocooper-space.png'); + this.game.load.image('touhou', '../assets/pics/aya_touhou_teng_soldier.png'); + this.game.load.image('cougar', '../assets/pics/cougar_ihsf.png'); + this.game.load.spritesheet('button', '../assets/buttons/button_sprite_sheet.png', 193, 71); + + }, + + create: function () { + + console.log('Preloade finished, lets go to the main menu automatically'); + + this.game.state.start('mainmenu'); + + } + +} diff --git a/examples/state/index.php b/examples/state/index.php new file mode 100644 index 00000000..c02ea83b --- /dev/null +++ b/examples/state/index.php @@ -0,0 +1,30 @@ + + + + phaser.js - a new beginning + + + + + + + + + + + \ No newline at end of file diff --git a/examples/state/js.php b/examples/state/js.php new file mode 100644 index 00000000..009e2327 --- /dev/null +++ b/examples/state/js.php @@ -0,0 +1,109 @@ + +?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/text1.php b/examples/text1.php index 748f306b..4ccbb80d 100644 --- a/examples/text1.php +++ b/examples/text1.php @@ -12,7 +12,7 @@ (function () { - var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { create: create }); + var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { create: create }); function create() { diff --git a/src/core/Group.js b/src/core/Group.js index 84428158..e54dae95 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -686,9 +686,17 @@ Phaser.Group.prototype = { removeAll: function () { + if (this._container.children.length == 0) + { + return; + } + do { - this._container.children[0].events.onRemovedFromGroup.dispatch(this._container.children[0], this); + if (this._container.children[0].events) + { + this._container.children[0].events.onRemovedFromGroup.dispatch(this._container.children[0], this); + } this._container.removeChild(this._container.children[0]); } while (this._container.children.length > 0); diff --git a/src/core/LinkedList.js b/src/core/LinkedList.js index 2e5cdb5e..1cf214ce 100644 --- a/src/core/LinkedList.js +++ b/src/core/LinkedList.js @@ -73,11 +73,16 @@ Phaser.LinkedList.prototype = { callAll: function (callback) { + if (!this.first || !this.last) + { + return; + } + var entity = this.first; do { - if (entity[callback]) + if (entity && entity[callback]) { entity[callback].call(entity); } diff --git a/src/core/State.js b/src/core/State.js index 49cb0d12..93a75477 100644 --- a/src/core/State.js +++ b/src/core/State.js @@ -1,7 +1,7 @@ /** * State * -* This is a base State class which can be extended if you are creating your game with TypeScript. +* This is a base State class which can be extended if you are creating your own game. * It provides quick access to common functions such as the camera, cache, input, match, sound and more. * * @package Phaser.State @@ -18,7 +18,7 @@ Phaser.State = function () { this.cache = null; this.input = null; this.load = null; - // this.math = null; + this.math = null; this.sound = null; this.stage = null; this.time = null; @@ -39,7 +39,7 @@ Phaser.State.prototype = { this.cache = game.cache; this.input = game.input; this.load = game.load; - // this.math = game.math; + this.math = game.math; this.sound = game.sound; this.stage = game.stage; this.time = game.time; diff --git a/src/core/StateManager.js b/src/core/StateManager.js index c7a61391..fc653d30 100644 --- a/src/core/StateManager.js +++ b/src/core/StateManager.js @@ -151,6 +151,7 @@ Phaser.StateManager.prototype = { { // console.log('Phaser.StateManager.addState: Object given'); newState = state; + newState.game = this.game; } else if (typeof state === 'function') { @@ -237,9 +238,10 @@ Phaser.StateManager.prototype = { if (clearWorld) { - //this.game.world.destroy(); + this.game.world.destroy(); - if (clearCache == true) { + if (clearCache == true) + { this.game.cache.destroy(); } } diff --git a/src/core/World.js b/src/core/World.js index 353be125..92c153e0 100644 --- a/src/core/World.js +++ b/src/core/World.js @@ -107,7 +107,21 @@ Phaser.World.prototype = { this.bounds.height = height; } - } + }, + + /** + * Destroyer of worlds. + */ + destroy: function () { + + this.camera.x = 0; + this.camera.y = 0; + + this.game.input.reset(true); + + this.group.removeAll(); + + } }; diff --git a/src/gameobjects/Text.js b/src/gameobjects/Text.js index 56ec6549..110fcf8c 100644 --- a/src/gameobjects/Text.js +++ b/src/gameobjects/Text.js @@ -5,6 +5,9 @@ Phaser.Text = function (game, x, y, text, style) { text = text || ''; style = style || ''; + PIXI.Text.call(this, text, style); + + /* this.canvas = document.createElement("canvas"); this.context = this.canvas.getContext("2d"); @@ -21,10 +24,12 @@ Phaser.Text = function (game, x, y, text, style) { this.updateText(); this.dirty = false; + */ }; -Phaser.Text.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Text.prototype); +// Phaser.Text.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Text.prototype); +Phaser.Text.prototype = Phaser.Utils.extend(true, PIXI.Text.prototype); Phaser.Text.prototype.constructor = Phaser.Text; // Add our own custom methods diff --git a/src/input/Input.js b/src/input/Input.js index 14bc5c5e..3bce70e8 100644 --- a/src/input/Input.js +++ b/src/input/Input.js @@ -378,7 +378,7 @@ Phaser.Input.prototype = { **/ reset: function (hard) { - hard = hard || false; + if (typeof hard == 'undefined') { hard = false; } this.keyboard.reset(); this.mousePointer.reset();