casey Posted January 25, 2018 Share Posted January 25, 2018 I am trying to test out persistence of objects--at the moment an image, but eventually a group that will be a UI component. Right now, my image that I want to persist ("persist") goes from State1 to State2 just fine, but when I go back to State1, it disappears and I get an error ("uncaught typeError: cannot read property 'compressionAlgorithm of null in Phaser.js" and then points to a bunch of pixi.webgl render locations) Why is the item disappearing if it's been added to the stage, and how do I overcome this problem? Here is the code for State1: var state1 = { preload: function () { this.load.image('persist', 'persist.png'); this.load.image('btn', 'btn.png'); }, create: function () { this.persist = game.add.image(this.world.centerX, this.world.centerY, 'persist'); this.game.stage.addChild(this.persist); this.btn = this.add.image(200, 200, 'btn'); this.btn.inputEnabled = true; this.btn.events.onInputDown.add(this.changeState, this); }, changeState: function() { this.state.start("state2"); } And then State 2 is just taking us back to state1 on clicking the button var state2 ={ preload: function(){ this.load.image('btn', 'inventory/btn.png'); }, create: function(){ this.btn = this.add.image(200, 200, 'btn'); this.btn.inputEnabled = true; this.btn.events.onInputDown.add(this.changeState, this); }, changeState: function(){ this.state.start("state1"); } }; Thanks in advance Link to comment Share on other sites More sharing options...
samme Posted January 25, 2018 Share Posted January 25, 2018 Use distinct keys for the two button images. Either use checkImageKey in the preload calls or move all the preloading to a boot state (run only once), e.g., var boot = { preload: function () { this.load.image('persist', 'persist.png'); this.load.image('btn', 'btn.png'); this.load.image('btn-inventory', 'inventory/btn.png'); }, create: function () { this.state.start('state1'); } }; Link to comment Share on other sites More sharing options...
casey Posted January 25, 2018 Author Share Posted January 25, 2018 Brilliant. Wasted all day on that (well, that and trying to get a function statement to work inside a switch statement. grrrr. Link to comment Share on other sites More sharing options...
Recommended Posts