Group nearly done. Sprite.anchor appears to be broken though, must fix.

This commit is contained in:
Richard Davey
2013-09-06 20:20:58 +01:00
parent 822a2df289
commit eb7af3d2a2
11 changed files with 544 additions and 199 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 675 B

+81
View File
@@ -0,0 +1,81 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<textarea id="output" style="width: 1400px; height: 600px"></textarea>
<br />
<input type="button" id="grab" value="Grab" />
<input type="button" id="refresh" value="Refresh" />
<script type="text/javascript">
var game = new Phaser.Game(50, 31, Phaser.CANVAS, '', { preload: preload, create: create });
function preload() {
game.load.image('piccie', 'assets/bd/burd.png');
}
var logo;
var args;
function create() {
logo = game.add.sprite(0, 0, 'piccie');
document.getElementById('grab').onclick = canvasToConsole;
document.getElementById('refresh').onclick = refreshConsole;
}
function refreshConsole () {
args[0] = document.getElementById('output').value;
console.clear();
console.log.apply(console, args);
console.log(args.toString());
}
function canvasToConsole () {
var txt = "";
var prev;
args = [""];
var lfb = game.renderer.context.getImageData(0, 0, game.width, game.height).data;
for (var i = 0, j = 0; i < 1500; i++, j++)
{
if (!(i % 50)) // if(i && !(i % 50)) if don't like to start with a "\n"
txt += prev = "\n";
var col = "background: rgb(" + [lfb[j++], lfb[j++], lfb[j++]] + ")";
if (col == prev)
{
txt += " ";
}
else
{
txt += "%c ";
args.push(col);
prev = col;
}
}
args[0] = txt;
document.getElementById('output').innerText = args[0];
console.clear();
console.log.apply(console, args);
// console.log(args[0]);
}
</script>
</body>
</html>
+33 -7
View File
@@ -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++;
}
+62
View File
@@ -0,0 +1,62 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('alien', 'assets/sprites/space-baddie.png');
game.load.image('ship', 'assets/sprites/shmup-ship.png');
}
var player;
var aliens;
function create() {
player = game.add.sprite(400, 500, 'ship');
// player.anchor.setTo(0.5, 0.5);
aliens = game.add.group();
for (var y = 0; y < 4; y++)
{
for (var x = 0; x < 10; x++)
{
aliens.create(x * 48, y * 64, 'alien');
}
}
aliens.x = 100;
var tween = game.add.tween(aliens).to({x: 200}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
tween.onComplete.add(descend, this);
}
function descend() {
aliens.y += 10;
}
function update() {
}
function render() {
}
})();
</script>
</body>
</html>
-4
View File
@@ -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();
}
+320 -56
View File
@@ -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
+23 -43
View File
@@ -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)
},
+7 -1
View File
@@ -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);
+10 -1
View File
@@ -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())
-84
View File
@@ -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());
},
+8 -3
View File
@@ -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 ++ ) {