xkorikx Posted May 3, 2014 Share Posted May 3, 2014 (edited) Hi,I'm some newbie programmer that need a little help in his starts.Now i have three questions to ask you. First. I've find an examples of sprite garbageCollector, but its work only in older versions (i test it on 1.1.5). How can i fix it?Here is code:var game = new Phaser.Game(800, 480, Phaser.AUTO, '', { preload: preload, create: create, update: update });var clouds;var cloud_dummy;function preload() { game.load.image('mario_cloud', 'mario_cloud.png');}function create() { clouds = game.add.group(); clouds.enableBody = true; clouds.createMultiple( 10, 'mario_cloud'); timer_shit = game.time.events.loop(1500, add_cloud, this);}function update() {}function add_cloud() { cloud_dummy = clouds.getFirstDead(); cloud_dummy.reset(800, 100); cloud_dummy.body.velocity.x -= 150; cloud_dummy.body.immovable = true; cloud_dummy.outOfBoundsKill = true;}Error message:Phaser v2.0.3 - WebGL - WebAudio http://phaser.io ♥♥♥ phaser.min.js:8 Uncaught TypeError: Cannot read property 'reset' of null (index):34 add_cloud (index):34 b.Timer.update phaser.min.js:12 b.Time.update phaser.min.js:12 b.Game.update phaser.min.js:8 b.RequestAnimationFrame.updateRAF phaser.min.js:11 window.requestAnimationFrame.forceSetTimeOut._onLoop phaser.min.js:11 Second question:I've some trouble with accessing elements of group. In example that u can see above, we have group named clouds and a cycle that add cloud_dummy elements to it.For example i have 10 elements on the screen, and i wanna access 3rd element of group to run other animation.run(). How can i do this trick? Third question:I've 2 browsers on my net-book, Chrome and Firefox. In Firefox my games(like example above) lag so hard, but Chrome give me around 60fps and have a little lagspikes that drop fps to 50. Phaser.CANVAS works better in Firefox and Phaser.WEBGL works better in chrome. How can i optimize my games to work better in both browsers? EDIT 03.05.2014:I've find a reason why my garbageCollector won't work. I forgot to check world bound for every sprite...Code shoul look like that:function add_cloud() { cloud_dummy = clouds.getFirstDead(); cloud_dummy.reset(800, 100); cloud_dummy.body.velocity.x -= 150; cloud_dummy.body.immovable = true; cloud_dummy.checkWorldBounds = true; cloud_dummy.outOfBoundsKill = true;} Edited May 3, 2014 by xkorikx Link to comment Share on other sites More sharing options...
codevinsky Posted May 5, 2014 Share Posted May 5, 2014 The proper way to do recycling is as follows:function addCloud() { var cloud = clouds.getFirstExists(false); if(!cloud) { cloud = game.add.sprite(0,0, 'mario_cloud'); game.physics.arcade.enableBody(cloud); cloud.body.velocity.x = -150; cloud.body.immovable = true; cloud.checkWorldBounds = true; cloud.outOfBoundsKill = true; } cloud.reset(800,100); cloud.revive();}group.getFirstExists(false) will get the first element of the group that no longer exists. This usually indicates that "kill()" has been called on this element. It's a much safer way to cycle through "dead" objects. Link to comment Share on other sites More sharing options...
Recommended Posts