diff --git a/examples/assets/bd/burd.png b/examples/assets/bd/burd.png new file mode 100644 index 00000000..f469cf8e Binary files /dev/null and b/examples/assets/bd/burd.png differ diff --git a/examples/consoleBanner3.php b/examples/consoleBanner3.php new file mode 100644 index 00000000..19a63786 --- /dev/null +++ b/examples/consoleBanner3.php @@ -0,0 +1,81 @@ + + + + phaser.js - a new beginning + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/examples/group1.php b/examples/group1.php index 49acff28..a4aa7710 100644 --- a/examples/group1.php +++ b/examples/group1.php @@ -16,6 +16,7 @@ function preload() { game.load.image('diamond', 'assets/sprites/diamond.png'); + game.load.image('carrot', 'assets/sprites/carrot.png'); } var g; @@ -24,26 +25,51 @@ function create() { - g = new Phaser.Group(game, 'aliens'); + t = game.add.sprite(100, 100, 'carrot'); + t.name = 'c0'; + t.body.bounce.y = Math.random(); + t.body.collideWorldBounds = true; + + g = game.add.group(); + // g.x = 400; + // g.y = 300; + // g._container.anchor.x = 0.5; + // g._container.anchor.y = 0.5; for (var i = 0; i < 10; i++) { - s = g.createSprite(100 + i * 64, 300, 'diamond'); - s.name = 'diamond' + i; - s.body.collideWorldBounds = true; - s.body.bounce.y = Math.random(); + var x = (i * 64); + s = g.create(x, 0, 'diamond'); + s.name = 'd' + i; + s.anchor.setTo(0.5, 0.5); } - g.getRandom().y += 200; + // g.forEach(setAlpha, this); - //g.setAll('body.velocity.y', 250); + // g.dump(); + + // g.replace(s, t); + + // g.dump(); + + // g.callAll('dump', game, 123, 456, 789); + + // g.getRandom().y += 200; + + // g.setAll('body.velocity.y', 250); // g.divideAll('y', 2); // g.multiplyAll('y', 3); } + function setAlpha (sprite) { + sprite.alpha = 0.4; + } + function update() { + // g.addAll('angle', 10); + g.angle++; } diff --git a/examples/invaders.php b/examples/invaders.php new file mode 100644 index 00000000..c393a804 --- /dev/null +++ b/examples/invaders.php @@ -0,0 +1,62 @@ + + + + phaser.js - a new beginning + + + + + + + + \ No newline at end of file diff --git a/src/core/Game.js b/src/core/Game.js index ffd728b7..80692e57 100644 --- a/src/core/Game.js +++ b/src/core/Game.js @@ -378,7 +378,6 @@ Phaser.Game.prototype = { if (!this.paused) { this.plugins.preUpdate(); - // this.world.preUpdate(); this.physics.preUpdate(); this.input.update(); @@ -391,9 +390,6 @@ Phaser.Game.prototype = { this.renderer.render(this.world._stage); this.state.render(); - // this.world.postUpdate(); - // this.physics.postUpdate(); - this.plugins.postRender(); } diff --git a/src/core/Group.js b/src/core/Group.js index b605b0e4..89673ab2 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -1,14 +1,21 @@ -Phaser.Group = function (game, name) { +Phaser.Group = function (game, parent, name) { + + parent = parent || null; this.game = game; - this.name = name || ''; + this.name = name || 'group'; this._container = new PIXI.DisplayObjectContainer(); - // Swap for proper access to stage - this.game.world._stage.addChild(this._container); + if (parent) + { + parent.addChild(this._container); + } + else + { + this.game.world.add(this._container); + } - this.active = true; this.exists = true; /** @@ -44,13 +51,13 @@ Phaser.Group.prototype = { }, - getChildAt: function (index) { + getAt: function (index) { return this._container.getChildAt(index); }, - createSprite: function (x, y, key, frame) { + create: function (x, y, key, frame) { var child = new Phaser.Sprite(this.game, x, y, key, frame); child.group = this; @@ -151,7 +158,7 @@ Phaser.Group.prototype = { bringToTop: function (child) { - if (!child === this._container.last) + if (child !== this._container.last) { this.swap(child, this._container.last); } @@ -160,7 +167,32 @@ Phaser.Group.prototype = { }, - replace: function (newChild, oldChild) { + getIndex: function (child) { + + return this._container.children.indexOf(child); + + }, + + replace: function (oldChild, newChild) { + + if (!this._container.first._iNext) + { + return; + } + + var index = this.getIndex(oldChild); + + if (index != -1) + { + if (newChild.parent != undefined) + { + newChild.parent.removeChild(newChild); + } + + this._container.removeChild(oldChild); + this._container.addChildAt(newChild, index); + } + }, /** @@ -269,18 +301,21 @@ Phaser.Group.prototype = { checkVisible = checkVisible || false; operation = operation || 0; - var currentNode = this._container.first._iNext; - - do + if (this._container.first._iNext) { - if ((checkAlive == false || (checkAlive && currentNode.alive)) && (checkVisible == false || (checkVisible && currentNode.visible))) + var currentNode = this._container.first._iNext; + + do { - this.setProperty(currentNode, key, value, operation); - } + if ((checkAlive == false || (checkAlive && currentNode.alive)) && (checkVisible == false || (checkVisible && currentNode.visible))) + { + this.setProperty(currentNode, key, value, operation); + } - currentNode = currentNode._iNext; + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext) } - while (currentNode != this._container.last._iNext) }, @@ -308,16 +343,125 @@ Phaser.Group.prototype = { }, - callAll: function () { + /** + * Calls a function on all of the active children (children with exists=true). + * You must pass the context in which the callback is applied. + * After the context you can add as many parameters as you like, which will all be passed to the child. + */ + callAll: function (callback, callbackContext) { + + var args = Array.prototype.splice.call(arguments, 2); + + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (currentNode.exists && currentNode[callback]) + { + currentNode[callback].apply(currentNode, args); + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext) + + } + }, - forEach: function () { + forEach: function (callback, callbackContext, checkExists) { + + checkExists = checkExists || false; + + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (checkExists == false || (checkExists && currentNode.exists)) + { + callback.call(callbackContext, currentNode); + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); + + } + }, forEachAlive: function () { + + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (currentNode.alive) + { + callback.call(callbackContext, currentNode); + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); + + } + }, forEachDead: function () { + + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (currentNode.alive == false) + { + callback.call(callbackContext, currentNode); + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); + + } + }, + + /** + * Call this function to retrieve the first object with exists == (the given state) in the group. + * + * @return {Any} The first child, or null if none found. + */ + getFirstExists: function (state) { + + state = state || true; + + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (currentNode.exists == state) + { + return currentNode; + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); + } + + return null; + }, /** @@ -328,18 +472,21 @@ Phaser.Group.prototype = { */ getFirstAlive: function () { - var currentNode = this._container.first._iNext; - - do + if (this._container.first._iNext) { - if (currentNode.alive) + var currentNode = this._container.first._iNext; + + do { - return currentNode; - } + if (currentNode.alive) + { + return currentNode; + } - currentNode = currentNode._iNext; + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); } - while (currentNode != this._container.last._iNext) return null; @@ -353,18 +500,21 @@ Phaser.Group.prototype = { */ getFirstDead: function () { - var currentNode = this._container.first._iNext; - - do + if (this._container.first._iNext) { - if (!currentNode.alive) + var currentNode = this._container.first._iNext; + + do { - return currentNode; - } + if (!currentNode.alive) + { + return currentNode; + } - currentNode = currentNode._iNext; + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); } - while (currentNode != this._container.last._iNext) return null; @@ -378,18 +528,22 @@ Phaser.Group.prototype = { countLiving: function () { var total = -1; - var currentNode = this._container.first._iNext; - - do - { - if (currentNode.alive) - { - total++; - } - currentNode = currentNode._iNext; + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (currentNode.alive) + { + total++; + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); } - while (currentNode != this._container.last._iNext); return total; @@ -403,18 +557,22 @@ Phaser.Group.prototype = { countDead: function () { var total = -1; - var currentNode = this._container.first._iNext; - - do - { - if (!currentNode.alive) - { - total++; - } - currentNode = currentNode._iNext; + if (this._container.first._iNext) + { + var currentNode = this._container.first._iNext; + + do + { + if (!currentNode.alive) + { + total++; + } + + currentNode = currentNode._iNext; + } + while (currentNode != this._container.last._iNext); } - while (currentNode != this._container.last._iNext); return total; @@ -438,9 +596,115 @@ Phaser.Group.prototype = { }, remove: function (child) { + + this._container.removeChild(child); + + }, + + removeAll: function () { + + do + { + this._container.removeChild(this._container.children[0]); + } + while (this._container.children.length > 0); + + }, + + removeBetween: function (startIndex, endIndex) { + + if (startIndex > endIndex || startIndex < 0 || endIndex > this._container.children.length) + { + return false; + } + + for (var i = startIndex; i < endIndex; i++) + { + var child = this._container.children[i]; + this._container.removeChild(child); + } + }, destroy: function () { + + this.removeAll(); + + this._container.parent.removeChild(this._container); + + this._container = null; + + this.game = null; + + this.exists = false; + + }, + + dump: function () { + + console.log("\nNode\t\t|\t\tNext\t\t|\t\tPrev\t\t|\t\tFirst\t\t|\t\tLast"); + console.log("\t\t\t|\t\t\t\t\t|\t\t\t\t\t|\t\t\t\t\t|"); + + var displayObject = this._container; + + var testObject = displayObject.last._iNext; + displayObject = displayObject.first; + + do + { + var name = displayObject.name || '*'; + var nameNext = '-'; + var namePrev = '-'; + var nameFirst = '-'; + var nameLast = '-'; + + if (displayObject._iNext) + { + nameNext = displayObject._iNext.name; + } + + if (displayObject._iPrev) + { + namePrev = displayObject._iPrev.name; + } + + if (displayObject.first) + { + nameFirst = displayObject.first.name; + } + + if (displayObject.last) + { + nameLast = displayObject.last.name; + } + + if (typeof nameNext === 'undefined') + { + nameNext = '-'; + } + + if (typeof namePrev === 'undefined') + { + namePrev = '-'; + } + + if (typeof nameFirst === 'undefined') + { + nameFirst = '-'; + } + + if (typeof nameLast === 'undefined') + { + nameLast = '-'; + } + + console.log(name + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast); + + displayObject = displayObject._iNext; + + } + while(displayObject != testObject) + } }; @@ -481,7 +745,7 @@ Object.defineProperty(Phaser.Group.prototype, "angle", { set: function(value) { this._container.rotation = Phaser.Math.degToRad(value); - } + }, enumerable: true, configurable: true diff --git a/src/core/World.js b/src/core/World.js index 3c0bfb75..c0ccbf59 100644 --- a/src/core/World.js +++ b/src/core/World.js @@ -5,11 +5,6 @@ Phaser.World = function (game) { this._stage = new PIXI.Stage(0x000000); this._stage.name = '_stage_root'; - this._container = new PIXI.DisplayObjectContainer(); - // this._container.name = '_stage_root'; - - this._stage.addChild(this._container); - this.bounds = new Phaser.Rectangle(0, 0, game.width, game.height); }; @@ -17,7 +12,7 @@ Phaser.World = function (game) { Phaser.World.prototype = { _stage: null, - _container: null, + _stage: null, _length: 0, bounds: null, @@ -31,66 +26,51 @@ Phaser.World.prototype = { }, add: function (gameobject) { - this._container.addChild(gameobject); + + this._stage.addChild(gameobject); return gameobject; + }, addAt: function (gameobject, index) { - this._container.addChildAt(gameobject, index); + + this._stage.addChildAt(gameobject, index); return gameobject; + }, getAt: function (index) { - return this._container.getChildAt(index); + + return this._stage.getChildAt(index); + }, remove: function (gameobject) { - this._container.removeChild(gameobject); + + this._stage.removeChild(gameobject); return gameobject; + }, update: function () { this.camera.update(); - // TODO - sort this out, the nodes are wrong - var displayObject = this._stage; - - var testObject = displayObject.last._iNext; - displayObject = displayObject.first; - - do + if (this._stage.first._iNext) { - if (displayObject['update']) - { - displayObject.update(); - } + var currentNode = this._stage.first._iNext; - // count++ - displayObject = displayObject._iNext; - } - while(displayObject != testObject) - - }, - - postUpdate: function () { - - var displayObject = this._stage; - - var testObject = displayObject.last._iNext; - displayObject = displayObject.first; - - do - { - if (displayObject['postUpdate']) + do { - displayObject.postUpdate(); + if (currentNode['update']) + { + currentNode.update(); + } + + currentNode = currentNode._iNext; } - - // count++ - displayObject = displayObject._iNext; + while (currentNode != this._stage.last._iNext) } - while(displayObject != testObject) }, diff --git a/src/gameobjects/GameObjectFactory.js b/src/gameobjects/GameObjectFactory.js index bcf77608..b6fa6eba 100644 --- a/src/gameobjects/GameObjectFactory.js +++ b/src/gameobjects/GameObjectFactory.js @@ -26,7 +26,7 @@ Phaser.GameObjectFactory.prototype = { }, /** - * Create a new Sprite with specific position and sprite sheet key. + * Create a new Sprite with specific position and sprite sheet key that will automatically be added as a child of the given parent. * * @param x {number} X position of the new sprite. * @param y {number} Y position of the new sprite. @@ -55,6 +55,12 @@ Phaser.GameObjectFactory.prototype = { }, + group: function (parent, name) { + + return new Phaser.Group(this.game, parent, name); + + }, + audio: function (key, volume, loop) { return this.game.sound.add(key, volume, loop); diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 495e15ce..92953252 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -7,9 +7,13 @@ Phaser.Sprite = function (game, x, y, key, frame) { this.game = game; + // If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all this.exists = true; - this.active = true; + + // An "invisible" sprite isn't rendered at all this.visible = true; + + // This is a handy little var your game can use to determine if a sprite is alive or not, it doesn't effect rendering this.alive = true; this.group = null; @@ -144,6 +148,11 @@ Phaser.Sprite.prototype.constructor = Phaser.Sprite; */ Phaser.Sprite.prototype.update = function() { + if (!this.exists) + { + return; + } + this._cache.dirty = false; if (this.animations.update()) diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index c26802ed..4fb4ecab 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -160,36 +160,6 @@ Phaser.Physics.Arcade.Body.prototype = { }, - postUpdate: function () { - - /* - // The State update may (almost certainly?) will had - - if (this.collideWorldBounds) - { - // Adjust sprite directly? - this.checkWorldBounds(); - } - - console.log('pre pu', this.x, this.y); - - // if (this.moves) - // { - this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width); - this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height); - // } - - console.log('post pu', this.sprite.x, this.sprite.y); - - if (this.allowRotation) - { - this.sprite.angle = this.rotation; - } - */ - - - }, - setSize: function (width, height, offsetX, offsetY) { offsetX = offsetX || this.offset.x; @@ -207,60 +177,6 @@ Phaser.Physics.Arcade.Body.prototype = { }, - /* - hullWidth: function () { - - if (this.deltaX() > 0) - { - return this.width + this.deltaX(); - } - else - { - return this.width - this.deltaX(); - } - - }, - - hullHeight: function () { - - if (this.deltaY() > 0) - { - return this.height + this.deltaY(); - } - else - { - return this.height - this.deltaY(); - } - - }, - - hullX: function () { - - if (this.x < this.lastX) - { - return this.x; - } - else - { - return this.lastX; - } - - }, - - hullY: function () { - - if (this.y < this.lastY) - { - return this.y; - } - else - { - return this.lastY; - } - - }, - */ - deltaAbsX: function () { return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX()); }, diff --git a/src/tween/Tween.js b/src/tween/Tween.js index 06b8fc54..7102ca8d 100644 --- a/src/tween/Tween.js +++ b/src/tween/Tween.js @@ -311,15 +311,20 @@ Phaser.Tween.prototype = { this._startTime = time + this._delayTime; + this.onComplete.dispatch(this._object); + + if ( this._onCompleteCallback !== null ) { + this._onCompleteCallback.call( this._object ); + } + return true; } else { + this.onComplete.dispatch(this._object); + if ( this._onCompleteCallback !== null ) { - - this.onComplete.dispatch(this._object); this._onCompleteCallback.call( this._object ); - } for ( var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i ++ ) {