mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
167 lines
5.8 KiB
JavaScript
167 lines
5.8 KiB
JavaScript
/****************************************************************************
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
http://www.cocos.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Chukong Aipu reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
/**
|
|
* <p>cc.LoaderScene is a scene that you can load it when you loading files</p>
|
|
* <p>cc.LoaderScene can present thedownload progress </p>
|
|
* @class
|
|
* @extends _ccsg.Scene
|
|
* @example
|
|
* var lc = new cc.LoaderScene();
|
|
*/
|
|
cc.LoaderScene = _ccsg.Scene.extend({
|
|
_interval : null,
|
|
_label : null,
|
|
_className:"LoaderScene",
|
|
_onProjectionChange: null,
|
|
cb: null,
|
|
target: null,
|
|
/**
|
|
* Contructor of cc.LoaderScene
|
|
* @returns {boolean}
|
|
*/
|
|
init : function(){
|
|
var self = this;
|
|
|
|
//logo
|
|
var logoWidth = 160;
|
|
var logoHeight = 200;
|
|
|
|
// bg
|
|
var bgLayer = self._bgLayer = new cc.LayerColor(cc.color(32, 32, 32, 255));
|
|
self.addChild(bgLayer, 0);
|
|
|
|
//image move to CCSceneFile.js
|
|
var fontSize = 24, lblHeight = -logoHeight / 2 + 100;
|
|
if(cc._loaderImage){
|
|
//loading logo
|
|
var img = new Image();
|
|
img.src = cc._loaderImage;
|
|
logoWidth = img.width;
|
|
logoHeight = img.height;
|
|
self._initStage(img, cc.visibleRect.center);
|
|
|
|
fontSize = 14;
|
|
lblHeight = -logoHeight / 2 - 10;
|
|
}
|
|
//loading percent
|
|
var label = self._label = new cc.LabelTTF("Loading... 0%", "Arial", fontSize);
|
|
label.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(0, lblHeight)));
|
|
label.setColor(cc.color(180, 180, 180));
|
|
bgLayer.addChild(this._label, 10);
|
|
return true;
|
|
},
|
|
|
|
_initStage: function (img, centerPos) {
|
|
var self = this;
|
|
var texture2d = self._texture2d = new cc.Texture2D();
|
|
texture2d.initWithElement(img);
|
|
texture2d.handleLoadedTexture();
|
|
var logo = self._logo = new _ccsg.Sprite(texture2d);
|
|
logo.x = centerPos.x;
|
|
logo.y = centerPos.y;
|
|
self._bgLayer.addChild(logo, 10);
|
|
},
|
|
/**
|
|
* custom onEnter
|
|
*/
|
|
onEnter: function () {
|
|
var self = this;
|
|
_ccsg.Node.prototype.onEnter.call(self);
|
|
self.schedule(self._startLoading, 0.3);
|
|
this._onProjectionChange = function(){
|
|
self._updateTransform();
|
|
};
|
|
cc.director.on(cc.Director.EVENT_PROJECTION_CHANGED, this._onProjectionChange);
|
|
},
|
|
/**
|
|
* custom onExit
|
|
*/
|
|
onExit: function () {
|
|
cc.director.off(cc.Director.EVENT_PROJECTION_CHANGED, this._onProjectionChange);
|
|
_ccsg.Node.prototype.onExit.call(this);
|
|
var tmpStr = "Loading... 0%";
|
|
this._label.setString(tmpStr);
|
|
},
|
|
|
|
/**
|
|
* init with resources
|
|
* @param {Array} resources
|
|
* @param {Function|String} cb
|
|
* @param {Object} target
|
|
*/
|
|
initWithResources: function (resources, cb, target) {
|
|
if(cc.js.isString(resources))
|
|
resources = [resources];
|
|
this.resources = resources || [];
|
|
this.cb = cb;
|
|
this.target = target;
|
|
},
|
|
|
|
_startLoading: function () {
|
|
var self = this;
|
|
self.unschedule(self._startLoading);
|
|
var res = self.resources;
|
|
cc.loader.load(res,
|
|
function (loadedCount, count) {
|
|
var percent = (loadedCount / count * 100) | 0;
|
|
percent = Math.min(percent, 100);
|
|
self._label.setString("Loading... " + percent + "%");
|
|
}, function (error, items) {
|
|
if (self.cb)
|
|
self.cb.call(self.target, error, items);
|
|
});
|
|
},
|
|
|
|
_updateTransform: function(){
|
|
this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty);
|
|
this._bgLayer._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty);
|
|
this._label._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty);
|
|
this._logo._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty);
|
|
}
|
|
});
|
|
/**
|
|
* <p>cc.LoaderScene.preload can present a loaderScene with download progress.</p>
|
|
* <p>when all the resource are downloaded it will invoke call function</p>
|
|
* @param resources
|
|
* @param cb
|
|
* @param target
|
|
* @returns {cc.LoaderScene|*}
|
|
* @example
|
|
* //Example
|
|
* cc.LoaderScene.preload(g_resources, function () {
|
|
cc.director.runScene(new HelloWorldScene());
|
|
}, this);
|
|
*/
|
|
cc.LoaderScene.preload = function(resources, cb, target){
|
|
var _cc = cc;
|
|
if(!_cc.loaderScene) {
|
|
_cc.loaderScene = new cc.LoaderScene();
|
|
_cc.loaderScene.init();
|
|
}
|
|
_cc.loaderScene.initWithResources(resources, cb, target);
|
|
|
|
cc.director.runScene(_cc.loaderScene);
|
|
return _cc.loaderScene;
|
|
}; |