@@ -213,7 +213,9 @@ void LazySprite::loadFromFile(const std::filesystem::path& path, Format format,
213
213
res = std::move (res)
214
214
]() mutable {
215
215
auto self = selfref.lock ();
216
- if (!self) return ;
216
+
217
+ // if sprite was destructed or loading has been cancelled, do nothing
218
+ if (!self || !self->m_isLoading ) return ;
217
219
218
220
if (!res) {
219
221
self->onError (fmt::format (" failed to load from file {}: {}" , path, res.unwrapErr ()));
@@ -260,7 +262,7 @@ void LazySprite::doInitFromBytes(std::vector<uint8_t> data, std::string cacheKey
260
262
261
263
Loader::get ()->queueInMainThread ([selfref = std::move (selfref)]() mutable {
262
264
auto self = selfref.lock ();
263
- if (self) {
265
+ if (self && self-> m_isLoading ) {
264
266
self->onError (" invalid image data or format" );
265
267
}
266
268
});
@@ -269,15 +271,15 @@ void LazySprite::doInitFromBytes(std::vector<uint8_t> data, std::string cacheKey
269
271
}
270
272
271
273
// image initialization succeeded, all we need to do now is to
272
- // create the opengl texture (must be on main thread!) and then set this sprite to use that.
274
+ // create the OpenGL texture (must be on main thread!) and then set this sprite to use that.
273
275
274
276
Loader::get ()->queueInMainThread ([
275
277
selfref = std::move (selfref),
276
278
image,
277
279
cacheKey = std::move (cacheKey)
278
280
] {
279
281
auto self = selfref.lock ();
280
- if (!self) return ;
282
+ if (!self || !self-> m_isLoading ) return ;
281
283
282
284
auto texture = new CCTexture2D ();
283
285
if (!texture->initWithImage (image)) {
@@ -399,6 +401,15 @@ bool LazySprite::isLoading() {
399
401
return m_isLoading;
400
402
}
401
403
404
+ void LazySprite::cancelLoad () {
405
+ m_isLoading = false ;
406
+
407
+ if (m_loadingCircle) {
408
+ m_loadingCircle->removeFromParent ();
409
+ m_loadingCircle = nullptr ;
410
+ }
411
+ }
412
+
402
413
LazySprite* LazySprite::create (CCSize size, bool loadingCircle) {
403
414
auto ret = new LazySprite;
404
415
if (ret->init (size, loadingCircle)) {
0 commit comments