Updated Loader so it no longer checks if the animation data is valid, passes that control to the AnimationLoader. Also fixed camera bounds check in Sprite.

This commit is contained in:
Richard Davey
2013-05-23 15:45:04 +01:00
parent c7485301ca
commit 94add2ea6e
9 changed files with 637 additions and 266 deletions
+17 -45
View File
@@ -166,32 +166,23 @@ module Phaser {
atlasData = JSON.parse(atlasData);
}
// Malformed?
if (atlasData['frames'])
{
this._queueSize++;
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData['frames'], format: format, error: false, loaded: false };
this._keys.push(key);
}
else
{
throw new Error("Phaser.Loader. Invalid Texture Atlas JSON given, missing frames block");
}
this._queueSize++;
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData['frames'], format: format, error: false, loaded: false };
this._keys.push(key);
}
else if (format == Loader.TEXTURE_ATLAS_XML_STARLING)
{
// An xml string or object has been given
if (typeof atlasData === 'string')
{
var tmp;
var xml;
try
{
if (window['DOMParser'])
{
tmp = new DOMParser();
xml = tmp.parseFromString(atlasData, "text/xml");
var domparser = new DOMParser();
xml = domparser.parseFromString(atlasData, "text/xml");
}
else
{
@@ -215,17 +206,9 @@ module Phaser {
}
}
// Malformed?
if (atlasData.getElementsByTagName('TextureAtlas'))
{
this._queueSize++;
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData, format: format, error: false, loaded: false };
this._keys.push(key);
}
else
{
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
}
this._queueSize++;
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData, format: format, error: false, loaded: false };
this._keys.push(key);
}
}
@@ -449,13 +432,9 @@ module Phaser {
private jsonLoadComplete(key: string) {
var data = JSON.parse(this._xhr.response);
var file = this._fileList[key];
// Malformed?
if (data['frames'])
{
var file = this._fileList[key];
this._game.cache.addTextureAtlas(file.key, file.url, file.data, data['frames'], file.format);
}
this._game.cache.addTextureAtlas(file.key, file.url, file.data, data['frames'], file.format);
this.nextFile(key, true);
@@ -468,23 +447,24 @@ module Phaser {
private dataLoadError(key: string) {
var file = this._fileList[key];
file.error = true;
this.nextFile(key, true);
}
private xmlLoadComplete(key: string) {
var atlasData = this._xhr.response; // xml?
var tmp;
var atlasData = this._xhr.response;
var xml;
try
{
if (window['DOMParser'])
{
tmp = new DOMParser();
xml = tmp.parseFromString(atlasData, "text/xml");
var domparser = new DOMParser();
xml = domparser.parseFromString(atlasData, "text/xml");
}
else
{
@@ -503,16 +483,8 @@ module Phaser {
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given");
}
// Malformed?
if (xml.getElementsByTagName('TextureAtlas'))
{
var file = this._fileList[key];
this._game.cache.addTextureAtlas(file.key, file.url, file.data, xml, file.format);
}
else
{
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
}
var file = this._fileList[key];
this._game.cache.addTextureAtlas(file.key, file.url, file.data, xml, file.format);
this.nextFile(key, true);
+12 -24
View File
@@ -185,7 +185,7 @@ module Phaser {
* @param camera {Rectangle} The rectangle you want to check.
* @return {boolean} Return true if bounds of this sprite intersects the given rectangle, otherwise return false.
*/
public inCamera(camera: Rectangle): bool {
public inCamera(camera: Rectangle, cameraOffsetX: number, cameraOffsetY: number): bool {
// Object fixed in place regardless of the camera scrolling? Then it's always visible
if (this.scrollFactor.x == 0 && this.scrollFactor.y == 0)
@@ -193,21 +193,12 @@ module Phaser {
return true;
}
// Otherwise, if it's scrolling perfectly in sync with the camera (1 to 1) then it's a simple bounds check on world coordinates
if (this.scrollFactor.x == 1 && this.scrollFactor.y == 1)
{
return camera.intersects(this.frameBounds, this.frameBounds.length);
}
else
{
// Else apply the offsets
this._dx = (this.frameBounds.x - camera.x) * this.scrollFactor.x;
this._dy = (this.frameBounds.y - camera.y) * this.scrollFactor.y;
this._dw = this.frameBounds.width * this.scale.x;
this._dh = this.frameBounds.height * this.scale.y;
this._dx = (this.frameBounds.x - camera.x);
this._dy = (this.frameBounds.y - camera.y);
this._dw = this.frameBounds.width * this.scale.x;
this._dh = this.frameBounds.height * this.scale.y;
return (camera.right > this._dx) && (camera.x < this._dx + this._dw) && (camera.bottom > this._dy) && (camera.y < this._dy + this._dh);
}
return (camera.right > this._dx) && (camera.x < this._dx + this._dw) && (camera.bottom > this._dy) && (camera.y < this._dy + this._dh);
}
@@ -248,7 +239,7 @@ module Phaser {
public render(camera: Camera, cameraOffsetX: number, cameraOffsetY: number): bool {
// Render checks
if (this.visible == false || this.scale.x == 0 || this.scale.y == 0 || this.alpha < 0.1 || this.cameraBlacklist.indexOf(camera.ID) !== -1 || this.inCamera(camera.worldView) == false)
if (this.visible == false || this.scale.x == 0 || this.scale.y == 0 || this.alpha < 0.1 || this.cameraBlacklist.indexOf(camera.ID) !== -1 || this.inCamera(camera.worldView, cameraOffsetX, cameraOffsetY) == false)
{
return false;
}
@@ -400,8 +391,8 @@ module Phaser {
if (this.renderDebug)
{
//this.renderBounds(camera, cameraOffsetX, cameraOffsetY);
this.collisionMask.render(camera, cameraOffsetX, cameraOffsetY);
this.renderBounds(camera, cameraOffsetX, cameraOffsetY);
//this.collisionMask.render(camera, cameraOffsetX, cameraOffsetY);
}
if (globalAlpha > -1)
@@ -421,14 +412,11 @@ module Phaser {
*/
private renderBounds(camera:Camera, cameraOffsetX:number, cameraOffsetY:number) {
//this._dx = cameraOffsetX + (this.frameBounds.topLeft.x - camera.worldView.x);
//this._dy = cameraOffsetY + (this.frameBounds.topLeft.y - camera.worldView.y);
this._dx = cameraOffsetX + (this.collisionMask.x - camera.worldView.x);
this._dy = cameraOffsetY + (this.collisionMask.y - camera.worldView.y);
this._dx = cameraOffsetX + (this.frameBounds.topLeft.x - camera.worldView.x);
this._dy = cameraOffsetY + (this.frameBounds.topLeft.y - camera.worldView.y);
this.context.fillStyle = this.renderDebugColor;
this.context.fillRect(this._dx, this._dy, this.collisionMask.width, this.collisionMask.height);
this.context.fillRect(this._dx, this._dy, this.frameBounds.width, this.frameBounds.height);
//this.context.fillStyle = this.renderDebugPointColor;
@@ -78,6 +78,12 @@ module Phaser {
*/
public static parseJSONData(game: Game, json): FrameData {
// Malformed?
if (!json['frames'])
{
throw new Error("Phaser.AnimationLoader.parseJSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
}
// Let's create some frames then
var data: FrameData = new FrameData();
@@ -98,6 +104,12 @@ module Phaser {
public static parseXMLData(game: Game, xml, format: number): FrameData {
// Malformed?
if (!xml.getElementsByTagName('TextureAtlas'))
{
throw new Error("Phaser.AnimationLoader.parseXMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
}
// Let's create some frames then
var data: FrameData = new FrameData();