Skip to content

Asynchronous loading texture #3350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cocos2d/core/event-manager/CCEventHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,32 @@ cc.EventHelper.prototype = {
return false;
},

removeEventListener: function( type, target){
removeEventListener: function( type, listener){
if ( this._listeners === undefined )
return;

var listeners = this._listeners;
var listenerArray = listeners[ type ];
if (!listenerArray) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do this, you are checking it in the next if


if ( listenerArray !== undefined ) {
for(var i = 0; i < listenerArray.length ; ){
var selListener = listenerArray[i];
if(selListener.callback === listener)
listenerArray.splice( i, 1 );
else
i++
}
}
},

removeEventTarget: function( type, listener, target){
if ( this._listeners === undefined )
return;

var listeners = this._listeners;
var listenerArray = listeners[ type ];
if (!listenerArray) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do this, you are checking it in the next if


if ( listenerArray !== undefined ) {
for(var i = 0; i < listenerArray.length ; ){
Expand Down
50 changes: 35 additions & 15 deletions cocos2d/core/sprites/CCSprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
ctor: function (fileName, rect, rotated) {
var self = this;
cc.Node.prototype.ctor.call(self);
self._loader = new cc.Sprite.loadManager(self);
self._shouldBeHidden = false;
self._offsetPosition = cc.p(0, 0);
self._unflippedOffsetPositionFromCenter = cc.p(0, 0);
Expand Down Expand Up @@ -246,11 +247,11 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
*/
initWithSpriteFrame:function (spriteFrame) {
cc.assert(spriteFrame, cc._LogInfos.Sprite_initWithSpriteFrame);

this._loader.clear();
if(!spriteFrame.textureLoaded()){
//add event listener
this._textureLoaded = false;
spriteFrame.addEventListener("load", this._renderCmd._spriteFrameLoadedCallback, this);
this._loader.add(spriteFrame, this._renderCmd._spriteFrameLoadedCallback, this);
}

//TODO
Expand Down Expand Up @@ -669,6 +670,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
initWithTexture: function (texture, rect, rotated, counterclockwise) {
var _t = this;
cc.assert(arguments.length !== 0, cc._LogInfos.CCSpriteBatchNode_initWithTexture);
this._loader.clear();

rotated = rotated || false;
texture = this._renderCmd._handleTextureForRotatedTexture(texture, rect, rotated, counterclockwise);
Expand Down Expand Up @@ -705,9 +707,8 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
_t._rect.width = rect.width;
_t._rect.height = rect.height;
}
if(_t.texture)
_t.texture.removeEventListener("load", _t);
texture.addEventListener("load", _t._renderCmd._textureLoadedCallback, _t);

this._loader.add(texture, _t._renderCmd._textureLoadedCallback, _t);
_t.setTexture(texture);
return true;
}
Expand Down Expand Up @@ -789,6 +790,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
newFrame = cc.spriteFrameCache.getSpriteFrame(newFrame);
cc.assert(newFrame, cc._LogInfos.Sprite_setSpriteFrame)
}
this._loader.clear();

this.setNodeDirty(true);

Expand All @@ -801,7 +803,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
var locTextureLoaded = newFrame.textureLoaded();
if (!locTextureLoaded) {
_t._textureLoaded = false;
newFrame.addEventListener("load", function (sender) {
this._loader.add(newFrame, function (sender) {
_t.setNodeDirty(true);
_t._textureLoaded = true;
var locNewTexture = sender.getTexture();
Expand Down Expand Up @@ -898,6 +900,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
* @param {cc.Texture2D|String} texture
*/
setTexture: function (texture) {
this._loader.clear();
if(!texture)
return this._renderCmd._setTexture(null);

Expand All @@ -913,12 +916,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
this._textureLoaded = true;
}else{
this._renderCmd._setTexture(null);
texture.addEventListener("load", function(){
this.setNodeDirty(true);
this._setTexture(texture, isFileName);
this.setColor(this._realColor);
this._textureLoaded = true;
}, this);
this._loader.add(texture, this._renderCmd._textureLoadedCallback, this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work properly, it will check node's _textureLoaded and will return directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

texture._loaded === true;
_textureLoaded is not updated

}
},

Expand All @@ -931,9 +929,9 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
_changeRectWithTexture: function(texture){
var contentSize = texture._contentSize;
var rect = cc.rect(
0, 0,
contentSize.width, contentSize.height
);
0, 0,
contentSize.width, contentSize.height
);
this.setTextureRect(rect);
},

Expand Down Expand Up @@ -990,3 +988,25 @@ cc.EventHelper.prototype.apply(cc.Sprite.prototype);
cc.assert(cc.isFunction(cc._tmp.PrototypeSprite), cc._LogInfos.MissingFile, "SpritesPropertyDefine.js");
cc._tmp.PrototypeSprite();
delete cc._tmp.PrototypeSprite;

(function () {
var manager = cc.Sprite.loadManager = function (target) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class name should start with UpperCase letter

this.target = target;
Copy link
Contributor

@pandamicro pandamicro Aug 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Seems you are not using it

this.list = [];
};

manager.prototype.add = function (source, callback, target) {
source.addEventListener('load', callback, target);
this.list.push({
source: source,
listener: callback,
target: target
});
};
manager.prototype.clear = function () {
while (this.list.length > 0) {
var item = this.list.pop();
item.source.removeEventListener('load', item.listener, item.target);
}
};
})();
2 changes: 1 addition & 1 deletion cocos2d/core/textures/CCTexture2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
* @param {cc.Node} target
*/
removeLoadedEventListener: function (target) {
this.removeEventListener("load", target);
this.removeEventTarget("load", target);
},

_generateColorTexture: function(){/*overide*/},
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/textures/TexturesWebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ cc._tmp.WebGLTexture2D = function () {
* @param {cc.Node} target
*/
removeLoadedEventListener: function (target) {
this.removeEventListener("load", target);
this.removeEventTarget("load", target);
}
});
};
Expand Down
Loading