Skip to content

Commit 94add2e

Browse files
committed
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.
1 parent c748530 commit 94add2e

9 files changed

Lines changed: 637 additions & 266 deletions

File tree

Phaser/Loader.ts

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -166,32 +166,23 @@ module Phaser {
166166
atlasData = JSON.parse(atlasData);
167167
}
168168

169-
// Malformed?
170-
if (atlasData['frames'])
171-
{
172-
this._queueSize++;
173-
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData['frames'], format: format, error: false, loaded: false };
174-
this._keys.push(key);
175-
}
176-
else
177-
{
178-
throw new Error("Phaser.Loader. Invalid Texture Atlas JSON given, missing frames block");
179-
}
169+
this._queueSize++;
170+
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData['frames'], format: format, error: false, loaded: false };
171+
this._keys.push(key);
180172
}
181173
else if (format == Loader.TEXTURE_ATLAS_XML_STARLING)
182174
{
183175
// An xml string or object has been given
184176
if (typeof atlasData === 'string')
185177
{
186-
var tmp;
187178
var xml;
188179

189180
try
190181
{
191182
if (window['DOMParser'])
192183
{
193-
tmp = new DOMParser();
194-
xml = tmp.parseFromString(atlasData, "text/xml");
184+
var domparser = new DOMParser();
185+
xml = domparser.parseFromString(atlasData, "text/xml");
195186
}
196187
else
197188
{
@@ -215,17 +206,9 @@ module Phaser {
215206
}
216207
}
217208

218-
// Malformed?
219-
if (atlasData.getElementsByTagName('TextureAtlas'))
220-
{
221-
this._queueSize++;
222-
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData, format: format, error: false, loaded: false };
223-
this._keys.push(key);
224-
}
225-
else
226-
{
227-
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
228-
}
209+
this._queueSize++;
210+
this._fileList[key] = { type: 'textureatlas', key: key, url: textureURL, data: null, atlasURL: null, atlasData: atlasData, format: format, error: false, loaded: false };
211+
this._keys.push(key);
229212
}
230213

231214
}
@@ -449,13 +432,9 @@ module Phaser {
449432
private jsonLoadComplete(key: string) {
450433

451434
var data = JSON.parse(this._xhr.response);
435+
var file = this._fileList[key];
452436

453-
// Malformed?
454-
if (data['frames'])
455-
{
456-
var file = this._fileList[key];
457-
this._game.cache.addTextureAtlas(file.key, file.url, file.data, data['frames'], file.format);
458-
}
437+
this._game.cache.addTextureAtlas(file.key, file.url, file.data, data['frames'], file.format);
459438

460439
this.nextFile(key, true);
461440

@@ -468,23 +447,24 @@ module Phaser {
468447
private dataLoadError(key: string) {
469448

470449
var file = this._fileList[key];
450+
471451
file.error = true;
452+
472453
this.nextFile(key, true);
473454

474455
}
475456

476457
private xmlLoadComplete(key: string) {
477458

478-
var atlasData = this._xhr.response; // xml?
479-
var tmp;
459+
var atlasData = this._xhr.response;
480460
var xml;
481461

482462
try
483463
{
484464
if (window['DOMParser'])
485465
{
486-
tmp = new DOMParser();
487-
xml = tmp.parseFromString(atlasData, "text/xml");
466+
var domparser = new DOMParser();
467+
xml = domparser.parseFromString(atlasData, "text/xml");
488468
}
489469
else
490470
{
@@ -503,16 +483,8 @@ module Phaser {
503483
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given");
504484
}
505485

506-
// Malformed?
507-
if (xml.getElementsByTagName('TextureAtlas'))
508-
{
509-
var file = this._fileList[key];
510-
this._game.cache.addTextureAtlas(file.key, file.url, file.data, xml, file.format);
511-
}
512-
else
513-
{
514-
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
515-
}
486+
var file = this._fileList[key];
487+
this._game.cache.addTextureAtlas(file.key, file.url, file.data, xml, file.format);
516488

517489
this.nextFile(key, true);
518490

Phaser/gameobjects/Sprite.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -185,29 +185,20 @@ module Phaser {
185185
* @param camera {Rectangle} The rectangle you want to check.
186186
* @return {boolean} Return true if bounds of this sprite intersects the given rectangle, otherwise return false.
187187
*/
188-
public inCamera(camera: Rectangle): bool {
188+
public inCamera(camera: Rectangle, cameraOffsetX: number, cameraOffsetY: number): bool {
189189

190190
// Object fixed in place regardless of the camera scrolling? Then it's always visible
191191
if (this.scrollFactor.x == 0 && this.scrollFactor.y == 0)
192192
{
193193
return true;
194194
}
195195

196-
// Otherwise, if it's scrolling perfectly in sync with the camera (1 to 1) then it's a simple bounds check on world coordinates
197-
if (this.scrollFactor.x == 1 && this.scrollFactor.y == 1)
198-
{
199-
return camera.intersects(this.frameBounds, this.frameBounds.length);
200-
}
201-
else
202-
{
203-
// Else apply the offsets
204-
this._dx = (this.frameBounds.x - camera.x) * this.scrollFactor.x;
205-
this._dy = (this.frameBounds.y - camera.y) * this.scrollFactor.y;
206-
this._dw = this.frameBounds.width * this.scale.x;
207-
this._dh = this.frameBounds.height * this.scale.y;
196+
this._dx = (this.frameBounds.x - camera.x);
197+
this._dy = (this.frameBounds.y - camera.y);
198+
this._dw = this.frameBounds.width * this.scale.x;
199+
this._dh = this.frameBounds.height * this.scale.y;
208200

209-
return (camera.right > this._dx) && (camera.x < this._dx + this._dw) && (camera.bottom > this._dy) && (camera.y < this._dy + this._dh);
210-
}
201+
return (camera.right > this._dx) && (camera.x < this._dx + this._dw) && (camera.bottom > this._dy) && (camera.y < this._dy + this._dh);
211202

212203
}
213204

@@ -248,7 +239,7 @@ module Phaser {
248239
public render(camera: Camera, cameraOffsetX: number, cameraOffsetY: number): bool {
249240

250241
// Render checks
251-
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)
242+
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)
252243
{
253244
return false;
254245
}
@@ -400,8 +391,8 @@ module Phaser {
400391

401392
if (this.renderDebug)
402393
{
403-
//this.renderBounds(camera, cameraOffsetX, cameraOffsetY);
404-
this.collisionMask.render(camera, cameraOffsetX, cameraOffsetY);
394+
this.renderBounds(camera, cameraOffsetX, cameraOffsetY);
395+
//this.collisionMask.render(camera, cameraOffsetX, cameraOffsetY);
405396
}
406397

407398
if (globalAlpha > -1)
@@ -421,14 +412,11 @@ module Phaser {
421412
*/
422413
private renderBounds(camera:Camera, cameraOffsetX:number, cameraOffsetY:number) {
423414

424-
//this._dx = cameraOffsetX + (this.frameBounds.topLeft.x - camera.worldView.x);
425-
//this._dy = cameraOffsetY + (this.frameBounds.topLeft.y - camera.worldView.y);
426-
427-
this._dx = cameraOffsetX + (this.collisionMask.x - camera.worldView.x);
428-
this._dy = cameraOffsetY + (this.collisionMask.y - camera.worldView.y);
415+
this._dx = cameraOffsetX + (this.frameBounds.topLeft.x - camera.worldView.x);
416+
this._dy = cameraOffsetY + (this.frameBounds.topLeft.y - camera.worldView.y);
429417

430418
this.context.fillStyle = this.renderDebugColor;
431-
this.context.fillRect(this._dx, this._dy, this.collisionMask.width, this.collisionMask.height);
419+
this.context.fillRect(this._dx, this._dy, this.frameBounds.width, this.frameBounds.height);
432420

433421
//this.context.fillStyle = this.renderDebugPointColor;
434422

Phaser/system/animation/AnimationLoader.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ module Phaser {
7878
*/
7979
public static parseJSONData(game: Game, json): FrameData {
8080

81+
// Malformed?
82+
if (!json['frames'])
83+
{
84+
throw new Error("Phaser.AnimationLoader.parseJSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
85+
}
86+
8187
// Let's create some frames then
8288
var data: FrameData = new FrameData();
8389

@@ -98,6 +104,12 @@ module Phaser {
98104

99105
public static parseXMLData(game: Game, xml, format: number): FrameData {
100106

107+
// Malformed?
108+
if (!xml.getElementsByTagName('TextureAtlas'))
109+
{
110+
throw new Error("Phaser.AnimationLoader.parseXMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
111+
}
112+
101113
// Let's create some frames then
102114
var data: FrameData = new FrameData();
103115

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ V0.9.6
103103
* Added Loader.crossOrigin property which is applied to loaded Images
104104
* Added AnimationManager.destroy() to clear out all local references and objects
105105
* Added the clearAnimations parameter to Sprite.loadGraphic(). Allows you to change animation textures but retain the frame data.
106-
* Added the GameObjectFactory to Game. You now make Sprites like this: game.add.sprite(). Much better separation of game object creation methods now. But you'll have to update ALL code, sorry!
106+
* Added the GameObjectFactory to Game. You now make Sprites like this: game.add.sprite(). Much better separation of game object creation methods now. But you'll have to update ALL code, sorry! (blame JesseFreeman for breaking your code and coming up with the idea :)
107107
* Added GameObjectFactory methods to add existing objects to the game world, such as existingSprite(), existingTween(), etc.
108108
* Added the GameObjectFactory to Phaser.State
109109
* Added new format parameter to Loader.addTextureAtlas defining the format. Currently supported: JSON Array and Starling/Sparrow XML.
@@ -112,11 +112,10 @@ V0.9.6
112112
* TODO: Game.Time should monitor pause duration
113113
* TODO: Investigate bug re: tilemap collision and animation frames
114114
* TODO: Update tests that use arrow keys and include touch/mouse support (FlxControlHandler style)
115-
* TODO: GameObject.clipRect
115+
* TODO: GameObject.clipRect - won't work with rotation :( have to use context.clip which is crazy expensive, damnit
116116
* TODO: Polygon geom primitive
117117
* TODO: Move GameObject transforms to a single matrix
118118
* TODO: this.target.view.style.cursor = "pointer"; ("default")
119-
* TODO: Fix bug in scrollFactor inCamera check where the scrollFactor > 0 and < 1
120119
* TODO: If the Camera is larger than the Stage size then the rotation offset isn't correct
121120
* TODO: Texture Repeat doesn't scroll, because it's part of the camera not the world, need to think about this more
122121

0 commit comments

Comments
 (0)