Skip to content
Browse files

fixed #1080 cc.Loader

  • Loading branch information...
1 parent 65eddfe commit 3445fb6384f4c726af055d9c6b105d0da3a49c8b @ShengxiangChen ShengxiangChen committed Sep 18, 2012
View
8 CocosDenshion/SimpleAudioEngine.js
@@ -145,7 +145,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
this.removeEventListener('canplaythrough', arguments.callee, false);
}, false);
soundCache.addEventListener("error", function (e) {
- cc.Loader.shareLoader().onResLoadingErr();
+ cc.Loader.getInstance().onResLoadingErr();
}, false);
soundCache.addEventListener("playing", function (e) {
cc.sharedEngine._isBgmPlaying = true;
@@ -159,7 +159,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
this._bgmList[path] = soundCache
}
- cc.Loader.shareLoader().onResLoaded();
+ cc.Loader.getInstance().onResLoaded();
},
/**
* Play background music.
@@ -513,7 +513,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
false);
}, false);
soundCache.addEventListener("error", function (e) {
- cc.Loader.shareLoader().onResLoadingErr();
+ cc.Loader.getInstance().onResLoadingErr();
}, false);*/
// load it
@@ -522,7 +522,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
this._audioList[path] = [];
this._pushEffectCache(path);
}
- cc.Loader.shareLoader().onResLoaded();
+ cc.Loader.getInstance().onResLoaded();
},
/**
View
8 Demo/HelloDomMenu/main.js
@@ -32,13 +32,13 @@ var cocos2dApp = cc.Application.extend({
cc.COCOS2D_DEBUG = this.config['COCOS2D_DEBUG'];
cc.setup(this.config['tag']);
cc.AudioEngine.getInstance().init("mp3,ogg");
- cc.Loader.shareLoader().onloading = function () {
- cc.LoaderScene.shareLoaderScene().draw();
+ cc.Loader.getInstance().onloading = function () {
+ cc.LoaderScene.getInstance().draw();
};
- cc.Loader.shareLoader().onload = function () {
+ cc.Loader.getInstance().onload = function () {
cc.AppController.shareAppController().didFinishLaunchingWithOptions();
};
- cc.Loader.shareLoader().preload([
+ cc.Loader.getInstance().preload([
{type:"image", src:"res/HelloWorld.png"},
{type:"image", src:"res/CloseNormal.png"},
{type:"image", src:"res/CloseSelected.png"}
View
8 Demo/NativeClientDemo/main.js
@@ -32,13 +32,13 @@ var cocos2dApp = cc.Application.extend({
cc.COCOS2D_DEBUG = this.config['COCOS2D_DEBUG'];
cc.setup(this.config['tag']);
cc.AudioEngine.getInstance().init("mp3,ogg");
- cc.Loader.shareLoader().onloading = function () {
- cc.LoaderScene.shareLoaderScene().draw();
+ cc.Loader.getInstance().onloading = function () {
+ cc.LoaderScene.getInstance().draw();
};
- cc.Loader.shareLoader().onload = function () {
+ cc.Loader.getInstance().onload = function () {
cc.AppController.shareAppController().didFinishLaunchingWithOptions();
};
- cc.Loader.shareLoader().preload([
+ cc.Loader.getInstance().preload([
{type:"image", src:"res/HelloWorld.png"},
{type:"image", src:"res/CloseNormal.png"},
{type:"image", src:"res/CloseSelected.png"}
View
0 Demo/demo.html → Demo/index.html
File renamed without changes.
View
8 HelloHTML5World/main.js
@@ -32,13 +32,13 @@ var cocos2dApp = cc.Application.extend({
cc.COCOS2D_DEBUG = this.config['COCOS2D_DEBUG'];
cc.setup(this.config['tag']);
cc.AudioEngine.getInstance().init("mp3,ogg");
- cc.Loader.shareLoader().onloading = function () {
- cc.LoaderScene.shareLoaderScene().draw();
+ cc.Loader.getInstance().onloading = function () {
+ cc.LoaderScene.getInstance().draw();
};
- cc.Loader.shareLoader().onload = function () {
+ cc.Loader.getInstance().onload = function () {
cc.AppController.shareAppController().didFinishLaunchingWithOptions();
};
- cc.Loader.shareLoader().preload(g_ressources);
+ cc.Loader.getInstance().preload(g_ressources);
},
applicationDidFinishLaunching:function () {
// initialize director
View
57 cocos2d/CCLoader.js
@@ -38,7 +38,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* Check the loading status
*/
isLoadedComplete:function () {
- var loaderCache = cc.Loader.shareLoader();
+ var loaderCache = cc.Loader.getInstance();
if (loaderCache.loadedResourceCount == loaderCache.resourceCount) {
if (loaderCache.onload) {
loaderCache.timer = setTimeout(loaderCache.onload, 16);
@@ -50,7 +50,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
loaderCache.timer = setTimeout(loaderCache.onloading, 16);
}
else {
- cc.LoaderScene.shareLoaderScene().draw();
+ cc.LoaderScene.getInstance().draw();
}
loaderCache.timer = setTimeout(loaderCache.isLoadedComplete, 16);
}
@@ -61,7 +61,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* @param {String} name
* @example
* //example
- * cc.Loader.shareLoader().onResLoadingErr(name);
+ * cc.Loader.getInstance().onResLoadingErr(name);
*/
onResLoadingErr:function (name) {
cc.log("cocos2d:Failed loading resource: " + name);
@@ -71,7 +71,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
*Callback when a resource file loaded.
* @example
* //example
- * cc.Loader.shareLoader().onResLoaded();
+ * cc.Loader.getInstance().onResLoaded();
*/
onResLoaded:function () {
this.loadedResourceCount++;
@@ -83,19 +83,19 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* @return {Number}
* @example
* //example
- * cc.log(cc.Loader.shareLoader().getProgressBar() + "%");
+ * cc.log(cc.Loader.getInstance().getProgressBar() + "%");
*/
getProgressBar:function () {
var per = this.loadedResourceCount / this.resourceCount;
- per = parseInt(per * 100);
+ per = 0 | (per * 100);
return per;
},
/**
* status when resources loading success
* @example
* //example
- * cc.Loader.shareLoader().onload = function () {
+ * cc.Loader.getInstance().onload = function () {
* cc.AppController.shareAppController().didFinishLaunchingWithOptions();
* };
*/
@@ -105,7 +105,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* status when res loading error
* @example
* //example
- * cc.Loader.shareLoader().onerror = function () {
+ * cc.Loader.getInstance().onerror = function () {
* //do something
* };
*/
@@ -115,8 +115,8 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* status when res loading
* @example
* //example
- * cc.Loader.shareLoader().onloading = function () {
- * cc.LoaderScene.shareLoaderScene().draw();
+ * cc.Loader.getInstance().onloading = function () {
+ * cc.LoaderScene.getInstance().draw();
* };
*/
onloading:undefined,
@@ -131,40 +131,36 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* {type:"image", src:"hello.png"},
* {type:"tmx", src:"hello.tmx"}
* ]
- * cc.Loader.shareLoader().preload(res);
+ * cc.Loader.getInstance().preload(res);
*/
preload:function (res) {
var sharedTextureCache = cc.TextureCache.getInstance();
var sharedEngine = cc.AudioEngine.getInstance();
- var shareParser = cc.SAXParser.shareParser();
+ var sharedParser = cc.SAXParser.getInstance();
+ var sharedFileUtils = cc.FileUtils.getInstance();
+ this.resourceCount = res.length;
for (var i = 0; i < res.length; i++) {
switch (res[i].type) {
case "image":
sharedTextureCache.addImage(res[i].src);
- this.resourceCount += 1;
break;
case "bgm":
sharedEngine.preloadBackgroundMusic(res[i].src);
- this.resourceCount += 1;
break;
case "effect":
sharedEngine.preloadEffect(res[i].src);
- this.resourceCount += 1;
break;
case "plist":
case "tmx":
case "fnt":
- shareParser.preloadPlist(res[i].src);
- this.resourceCount += 1;
+ sharedParser.preloadPlist(res[i].src);
break;
case "tga":
//cc.log("cocos2d:not implemented yet")
break;
case "ccbi":
- case "binary":
- cc.FileUtils.getInstance().preloadBinaryFileData(res[i].src);
- this.resourceCount += 1;
+ sharedFileUtils.preloadBinaryFileData(res[i].src);
break;
default:
throw "cocos2d:unknow type : " + res[i].type;
@@ -179,12 +175,13 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
* Share Loader
* @return {cc.Loader}
*/
-cc.Loader.shareLoader = function () {
- if (!cc.shareLoader) {
- cc.shareLoader = new cc.Loader();
+cc.Loader.getInstance = function () {
+ if (!this._instance) {
+ this._instance = new cc.Loader();
}
- return cc.shareLoader;
+ return this._instance;
};
+cc.Loader._instance = null;
/**
* Default loading screen, you can customize the loading screen.
@@ -218,17 +215,19 @@ cc.LoaderScene = cc.Class.extend(/** @lends cc.LoaderScene# */{
cc.renderContext.fillStyle = "#b2b4b3";
cc.renderContext.font = 'Bold 12px Verdana';
cc.renderContext.textAlign = 'left';
- cc.drawingUtil.fillText("Loading " + cc.Loader.shareLoader().getProgressBar() + "%", logoWidth + 30, logoHeight - 15);
+ cc.drawingUtil.fillText("Loading " + cc.Loader.getInstance().getProgressBar() + "%", logoWidth + 30, logoHeight - 15);
}
});
/**
* Shared loader scene
* @return {cc.LoaderScene}
*/
-cc.LoaderScene.shareLoaderScene = function () {
- if (!cc.shareLoaderScene) {
- cc.shareLoaderScene = new cc.LoaderScene();
+cc.LoaderScene.getInstance = function () {
+ if (!this._instance) {
+ this._instance = new cc.LoaderScene();
}
- return cc.shareLoaderScene;
+ return this._instance;
};
+
+cc.LoaderScene._instance = null;
View
2 cocos2d/label_nodes/CCLabelBMFont.js
@@ -154,7 +154,7 @@ cc.BMFontConfiguration = cc.Class.extend(/** @lends cc.BMFontConfiguration# */{
},
_parseConfigFile:function (controlFile) {
- var data = cc.SAXParser.shareParser().getList(controlFile);
+ var data = cc.SAXParser.getInstance().getList(controlFile);
cc.Assert(data, "cc.BMFontConfiguration._parseConfigFile | Open file error.");
// parse spacing / padding
View
6 cocos2d/platform/CCFileUtils.js
@@ -99,7 +99,7 @@ cc.FileUtils = cc.Class.extend({
xhr.onload = function(e) {
var arrayStr = xhr.responseText;
if(arrayStr){
- cc.Loader.shareLoader().onResLoaded();
+ cc.Loader.getInstance().onResLoaded();
selfPointer._fileDataCache[fileUrl] = selfPointer._stringConvertToArray(arrayStr);
}
};
@@ -214,7 +214,7 @@ cc.FileUtils = cc.Class.extend({
* @return {object} The Dictionary of object generated from the file
*/
dictionaryWithContentsOfFile:function (fileName) {
- var parser = cc.SAXParser.shareParser();
+ var parser = cc.SAXParser.getInstance();
this.rootDict = parser.parse(fileName);
return this.rootDict;
},
@@ -283,7 +283,7 @@ cc.DictMaker = cc.Class.extend(/** @lends cc.DictMaker# */{
* @return {Array}
*/
dictionaryWithContentsOfFile:function (fileName) {
- var parser = cc.SAXParser.shareParser();
+ var parser = cc.SAXParser.getInstance();
this.rootDict = parser.parse(fileName);
return this.rootDict;
}
View
25 cocos2d/platform/CCSAXParser.js
@@ -173,13 +173,22 @@ cc.SAXParser = cc.Class.extend(/** @lends cc.SAXParser# */{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
+ var that = this;
+ xmlhttp.onreadystatechange = function () {
+ if (xmlhttp.readyState == 4) {
+ if (xmlhttp.status == 200) {
+ cc.Loader.getInstance().onResLoaded();
+ that.xmlList[filePath] = xmlhttp.responseText;
+ } else {
+ cc.Assert("cocos2d:There was a problem retrieving the xml data:" + xmlhttp.statusText);
+ }
+ }
+ };
// load xml
xmlhttp.open("GET", filePath, false);
xmlhttp.send(null);
- this.xmlList[filePath] = xmlhttp.responseText;
- cc.Loader.shareLoader().onResLoaded();
} else {
- alert("Your browser does not support XMLHTTP.");
+ cc.Assert("cocos2d:Your browser does not support XMLHTTP.");
}
},
@@ -223,9 +232,11 @@ cc.SAXParser = cc.Class.extend(/** @lends cc.SAXParser# */{
* @function
* @return {cc.SAXParser}
*/
-cc.SAXParser.shareParser = function () {
- if (!cc.shareParser) {
- cc.shareParser = new cc.SAXParser();
+cc.SAXParser.getInstance = function () {
+ if (!this._instance) {
+ this._instance = new cc.SAXParser();
}
- return cc.shareParser;
+ return this._instance;
};
+
+cc.SAXParser._instance = null;
View
6 cocos2d/textures/CCTextureCache.js
@@ -150,17 +150,17 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
cc.Assert(path != null, "TextureCache: path MUST not be null");
var texture = this.textures[path.toString()];
if (texture) {
- cc.Loader.shareLoader().onResLoaded();
+ cc.Loader.getInstance().onResLoaded();
}
else {
texture = new Image();
var that = this;
texture.addEventListener("load", function () {
- cc.Loader.shareLoader().onResLoaded();
+ cc.Loader.getInstance().onResLoaded();
});
texture.addEventListener("error", function () {
- cc.Loader.shareLoader().onResLoadingErr(path);
+ cc.Loader.getInstance().onResLoadingErr(path);
});
texture.src = path;
this.textures[path.toString()] = texture;
View
2 cocos2d/tileMap_parallax_nodes/CCTMXXMLParser.js
@@ -434,7 +434,7 @@ cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{
* @return {Element}
*/
parseXMLFile:function (tmxFile) {
- var mapXML = cc.SAXParser.shareParser().tmxParse(tmxFile);
+ var mapXML = cc.SAXParser.getInstance().tmxParse(tmxFile);
// PARSE <map>
var map = mapXML.documentElement;
View
2 index.html
@@ -53,7 +53,7 @@
<li><a href="HelloHTML5World/index.html">Hello World</a> <span
class="comment"> - Hello World for Cocos2d-HTML5</span></li>
<li><a href="tests/index.html">Test cases</a> <span class="comment"> - Engine Testcases</span></li>
- <li><a href="Demo/demo.html">Demos</a> <span class="comment"> - Platform Demos</span></li>
+ <li><a href="Demo/index.html">Demos</a> <span class="comment"> - Platform Demos</span></li>
</ol>
<hr/>
<h2>Note</h2>
View
8 template/main.js
@@ -32,13 +32,13 @@ var cocos2dApp = cc.Application.extend({
cc.COCOS2D_DEBUG = this.config['COCOS2D_DEBUG'];
cc.setup(this.config['tag']);
cc.AudioEngine.getInstance().init("mp3,ogg");
- cc.Loader.shareLoader().onloading = function () {
- cc.LoaderScene.shareLoaderScene().draw();
+ cc.Loader.getInstance().onloading = function () {
+ cc.LoaderScene.getInstance().draw();
};
- cc.Loader.shareLoader().onload = function () {
+ cc.Loader.getInstance().onload = function () {
cc.AppController.shareAppController().didFinishLaunchingWithOptions();
};
- cc.Loader.shareLoader().preload(g_ressources);
+ cc.Loader.getInstance().preload(g_ressources);
},
applicationDidFinishLaunching:function () {
// initialize director

0 comments on commit 3445fb6

Please sign in to comment.
Something went wrong with that request. Please try again.