-
Notifications
You must be signed in to change notification settings - Fork 905
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
Changes from 3 commits
920fb0f
81cceea
73e9281
a22afda
bade37a
fbb3d04
42494a4
df5fb23
b5c9645
15d00e3
9ee41bd
58ac462
a52c830
7a84cdc
06dcfeb
3c9515f
4dcd488
ef34d55
7f9f471
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ; ){ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
|
||
|
@@ -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(); | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work properly, it will check node's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. texture._loaded === true; |
||
} | ||
}, | ||
|
||
|
@@ -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); | ||
}, | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Class name should start with UpperCase letter |
||
this.target = target; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
}; | ||
})(); |
There was a problem hiding this comment.
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