Files
phaser/Phaser/Cache.js
T
Richard Davey e74114f384 Docs
2013-08-12 00:52:35 +01:00

128 lines
4.1 KiB
JavaScript

/// <reference path="Game.ts" />
/**
* Phaser - Cache
*
* A game only has one instance of a Cache and it is used to store all externally loaded assets such
* as images, sounds and data files as a result of Loader calls. Cache items use string based keys for look-up.
*/
var Phaser;
(function (Phaser) {
var Cache = (function () {
function Cache(game) {
this._game = game;
this._canvases = {
};
this._images = {
};
this._sounds = {
};
this._text = {
};
}
Cache.prototype.addCanvas = function (key, canvas, context) {
this._canvases[key] = {
canvas: canvas,
context: context
};
};
Cache.prototype.addSpriteSheet = function (key, url, data, frameWidth, frameHeight, frameMax) {
this._images[key] = {
url: url,
data: data,
spriteSheet: true,
frameWidth: frameWidth,
frameHeight: frameHeight
};
this._images[key].frameData = Phaser.AnimationLoader.parseSpriteSheet(this._game, key, frameWidth, frameHeight, frameMax);
};
Cache.prototype.addTextureAtlas = function (key, url, data, jsonData) {
this._images[key] = {
url: url,
data: data,
spriteSheet: true
};
this._images[key].frameData = Phaser.AnimationLoader.parseJSONData(this._game, jsonData);
};
Cache.prototype.addImage = function (key, url, data) {
this._images[key] = {
url: url,
data: data,
spriteSheet: false
};
};
Cache.prototype.addSound = function (key, url, data) {
this._sounds[key] = {
url: url,
data: data,
decoded: false
};
};
Cache.prototype.decodedSound = function (key, data) {
this._sounds[key].data = data;
this._sounds[key].decoded = true;
};
Cache.prototype.addText = function (key, url, data) {
this._text[key] = {
url: url,
data: data
};
};
Cache.prototype.getCanvas = function (key) {
if(this._canvases[key]) {
return this._canvases[key].canvas;
}
return null;
};
Cache.prototype.getImage = function (key) {
if(this._images[key]) {
return this._images[key].data;
}
return null;
};
Cache.prototype.getFrameData = function (key) {
if(this._images[key] && this._images[key].spriteSheet == true) {
return this._images[key].frameData;
}
return null;
};
Cache.prototype.getSound = function (key) {
if(this._sounds[key]) {
return this._sounds[key].data;
}
return null;
};
Cache.prototype.isSoundDecoded = function (key) {
if(this._sounds[key]) {
return this._sounds[key].decoded;
}
};
Cache.prototype.isSpriteSheet = function (key) {
if(this._images[key]) {
return this._images[key].spriteSheet;
}
};
Cache.prototype.getText = function (key) {
if(this._text[key]) {
return this._text[key].data;
}
return null;
};
Cache.prototype.destroy = function () {
for(var item in this._canvases) {
delete this._canvases[item['key']];
}
for(var item in this._images) {
delete this._images[item['key']];
}
for(var item in this._sounds) {
delete this._sounds[item['key']];
}
for(var item in this._text) {
delete this._text[item['key']];
}
};
return Cache;
})();
Phaser.Cache = Cache;
})(Phaser || (Phaser = {}));