Fixed Animation index 0 issue and hooked TilemapLayer to camera.

This commit is contained in:
photonstorm 2013-10-16 02:09:12 +01:00
parent fd5eeb9088
commit a97f271de7
15 changed files with 844 additions and 306 deletions

View file

@ -119,6 +119,10 @@ Version 1.0.7 (in progress in the dev branch)
* Added Sprite.destroy back in again and made it a lot more robust at cleaning up child objects.
* Added 'return this' to all the core Loader functions so you can chain load calls if you so wish.
* Group.alpha is now exposed publically and changes the Group container object (not the children directly, who can still have their own alpha values)
* Device.webGL uses new inspection code to accurately catch more webGL capable devices.
* Fixed an issue where creating an animation with just one frame with an index of zero would cause a UUID error (thanks SYNYST3R1)
* Fixed Rectangle.union (thanks andron77)
* Debug.renderSpriteBody updated to use a the new Sprite.Body.screenX/Y properties.

View file

@ -9,7 +9,7 @@
*
* Phaser - http://www.phaser.io
*
* v1.0.7 - Built at: Mon, 14 Oct 2013 15:29:30 +0100
* v1.0.7 - Built at: Tue, 15 Oct 2013 21:10:11 +0100
*
* By Richard Davey http://www.photonstorm.com @photonstorm
*
@ -12676,8 +12676,21 @@ Phaser.Keyboard.prototype = {
this._hotkeys[event.keyCode].processKeyUp(event);
}
this._keys[event.keyCode].isDown = false;
this._keys[event.keyCode].timeUp = this.game.time.now;
if (this._keys[event.keyCode])
{
this._keys[event.keyCode].isDown = false;
this._keys[event.keyCode].timeUp = this.game.time.now;
}
else
{
// Not used this key before, so register it
this._keys[event.keyCode] = {
isDown: false,
timeDown: this.game.time.now,
timeUp: this.game.time.now,
duration: 0
};
}
},
@ -22244,14 +22257,16 @@ Phaser.Rectangle.intersection = function (a, b, out) {
* @method Phaser.Rectangle.intersects
* @param {Phaser.Rectangle} a - The first Rectangle object.
* @param {Phaser.Rectangle} b - The second Rectangle object.
* @param {number} tolerance - A tolerance value to allow for an intersection test with padding, default to 0
* @return {boolean} A value of true if the specified object intersects with this Rectangle object; otherwise false.
*/
Phaser.Rectangle.intersects = function (a, b, tolerance) {
Phaser.Rectangle.intersects = function (a, b) {
tolerance = tolerance || 0;
return (a.x < b.right && b.x < a.right && a.y < b.bottom && b.y < a.bottom);
return !(a.x > b.right + tolerance || a.right < b.x - tolerance || a.y > b.bottom + tolerance || a.bottom < b.y - tolerance);
// return (a.x <= b.right && b.x <= a.right && a.y <= b.bottom && b.y <= a.bottom);
// return (a.left <= b.right && b.left <= a.right && a.top <= b.bottom && b.top <= a.bottom);
// return !(a.x > b.right + tolerance || a.right < b.x - tolerance || a.y > b.bottom + tolerance || a.bottom < b.y - tolerance);
};
@ -28982,17 +28997,14 @@ Phaser.Utils.Debug.prototype = {
return;
}
x = x || null;
y = y || null;
if (typeof x !== 'number') { x = 0; }
if (typeof y !== 'number') { y = 0; }
color = color || 'rgb(255,255,255)';
if (x && y)
{
this.currentX = x;
this.currentY = y;
this.currentColor = color;
}
this.currentX = x;
this.currentY = y;
this.currentColor = color;
this.currentAlpha = this.context.globalAlpha;
this.context.save();
@ -29009,6 +29021,7 @@ Phaser.Utils.Debug.prototype = {
*/
stop: function () {
this.context.restore();
this.context.globalAlpha = this.currentAlpha;
@ -29366,7 +29379,6 @@ Phaser.Utils.Debug.prototype = {
this.line('angle: ' + sprite.angle.toFixed(1) + ' rotation: ' + sprite.rotation.toFixed(1));
this.line('visible: ' + sprite.visible + ' in camera: ' + sprite.inCamera);
this.line('body x: ' + sprite.body.x.toFixed(1) + ' y: ' + sprite.body.y.toFixed(1));
this.stop();
// 0 = scaleX
// 1 = skewY
@ -29382,12 +29394,13 @@ Phaser.Utils.Debug.prototype = {
// this.line('ty: ' + sprite.worldTransform[5]);
// this.line('skew x: ' + sprite.worldTransform[3]);
// this.line('skew y: ' + sprite.worldTransform[1]);
// this.line('dx: ' + sprite.body.deltaX());
// this.line('dy: ' + sprite.body.deltaY());
this.line('deltaX: ' + sprite.body.deltaX());
this.line('deltaY: ' + sprite.body.deltaY());
// this.line('sdx: ' + sprite.deltaX());
// this.line('sdy: ' + sprite.deltaY());
// this.line('inCamera: ' + this.game.renderer.spriteRenderer.inCamera(this.game.camera, sprite));
this.stop();
},
@ -30741,10 +30754,28 @@ Phaser.Physics.Arcade.prototype = {
* @param object2 The second GameObject to separate
* @returns {boolean} Returns true if the objects were separated, otherwise false.
*/
separateTile: function (object, x, y, width, height, mass, collideLeft, collideRight, collideUp, collideDown, separateX, separateY) {
separateTile: function (body, tile) {
var separatedX = this.separateTileX(object.body, x, y, width, height, mass, collideLeft, collideRight, separateX);
var separatedY = this.separateTileY(object.body, x, y, width, height, mass, collideUp, collideDown, separateY);
var separatedX = this.separateTileX(body, tile, true);
var separatedY = this.separateTileY(body, tile, true);
/*
if (separatedX)
{
console.log('x overlap', this._overlap);
}
if (separatedY)
{
console.log('y overlap', this._overlap);
}
if (separatedX || separatedY)
{
return true;
}
*/
if (separatedX || separatedY)
{
@ -30761,59 +30792,53 @@ Phaser.Physics.Arcade.prototype = {
* @param tile The Tile to separate
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
*/
OLDseparateTileX: function (object, x, y, width, height, mass, collideLeft, collideRight, separate) {
separateTileX: function (body, tile, separate) {
// Can't separate two immovable objects (tiles are always immovable)
if (object.immovable)
if (body.immovable || body.deltaX() == 0 || Phaser.Rectangle.intersects(body.hullX, tile) == false)
{
return false;
}
// First, get the object delta
this._overlap = 0;
// console.log('separatedX', x, y, object.deltaX());
// The hulls overlap, let's process it
this._maxOverlap = body.deltaAbsX() + this.OVERLAP_BIAS;
if (object.deltaX() != 0)
console.log('sx hulls over');
console.log('x', body.hullX.x, 'y', body.hullX.y, 'bottom', body.hullX.y, 'right', body.hullX.right);
console.log(tile);
if (body.deltaX() < 0)
{
this._bounds1.setTo(object.x, object.y, object.width, object.height);
// Moving left
this._overlap = tile.right - body.hullX.x;
if ((this._bounds1.right > x) && (this._bounds1.x < x + width) && (this._bounds1.bottom > y) && (this._bounds1.y < y + height))
console.log('sx left', this._overlap);
if ((this._overlap > this._maxOverlap) || body.allowCollision.left == false || tile.tile.collideRight == false)
{
// The hulls overlap, let's process it
this._maxOverlap = object.deltaAbsX() + this.OVERLAP_BIAS;
this._overlap = 0;
}
else
{
body.touching.left = true;
}
}
else
{
// Moving right
this._overlap = body.hullX.right - tile.x;
// TODO - We need to check if we're already inside of the tile, i.e. jumping through an n-way tile
// in which case we didn't ought to separate because it'll look like tunneling
console.log('sx right', this._overlap);
if (object.deltaX() > 0)
{
// Going right ...
this._overlap = object.x + object.width - x;
if ((this._overlap > this._maxOverlap) || !object.allowCollision.right || !collideLeft)
{
this._overlap = 0;
}
else
{
object.touching.right = true;
}
}
else if (object.deltaX() < 0)
{
// Going left ...
this._overlap = object.x - width - x;
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.left || !collideRight)
{
this._overlap = 0;
}
else
{
object.touching.left = true;
}
}
if ((this._overlap > this._maxOverlap) || body.allowCollision.right == false || tile.tile.collideLeft == false)
{
this._overlap = 0;
}
else
{
body.touching.right = true;
}
}
@ -30822,21 +30847,37 @@ Phaser.Physics.Arcade.prototype = {
{
if (separate)
{
object.x = object.x - this._overlap;
if (object.bounce.x == 0)
if (body.deltaX() < 0)
{
object.velocity.x = 0;
console.log('sx sep left 1', body.x);
body.x = body.x + this._overlap;
console.log('sx sep left 2', body.x);
}
else
{
object.velocity.x = -object.velocity.x * object.bounce.x;
console.log('sx sep right 1', body.x);
body.x = body.x - this._overlap;
console.log('sx sep right 2', body.x);
}
if (body.bounce.x == 0)
{
body.velocity.x = 0;
}
else
{
body.velocity.x = -body.velocity.x * body.bounce.x;
}
body.updateHulls();
}
console.log('%c ', 'background: #7f7f7f')
return true;
}
else
{
console.log('%c ', 'background: #7f7f7f')
return false;
}
@ -30848,60 +30889,45 @@ Phaser.Physics.Arcade.prototype = {
* @param tile The Tile to separate
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
*/
OLDseparateTileY: function (object, x, y, width, height, mass, collideUp, collideDown, separate) {
separateTileY: function (body, tile, separate) {
// Can't separate two immovable objects (tiles are always immovable)
if (object.immovable)
if (body.immovable || body.deltaY() == 0 || Phaser.Rectangle.intersects(body.hullY, tile) == false)
{
return false;
}
// First, get the object delta
this._overlap = 0;
if (object.deltaY() != 0)
// The hulls overlap, let's process it
this._maxOverlap = body.deltaAbsY() + this.OVERLAP_BIAS;
if (body.deltaY() < 0)
{
this._bounds1.setTo(object.x, object.y, object.width, object.height);
// Moving up
this._overlap = tile.bottom - body.hullY.y;
if ((this._bounds1.right > x) && (this._bounds1.x < x + width) && (this._bounds1.bottom > y) && (this._bounds1.y < y + height))
if ((this._overlap > this._maxOverlap) || body.allowCollision.up == false || tile.tile.collideDown == false)
{
// The hulls overlap, let's process it
this._overlap = 0;
}
else
{
body.touching.up = true;
}
}
else
{
// Moving down
this._overlap = body.hullY.bottom - tile.y;
// Not currently used, may need it so keep for now
this._maxOverlap = object.deltaAbsY() + this.OVERLAP_BIAS;
// TODO - We need to check if we're already inside of the tile, i.e. jumping through an n-way tile
// in which case we didn't ought to separate because it'll look like tunneling
if (object.deltaY() > 0)
{
// Going down ...
this._overlap = object.bottom - y;
// if (object.allowCollision.down && collideDown && this._overlap < this._maxOverlap)
if ((this._overlap > this._maxOverlap) || !object.allowCollision.down || !collideDown)
{
this._overlap = 0;
}
else
{
object.touching.down = true;
}
}
else
{
// Going up ...
this._overlap = object.y - height - y;
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.up || !collideUp)
{
this._overlap = 0;
}
else
{
object.touching.up = true;
}
}
if ((this._overlap > this._maxOverlap) || body.allowCollision.down == false || tile.tile.collideUp == false)
{
this._overlap = 0;
}
else
{
body.touching.down = true;
}
}
@ -30910,17 +30936,27 @@ Phaser.Physics.Arcade.prototype = {
{
if (separate)
{
object.y = object.y - this._overlap;
if (object.bounce.y == 0)
if (body.deltaY() < 0)
{
object.velocity.y = 0;
body.y = body.y + this._overlap;
}
else
{
object.velocity.y = -object.velocity.y * object.bounce.y;
body.y = body.y - this._overlap;
}
if (body.bounce.y == 0)
{
body.velocity.y = 0;
}
else
{
body.velocity.y = -body.velocity.y * body.bounce.y;
}
body.updateHulls();
}
return true;
}
else
@ -30928,167 +30964,6 @@ Phaser.Physics.Arcade.prototype = {
return false;
}
},
/**
* Separates the two objects on their x axis
* @param object The GameObject to separate
* @param tile The Tile to separate
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
*/
separateTileX: function (object, x, y, width, height, mass, collideLeft, collideRight, separate) {
// Can't separate two immovable objects (tiles are always immovable)
if (object.immovable)
{
return false;
}
this._overlap = 0;
// Do we have any overlap at all?
if (Phaser.Rectangle.intersectsRaw(object, x, x + width, y, y + height))
{
this._maxOverlap = object.deltaAbsX() + this.OVERLAP_BIAS;
if (object.deltaX() == 0)
{
// Object is either stuck inside the tile or only overlapping on the Y axis
}
else if (object.deltaX() > 0)
{
// Going right ...
this._overlap = object.x + object.width - x;
if ((this._overlap > this._maxOverlap) || !object.allowCollision.right || !collideLeft)
{
this._overlap = 0;
}
else
{
object.touching.right = true;
}
}
else if (object.deltaX() < 0)
{
// Going left ...
this._overlap = object.x - width - x;
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.left || !collideRight)
{
this._overlap = 0;
}
else
{
object.touching.left = true;
}
}
if (this._overlap != 0)
{
if (separate)
{
// console.log('x over', this._overlap);
object.x = object.x - this._overlap;
if (object.bounce.x == 0)
{
object.velocity.x = 0;
}
else
{
object.velocity.x = -object.velocity.x * object.bounce.x;
}
}
return true;
}
}
return false;
},
/**
* Separates the two objects on their x axis
* @param object The GameObject to separate
* @param tile The Tile to separate
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
*/
separateTileY: function (object, x, y, width, height, mass, collideUp, collideDown, separate) {
// Can't separate two immovable objects (tiles are always immovable)
if (object.immovable)
{
return false;
}
this._overlap = 0;
if (Phaser.Rectangle.intersectsRaw(object, x, x + width, y, y + height))
{
this._maxOverlap = object.deltaAbsY() + this.OVERLAP_BIAS;
if (object.deltaY() == 0)
{
// Object is stuck inside a tile and not moving
}
else if (object.deltaY() > 0)
{
// Going down ...
this._overlap = object.bottom - y;
// if (object.allowCollision.down && collideDown && this._overlap < this._maxOverlap)
if ((this._overlap > this._maxOverlap) || !object.allowCollision.down || !collideDown)
{
this._overlap = 0;
}
else
{
object.touching.down = true;
}
}
else if (object.deltaY() < 0)
{
// Going up ...
this._overlap = object.y - height - y;
if ((-this._overlap > this._maxOverlap) || !object.allowCollision.up || !collideUp)
{
this._overlap = 0;
}
else
{
object.touching.up = true;
}
}
if (this._overlap != 0)
{
// console.log('y over', this._overlap);
if (separate)
{
object.y = object.y - this._overlap;
if (object.bounce.y == 0)
{
object.velocity.y = 0;
}
else
{
object.velocity.y = -object.velocity.y * object.bounce.y;
}
}
return true;
}
}
return false;
},
/**
@ -32641,10 +32516,36 @@ Phaser.Tile = function (tileset, index, x, y, width, height) {
*/
this.separateY = true;
/**
* @property {boolean} collisionCallback - Tilemap collision callback.
* @default
*/
this.collisionCallback = null;
/**
* @property {boolean} collisionCallback - Tilemap collision callback.
* @default
*/
this.collisionCallbackContext = this;
};
Phaser.Tile.prototype = {
/**
* Set callback to be called when this tilemap collides.
*
* @method Phaser.Tilemap.prototype.setCollisionCallback
* @param {Function} callback - Callback function.
* @param {object} context - Callback will be called with this context.
*/
setCollisionCallback: function (callback, context) {
this.collisionCallbackContext = context;
this.collisionCallback = callback;
},
/**
* Clean up memory.
* @method destroy
@ -32750,6 +32651,8 @@ Phaser.Tilemap = function (game, key) {
this.layers = [];
}
console.log(this.layers);
this.currentLayer = 0;
this.debugMap = [];
@ -32808,6 +32711,23 @@ Phaser.Tilemap.prototype = {
},
/**
* Get the tile located at specific position (in world coordinate) and layer (thus you give a position of a point which is within the tile).
* @param {number} x - X position of the point in target tile.
* @param {number} y - Y position of the point in target tile.
* @param {number} [layer] - layer of this tile located.
* @return {Tile} The tile with specific properties.
*/
getTileFromWorldXY: function (x, y, layer) {
if (typeof layer === "undefined") { layer = this.currentLayer; }
// return this.tiles[this.layers[layer].getTileFromWorldXY(x, y)];
},
/**
* Set a specific tile with its x and y in tiles.
* @method putTile
@ -32824,6 +32744,38 @@ Phaser.Tilemap.prototype = {
},
/**
* Set a specific tile with its x and y in tiles.
* @method putTileWorldXY
* @param {number} x - X position of this tile in world coordinates.
* @param {number} y - Y position of this tile in world coordinates.
* @param {number} index - The index of this tile type in the core map data.
*/
putTileWorldXY: function (x, y, index) {
x = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
y = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
if (x >= 0 && x < this.layers[this.currentLayer].width && y >= 0 && y < this.layers[this.currentLayer].height)
{
this.layers[this.currentLayer].data[y][x] = index;
}
},
// swapTile
// fillTile
// randomiseTiles
// replaceTiles
removeAllLayers: function () {
this.layers.length = 0;
this.currentLayer = 0;
},
dump: function () {
var txt = '';
@ -32858,6 +32810,13 @@ Phaser.Tilemap.prototype = {
args[0] = txt;
console.log.apply(console, args);
},
destroy: function () {
this.removeAllLayers();
this.game = null;
}
};
@ -32970,6 +32929,11 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset,
* @private
*/
this._ty = 0;
this._results = [];
this._tw = 0;
this._th = 0;
/**
* @property {number} _tl - Local render loop var to help avoid gc spikes.
@ -33008,9 +32972,11 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset,
this._y = 0;
this._prevX = 0;
this._prevY = 0;
this.dirty = true;
if (typeof tileset === 'string')
if (tileset instanceof Phaser.Tileset || typeof tileset === 'string')
{
this.updateTileset(tileset);
}
@ -33026,19 +32992,120 @@ Phaser.TilemapLayer.prototype = {
updateTileset: function (tileset) {
this.tileset = this.game.cache.getTileset(tileset);
if (tileset instanceof Phaser.Tileset)
{
this.tileset = tileset;
}
else if (typeof tileset === 'string')
{
this.tileset = this.game.cache.getTileset('tiles');
}
else
{
return;
}
this.tileWidth = this.tileset.tileWidth;
this.tileHeight = this.tileset.tileHeight;
this.updateMax();
},
updateMapData: function (tilemap, layerID) {
updateMapData: function (tilemap, layer) {
this.tilemap = tilemap;
this.layer = this.tilemap.layers[layerID];
if (typeof layer === 'undefined')
{
layer = 0;
}
this.updateMax();
if (tilemap instanceof Phaser.Tilemap)
{
this.tilemap = tilemap;
this.layer = this.tilemap.layers[layer];
this.updateMax();
}
},
/**
*
* @method getTileOverlaps
* @param {GameObject} object - Tiles you want to get that overlaps this.
* @return {array} Array with tiles informations (each contains x, y, and the tile).
*/
getTiles: function (x, y, width, height, collides) {
if (this.tilemap === null)
{
return;
}
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
if (typeof collides === 'undefined') { collides = false; }
// Cap the values
if (x < 0)
{
x = 0;
}
if (y < 0)
{
y = 0;
}
if (width > this.widthInPixels)
{
width = this.widthInPixels;
}
if (height > this.heightInPixels)
{
height = this.heightInPixels;
}
var tileWidth = this.tileWidth * this.sprite.scale.x;
var tileHeight = this.tileHeight * this.sprite.scale.y;
// Convert the pixel values into tile coordinates
this._tx = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
this._ty = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
this._tw = (this.game.math.snapToCeil(width, tileWidth) + tileWidth) / tileWidth;
this._th = (this.game.math.snapToCeil(height, tileHeight) + tileHeight) / tileHeight;
this._results.length = 0;
this._results.push( { x: x, y: y, width: width, height: height, tx: this._tx, ty: this._ty, tw: this._tw, th: this._th });
var _index = 0;
var _tile = null;
var sx = 0;
var sy = 0;
for (var wy = this._ty; wy < this._ty + this._th; wy++)
{
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
{
if (this.layer.data[wy] && this.layer.data[wy][wx])
{
// Could combine
_index = this.layer.data[wy][wx] - 1;
_tile = this.tileset.getTile(_index);
sx = _tile.width * this.sprite.scale.x;
sy = _tile.height * this.sprite.scale.y;
if (collides == false || (collides && _tile.collideNone == false))
{
// this._results.push({ x: wx * _tile.width, right: (wx * _tile.width) + _tile.width, y: wy * _tile.height, bottom: (wy * _tile.height) + _tile.height, width: _tile.width, height: _tile.height, tx: wx, ty: wy, tile: _tile });
this._results.push({ x: wx * sx, right: (wx * sx) + sx, y: wy * sy, bottom: (wy * sy) + sy, width: sx, height: sy, tx: wx, ty: wy, tile: _tile });
}
}
}
}
return this._results;
},
@ -33444,11 +33511,43 @@ Phaser.Tileset.prototype = {
},
canCollide: function (index) {
if (this.tiles[index])
{
return this.tiles[index].collideNone;
}
return null;
},
checkTileIndex: function (index) {
return (this.tiles[index]);
}
},
setCollisionRange: function (start, stop, left, right, up, down) {
if (this.tiles[start] && this.tiles[stop] && start < stop)
{
for (var i = start; i <= stop; i++)
{
this.tiles[i].setCollision(left, right, up, down);
}
}
},
setCollision: function (index, left, right, up, down) {
if (this.tiles[index])
{
this.tiles[index].setCollision(left, right, up, down);
}
},
}

View file

@ -28,7 +28,7 @@
// And this starts the animation playing by using its key ("walk")
// 30 is the frame rate (30fps)
// true means it will loop when it finishes
mummy.animations.play('walk', 30, true);
mummy.animations.play('walk', 20, true);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 402 B

After

Width:  |  Height:  |  Size: 366 B

View file

@ -11,7 +11,7 @@
function preload() {
game.load.tilemap('level1', 'assets/games/starstruck/tiles-1.png', 'assets/games/starstruck/level1.json', null, Phaser.Tilemap.JSON);
// game.load.tilemap('level1', 'assets/games/starstruck/tiles-1.png', 'assets/games/starstruck/level1.json', null, Phaser.Tilemap.JSON);
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
game.load.spritesheet('droid', 'assets/games/starstruck/droid.png', 32, 32);
game.load.image('starSmall', 'assets/games/starstruck/star.png');

View file

@ -52,7 +52,7 @@
// This one gives tileset as a string, the other an object
// layer = new Phaser.TilemapLayer(game, 0, 0, 640, 400, 'tiles', map, 0);
layer = new Phaser.TilemapLayer(game, 0, 0, 640, 400, tileset, map, 0);
layer = new Phaser.TilemapLayer(game, 0, 0, 800, 400, tileset, map, 0);
// To set tiles for collision you need to modify the Tileset, which is a property of the layer

201
examples/tilemaps/wip3.php Normal file
View file

@ -0,0 +1,201 @@
<?php
$title = "Tilemap Layer WIP #1";
require('../head.php');
?>
<script type="text/javascript">
// var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level3', 'assets/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/cybernoid.png', 16, 16);
// game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.image('phaser', 'assets/sprites/space-baddie.png');
}
var map;
var tileset;
var layer;
var cursors;
var overlap;
var sprite;
function create() {
game.stage.backgroundColor = '#3d3d3d';
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = new Phaser.Tilemap(game, 'level3');
// A Tileset is a single image containing a strip of tiles. Each tile is broken down into its own Phaser.Tile object on import.
// You can set properties on the Tile objects, such as collision, n-way movement and meta data.
// A Tilemap uses a Tileset to render. The indexes in the map corresponding to the Tileset indexes.
// This way multiple levels can share the same single Tileset without requiring one each.
tileset = game.cache.getTileset('tiles');
// Basically this sets EVERY SINGLE tile to fully collide on all faces
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
// And this turns off collision on the only tile we don't want collision on :)
tileset.setCollision(6, false, false, false, false);
tileset.setCollision(34, false, false, false, false);
tileset.setCollision(35, false, false, false, false);
// A TilemapLayer consists of an x,y coordinate (position), a width and height, a Tileset and a Tilemap which it uses for map data.
// The x/y coordinates are in World space and you can place the tilemap layer anywhere in the world.
// The width/height is the rendered size of the layer in pixels, not the size of the map data itself.
// This one gives tileset as a string, the other an object
// layer = new Phaser.TilemapLayer(game, 0, 0, 800, 400, tileset, map, 0);
layer = new Phaser.TilemapLayer(game, 0, 0, 400, 200, tileset, map, 0);
layer.sprite.scale.setTo(2, 2);
// layer.sprite.anchor.setTo(0.5, 0.5);
game.world.add(layer.sprite);
// This is a bit nuts, ought to find a way to automate it, but it looks cool :)
map.debugMap = [ '#000000',
'#e40058', '#e40058', '#e40058', '#80d010', '#bcbcbc', '#e40058', '#000000', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc', '#e40058', '#e40058', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#80d010', '#80d010', '#80d010', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#80d010', '#0070ec',
'#0070ec', '#24188c', '#24188c', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#80d010', '#80d010', '#80d010', '#e40058',
'#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#80d010', '#bcbcbc', '#80d010', '#000000', '#80d010',
'#80d010', '#80d010', '#bcbcbc', '#e40058', '#80d010', '#80d010', '#e40058', '#e40058', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc'
];
// map.dump();
// layer.sprite.scale.setTo(2, 2);
// Works a treat :)
// game.add.sprite(320, 0, layer.texture, layer.frame);
// game.add.sprite(0, 200, layer.texture, layer.frame);
// game.add.sprite(320, 200, layer.texture, layer.frame);
// game.world.setBounds(0, 0, 2000, 2000);
// game.camera.x = 400;
sprite = game.add.sprite(200, 80, 'phaser');
// sprite.x = 140;
// sprite.y = 40;
//sprite.scale.setTo(2, 2);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.velocity.setTo(0, 0);
// layer.sprite.angle += 0.5;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -100;
// layer.y -= 4;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 100;
// layer.y += 4;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -100;
// layer.x -= 4;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 100;
// layer.x += 4;
}
// getTiles: function (x, y, width, height, collides, layer) {
overlap = layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
if (overlap.length > 1)
{
// console.log('%c ', 'background: #000000')
for (var i = 1; i < overlap.length; i++)
{
game.physics.separateTile(sprite.body, overlap[i]);
}
}
}
function render() {
layer.render();
game.debug.renderSpriteBody(sprite);
game.debug.renderSpriteInfo(sprite, 32, 450);
// game.debug.renderCameraInfo(game.camera, 32, 32);
/*
game.context.save();
game.context.setTransform(1, 0, 0, 1, 0, 0);
game.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
if (overlap.length > 1)
{
var x = 0;
var y = 0;
for (var i = 1; i < overlap.length; i++)
{
game.context.drawImage(
overlap[i].tile.tileset.image,
overlap[i].tile.x,
overlap[i].tile.y,
overlap[i].tile.width,
overlap[i].tile.height,
0 + (x * overlap[i].tile.width),
420 + (y * overlap[i].tile.height),
overlap[i].tile.width,
overlap[i].tile.height
);
if (overlap[i].tile.collideNone == false)
{
game.context.fillRect(0 + (x * overlap[i].tile.width), 420 + (y * overlap[i].tile.height), overlap[i].tile.width, overlap[i].tile.height);
}
x++;
if (x == overlap[0].tw)
{
x = 0;
y++;
}
}
}
game.context.restore();
*/
// game.debug.renderRectangle(sprite.body.hullX);
}
</script>
<?php
require('../foot.php');
?>

210
examples/tilemaps/wip4.php Normal file
View file

@ -0,0 +1,210 @@
<?php
$title = "Tilemap Layer WIP #1";
require('../head.php');
?>
<script type="text/javascript">
// var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level3', 'assets/maps/cybernoid.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/maps/cybernoid.png', 16, 16);
game.load.image('phaser', 'assets/sprites/phaser-ship.png');
}
var map;
var tileset;
var layer;
var cursors;
var overlap;
var sprite;
function create() {
game.stage.backgroundColor = '#3d3d3d';
// A Tilemap object just holds the data needed to describe the map (i.e. the json exported from Tiled, or the CSV exported from elsewhere).
// You can add your own data or manipulate the data (swap tiles around, etc) but in order to display it you need to create a TilemapLayer.
map = new Phaser.Tilemap(game, 'level3');
// A Tileset is a single image containing a strip of tiles. Each tile is broken down into its own Phaser.Tile object on import.
// You can set properties on the Tile objects, such as collision, n-way movement and meta data.
// A Tilemap uses a Tileset to render. The indexes in the map corresponding to the Tileset indexes.
// This way multiple levels can share the same single Tileset without requiring one each.
tileset = game.cache.getTileset('tiles');
// Basically this sets EVERY SINGLE tile to fully collide on all faces
tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);
// And this turns off collision on the only tile we don't want collision on :)
tileset.setCollision(6, false, false, false, false);
tileset.setCollision(31, false, false, false, false);
tileset.setCollision(34, false, false, false, false);
tileset.setCollision(35, false, false, false, false);
tileset.setCollision(46, false, false, false, false);
// A TilemapLayer consists of an x,y coordinate (position), a width and height, a Tileset and a Tilemap which it uses for map data.
// The x/y coordinates are in World space and you can place the tilemap layer anywhere in the world.
// The width/height is the rendered size of the layer in pixels, not the size of the map data itself.
// This one gives tileset as a string, the other an object
layer = new Phaser.TilemapLayer(game, 0, 0, 800, 600, tileset, map, 0);
// layer = new Phaser.TilemapLayer(game, 0, 0, 400, 200, tileset, map, 0);
// layer.sprite.scale.setTo(2, 2);
// layer.sprite.anchor.setTo(0.5, 0.5);
layer.resizeWorld();
game.world.add(layer.sprite);
// This is a bit nuts, ought to find a way to automate it, but it looks cool :)
map.debugMap = [ '#000000',
'#e40058', '#e40058', '#e40058', '#80d010', '#bcbcbc', '#e40058', '#000000', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc', '#e40058', '#e40058', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#80d010', '#80d010', '#80d010', '#0070ec', '#0070ec', '#80d010', '#80d010', '#80d010', '#80d010', '#0070ec',
'#0070ec', '#24188c', '#24188c', '#80d010', '#80d010', '#80d010', '#bcbcbc', '#80d010', '#80d010', '#80d010', '#e40058',
'#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#e40058', '#bcbcbc', '#80d010', '#bcbcbc', '#80d010', '#000000', '#80d010',
'#80d010', '#80d010', '#bcbcbc', '#e40058', '#80d010', '#80d010', '#e40058', '#e40058', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#0070ec', '#0070ec', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc', '#bcbcbc',
'#bcbcbc', '#bcbcbc'
];
// map.dump();
// layer.sprite.scale.setTo(2, 2);
// Works a treat :)
// game.add.sprite(320, 0, layer.texture, layer.frame);
// game.add.sprite(0, 200, layer.texture, layer.frame);
// game.add.sprite(320, 200, layer.texture, layer.frame);
// game.world.setBounds(0, 0, 2000, 2000);
// game.camera.x = 400;
sprite = game.add.sprite(450, 80, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
// sprite.x = 140;
// sprite.y = 40;
//sprite.scale.setTo(2, 2);
// sprite.body.gravity.y = 100;
// sprite.body.bounce.x = 0.5;
// sprite.body.bounce.y = 0.2;
game.camera.follow(sprite);
game.camera.deadzone = new Phaser.Rectangle(160, 160, layer.renderWidth-320, layer.renderHeight-320);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
layer.update();
// getTiles: function (x, y, width, height, collides, layer) {
overlap = layer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
if (overlap.length > 1)
{
for (var i = 1; i < overlap.length; i++)
{
game.physics.separateTile(sprite.body, overlap[i]);
}
}
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
if (cursors.up.isDown)
{
sprite.body.velocity.y = -150;
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 150;
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -150;
sprite.scale.x = -1;
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 150;
sprite.scale.x = 1;
}
}
function render() {
layer.render();
// game.debug.renderSpriteBody(sprite);
// game.debug.renderSpriteInfo(sprite, 32, 450);
// game.debug.renderCameraInfo(game.camera, 32, 32);
/*
game.context.save();
game.context.setTransform(1, 0, 0, 1, 0, 0);
game.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
if (overlap.length > 1)
{
var x = 0;
var y = 0;
for (var i = 1; i < overlap.length; i++)
{
game.context.drawImage(
overlap[i].tile.tileset.image,
overlap[i].tile.x,
overlap[i].tile.y,
overlap[i].tile.width,
overlap[i].tile.height,
0 + (x * overlap[i].tile.width),
420 + (y * overlap[i].tile.height),
overlap[i].tile.width,
overlap[i].tile.height
);
if (overlap[i].tile.collideNone == false)
{
game.context.fillRect(0 + (x * overlap[i].tile.width), 420 + (y * overlap[i].tile.height), overlap[i].tile.width, overlap[i].tile.height);
}
x++;
if (x == overlap[0].tw)
{
x = 0;
y++;
}
}
}
game.context.restore();
*/
// game.debug.renderRectangle(game.camera.deadzone, 'rgba(0,200,0,0.5)');
}
</script>
<?php
require('../foot.php');
?>

View file

@ -101,7 +101,7 @@ Phaser.AnimationManager.prototype = {
// If they didn't set the useNumericIndex then let's at least try and guess it
if (typeof useNumericIndex === 'undefined')
{
if (frames && frames[0] && typeof frames[0] === 'number')
if (frames && typeof frames[0] === 'number')
{
useNumericIndex = true;
}

View file

@ -60,7 +60,7 @@ Phaser.FrameData.prototype = {
*/
getFrame: function (index) {
if (this._frames[index])
if (this._frames.length > index)
{
return this._frames[index];
}

View file

@ -661,6 +661,6 @@ Phaser.Rectangle.union = function (a, b, out) {
if (typeof out === "undefined") { out = new Phaser.Rectangle(); }
return out.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right), Math.max(a.bottom, b.bottom));
return out.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right) - Math.min(a.left, b.left), Math.max(a.bottom, b.bottom) - Math.min(a.top, b.top));
};

View file

@ -10,6 +10,8 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.preX = sprite.x;
this.preY = sprite.y;
this.preRotation = sprite.angle;
this.screenX = sprite.x;
this.screenY = sprite.y;
// un-scaled original size
this.sourceWidth = sprite.currentFrame.sourceSizeW;
@ -108,8 +110,10 @@ Phaser.Physics.Arcade.Body.prototype = {
this.embedded = false;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = this.preX;
@ -233,8 +237,10 @@ Phaser.Physics.Arcade.Body.prototype = {
this.angularVelocity = 0;
this.angularAcceleration = 0;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = this.preX;

View file

@ -323,8 +323,7 @@ Phaser.Device.prototype = {
this.file = !!window['File'] && !!window['FileReader'] && !!window['FileList'] && !!window['Blob'];
this.fileSystem = !!window['requestFileSystem'];
this.webGL = ( function () { try { return !! window.WebGLRenderingContext && !! document.createElement( 'canvas' ).getContext( 'experimental-webgl' ); } catch( e ) { return false; } } )();
// this.webGL = !!window['WebGLRenderingContext'];
this.webGL = ( function () { try { var canvas = document.createElement( 'canvas' ); return !! window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ); } catch( e ) { return false; } } )();
this.worker = !!window['Worker'];
if ('ontouchstart' in document.documentElement || window.navigator.msPointerEnabled) {

View file

@ -37,6 +37,7 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset,
* @default
*/
this.sprite = new Phaser.Sprite(this.game, x, y, this.texture, this.frame);
this.sprite.fixedToCamera = true;
/**
@ -167,6 +168,21 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset,
Phaser.TilemapLayer.prototype = {
update: function () {
this.x = this.game.camera.x;
this.y = this.game.camera.y;
},
resizeWorld: function () {
this.game.world.setBounds(0, 0, this.widthInPixels, this.heightInPixels);
console.log('world', this.game.world.bounds);
},
updateTileset: function (tileset) {
if (tileset instanceof Phaser.Tileset)
@ -242,11 +258,14 @@ Phaser.TilemapLayer.prototype = {
height = this.heightInPixels;
}
var tileWidth = this.tileWidth * this.sprite.scale.x;
var tileHeight = this.tileHeight * this.sprite.scale.y;
// Convert the pixel values into tile coordinates
this._tx = this.game.math.snapToFloor(x, this.tileWidth) / this.tileWidth;
this._ty = this.game.math.snapToFloor(y, this.tileHeight) / this.tileHeight;
this._tw = (this.game.math.snapToCeil(width, this.tileWidth) + this.tileWidth) / this.tileWidth;
this._th = (this.game.math.snapToCeil(height, this.tileHeight) + this.tileHeight) / this.tileHeight;
this._tx = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
this._ty = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
this._tw = (this.game.math.snapToCeil(width, tileWidth) + tileWidth) / tileWidth;
this._th = (this.game.math.snapToCeil(height, tileHeight) + tileHeight) / tileHeight;
this._results.length = 0;
@ -254,6 +273,8 @@ Phaser.TilemapLayer.prototype = {
var _index = 0;
var _tile = null;
var sx = 0;
var sy = 0;
for (var wy = this._ty; wy < this._ty + this._th; wy++)
{
@ -265,9 +286,13 @@ Phaser.TilemapLayer.prototype = {
_index = this.layer.data[wy][wx] - 1;
_tile = this.tileset.getTile(_index);
sx = _tile.width * this.sprite.scale.x;
sy = _tile.height * this.sprite.scale.y;
if (collides == false || (collides && _tile.collideNone == false))
{
this._results.push({ x: wx * _tile.width, right: (wx * _tile.width) + _tile.width, y: wy * _tile.height, bottom: (wy * _tile.height) + _tile.height, width: _tile.width, height: _tile.height, tx: wx, ty: wy, tile: _tile });
// this._results.push({ x: wx * _tile.width, right: (wx * _tile.width) + _tile.width, y: wy * _tile.height, bottom: (wy * _tile.height) + _tile.height, width: _tile.width, height: _tile.height, tx: wx, ty: wy, tile: _tile });
this._results.push({ x: wx * sx, right: (wx * sx) + sx, y: wy * sy, bottom: (wy * sy) + sy, width: sx, height: sy, tx: wx, ty: wy, tile: _tile });
}
}
}
@ -318,11 +343,6 @@ Phaser.TilemapLayer.prototype = {
this._tx = this._dx;
this._ty = this._dy;
// First let's just copy over the whole canvas, offset by the scroll difference
// Then we only need fill in the missing strip/s (could be top/bottom/left/right I guess)
// ScrollZone code might be useful here.
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (var y = this._startY; y < this._startY + this._maxY; y++)

View file

@ -626,8 +626,7 @@ Phaser.Utils.Debug.prototype = {
this.start(0, 0, color);
this.context.fillStyle = color;
// this.context.fillRect(sprite.body.x - sprite.body.deltaX(), sprite.body.y - sprite.body.deltaY(), sprite.body.width, sprite.body.height);
this.context.fillRect(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height);
this.context.fillRect(sprite.body.screenX, sprite.body.screenY, sprite.body.width, sprite.body.height);
this.stop();