Ninjadoodle Posted November 5, 2014 Share Posted November 5, 2014 Hi guys I'm cleaning up my game for devices ( using Cocoon JS ) and loading my levels dynamically ( level by level ), not to overload the device. This works nicely until about 2/3 of the game, when I get a 'Memory Warning' on both Android and iOS. I'm assuming that the graphics loaded each level are stacking up, and the phone is only releasing what it needs when the memory gets full. Is there a way I can 'Unload' the assets, in the same way I load them, when they are no longer needed? I'm worried this might cause crashes on weaker devices. Thank you in advance for any help! Quote Link to comment Share on other sites More sharing options...
enpu Posted November 6, 2014 Share Posted November 6, 2014 Hey! I have added game.clearAssets(); method that will clear whole texture cache. I will soon add another method, that you can use to destroy specific texture. Quote Link to comment Share on other sites More sharing options...
enpu Posted November 6, 2014 Share Posted November 6, 2014 Maybe i should add that also into game.Loader and setScene function as parameter, something like this:game.system.setScene('Level2', true); // this will clear texture cache, when changing sceneWhat do you think? Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 6, 2014 Author Share Posted November 6, 2014 Hi @enpu I think that would be a really good idea, as you have a choice whether to clear it or not. In my case, once I finish a level, 99% of the time those graphics are not used again Thank you!! Quote Link to comment Share on other sites More sharing options...
enpu Posted November 6, 2014 Share Posted November 6, 2014 Ok just pushed some changes, the syntax is now this:game.removeAsset('logo.png'); // Remove sprite from memorygame.removeAsset('sprites.json'); // Remove sprite sheet from memorygame.removeAssets(); // Remove all assets from memorygame.system.setScene('Game', true); // Change scene and remove all assets from memory Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 6, 2014 Author Share Posted November 6, 2014 Hi @enpu This is awesome! Just one question ... if I'm using the loader instead of game.system.setScene, can a similar syntax be used ? // Load next level assets here var loader = new game.Loader('Level1'); loader.start(); Quote Link to comment Share on other sites More sharing options...
enpu Posted November 6, 2014 Share Posted November 6, 2014 I think this should work:var loader = new game.Loader('Level1');game.removeAssets();loader.start();Let me know how it works Quote Link to comment Share on other sites More sharing options...
Stephan Posted November 6, 2014 Share Posted November 6, 2014 This is really a GREAT feature enpu! Thanx for adding it so quickly. This will come in very handily in several big projects that I will be working on. Is it possible already to clear the sound cache in a similar way? Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 6, 2014 Author Share Posted November 6, 2014 Hi @enpu I've just been testing this and there is one problem ... If you removeAsset, the engine doesn't seem to let you add the same asset at a later stage. For example if I load a levels assets, then unload them after leaving, then try to load the assets again upon entering, I get an error Quote Link to comment Share on other sites More sharing options...
Phempt Posted November 6, 2014 Share Posted November 6, 2014 Maybe i should add that also into game.Loader and setScene function as parameter, something like this:game.system.setScene('Level2', true); // this will clear texture cache, when changing sceneWhat do you think? I really like this idea! Quote Link to comment Share on other sites More sharing options...
enpu Posted November 6, 2014 Share Posted November 6, 2014 @Ninjadoodle you are right, will fix that asap! Quote Link to comment Share on other sites More sharing options...
enpu Posted November 6, 2014 Share Posted November 6, 2014 Should be now fixed! Little change to the code order:// Remove all assetsgame.removeAssets();// Add new assetsgame.addAsset('sprite.png');var loader = new game.Loader('Level1');loader.start(); Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 6, 2014 Author Share Posted November 6, 2014 Hi @enpu I'm not sure what's causing this, but reloading assets that have been removed before, makes CocoonJS crash (no error, the app just quits). It now works on desktop/browser, but not in Cocoon. Thank you for working on this!! Quote Link to comment Share on other sites More sharing options...
enpu Posted November 7, 2014 Share Posted November 7, 2014 Ok i think i have fixed the issue, can you test again Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted November 7, 2014 Author Share Posted November 7, 2014 Hi @enpu Awesome! It works perfectly now and no 'memory warning' as far as I can tell Thank you heaps for adding this awesome feature! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.